---
title: Enabling web analytics
related:
  - https://docs.kentico.com/13/configuring-xperience/managing-sites/configuring-settings-for-sites/settings-on-line-marketing/settings-web-analytics.md
  - https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/web-analytics/viewing-web-analytics-reports.md
  - https://docs.kentico.com/13/on-line-marketing-features/configuring-and-customizing-your-on-line-marketing-features/configuring-on-line-marketing-permissions.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

To set up logging of [Web analytics](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/web-analytics.md) for your website:

- [Configure web analytics settings](#configuring-web-analytics-settings)
- [Enable web analytics on the side of your live site application](#enabling-analytics-on-the-live-site)
- (Optional) [Integrate a geolocation database for country tracking](#enabling-country-tracking)
- (Optional) [Implement custom code that enables technology statistics](#implementing-technology-statistic-tracking) (tracking of browser types, operating systems, mobile devices)

## Configuring web analytics settings

To enable overall logging of web analytics:

1. Open the **Settings** application in the Xperience administration interface.
2. Navigate to the **On-line marketing -> Web Analytics** category.
3. Select the **Enable web analytics** checkbox.
4. **Save** the settings.

The other settings in the Web analytics category enable or disable tracking of individual types of statistics and events. See [Settings - Web analytics](https://docs.kentico.com/13/configuring-xperience/managing-sites/configuring-settings-for-sites/settings-on-line-marketing/settings-web-analytics.md) for details.

## Enabling analytics on the live site

In addition to configuring the settings in the administration, developers need to enable web analytics on the side of the live site application:

<!-- dev-model:mvc start -->

**MVC 5 development model.** Applies only when building with ASP.NET MVC 5. If this page also covers ASP.NET Core, that version is in its own block.

1. Open your live site project in Visual Studio.
2. Enable the web analytics feature by calling the **UseWebAnalytics** method of the _ApplicationBuilder_ instance.

   - Enable the feature at the start of your application's life cycle, for example in the **Application\_Start** method of your project's **Global.asax** file.

     > **Info:** MVC projects created by the [installer](https://docs.kentico.com/13/installation/installing-xperience.md) contain the _ApplicationConfig_ class, whose _RegisterFeatures_ method is called in the _Application\_Start_ method by default. You can use this class to encapsulate all of your _ApplicationBuilder_ code (enabling and configuring of Xperience MVC features).

     > **Note:** **Note**: The feature must be enabled **before** you register routes into the application's _RouteTable_. The _Kentico().MapRoutes()_ method adds required routes based on the set of enabled features.

     ```csharp

     using Kentico.OnlineMarketing.Web.Mvc;
     using Kentico.Web.Mvc;

     ...

     protected void Application_Start()
     {
         ...

         // Gets the ApplicationBuilder instance
         // Allows you to enable and configure selected Xperience MVC integration features
         ApplicationBuilder builder = ApplicationBuilder.Current;

         // Enables the web analytics feature, which provides the endpoints (system routes) used for analytics logging
         builder.UseWebAnalytics();

         ...
     }

     ```
3. Register the web analytics logging script onto all pages by calling the **WebAnalyticsLoggingScript** extension method in views.
   - Call the method in your website's main layout (and any other used layouts) to ensure that analytics are logged for every page.
   - To enable logging of the _Invalid pages_ statistic, make sure the script is present on your site's [404 error handling page](https://docs.kentico.com/13/securing-websites/developing-secure-websites/handling-error-messages-securely/handling-404-errors.md).
   - The logging script runs asynchronously, so we recommend adding it at the end of the __ tag in the page code.

     ```csharp title="Required namespaces"

     @using Kentico.OnlineMarketing.Web.Mvc
     @using Kentico.Web.Mvc

     ```

     ```csharp

         @* Registers scripts that provide logging of web analytics data *@
         @Html.Kentico().WebAnalyticsLoggingScript()


     ```

<!-- dev-model:mvc end -->

<!-- dev-model:core start -->

**ASP.NET Core development model.** Applies only when building with ASP.NET Core. If this page also covers MVC 5, that version is in its own block.

1. Open your live site project in Visual Studio.
2. Edit your application's startup class (**Startup.cs** by default).
3. [Enable](https://docs.kentico.com/13/developing-websites/developing-xperience-applications-using-asp-net-core/starting-with-asp-net-core-development/enabling-xperience-features-in-asp-net-core-applications.md) the web analytics feature by calling the **UseWebAnalytics** method of the _IFeaturesBuilder_ delegate passed to the _IServiceCollection.AddKentico_ method within _ConfigureServices_.

   ```csharp

   using Kentico.OnlineMarketing.Web.Mvc;
   using Kentico.Web.Mvc;

   ...

   public void ConfigureServices(IServiceCollection services)
   {
       ...
       // Enables and configures Xperience features
       services.AddKentico(features =>
       {
           // Enables the web analytics feature, which provides the endpoints (system routes) used for analytics logging
           features.UseWebAnalytics();
           ... 
       });

       ...
   }

   ```
4. Register the web analytics logging script onto all pages by calling the **WebAnalyticsLoggingScript** extension method in views.
   - Call the method in your website's main layout (and any other used layouts) to ensure that analytics are logged for every page.
   - To enable logging of the _Invalid pages_ statistic, make sure the script is present on your site's 404 error handling page.
   - The logging script runs asynchronously, so we recommend adding it at the end of the __ tag in the page code.

     ```csharp title="Required namespaces"

     @using Kentico.OnlineMarketing.Web.Mvc
     @using Kentico.Web.Mvc

     ```

     ```csharp

         @* Registers scripts that provide logging of web analytics data *@
         @Html.Kentico().WebAnalyticsLoggingScript()


     ```

<!-- dev-model:core end -->

Enabling the _UseWebAnalytics_ feature ensures that the system registers routes that process logging requests. The analytics logging itself is performed by client scripts added to pages by the _WebAnalyticsLoggingScript_ extension method.

The site now logs analytics based on the [configured settings](#configuring-web-analytics-settings).

## Enabling country tracking

By default, the **Audience** -> **Countries** web analytics statistic is not functional and does not log any data. If you wish to track countries, you need to integrate a geolocation database into your project.

Follow the instructions on the [Using geolocation for contacts](https://docs.kentico.com/13/on-line-marketing-features/configuring-and-customizing-your-on-line-marketing-features/configuring-contacts/using-geolocation-for-contacts.md) page. Even though the system primarily uses geolocation to determine the addresses of contacts, the same functionality also enables country tracking for web analytics.

## Implementing technology statistic tracking

By default, the following web analytics statistics from the **Technology** category are not functional and do not log any data:

- Browser types
- Operating system
- Mobile devices

To track these statistics, you need to implement custom functionality for user agent parsing:

1. Open your **live site** solution in Visual Studio.
2. [Add a custom assembly](https://docs.kentico.com/13/custom-development/adding-custom-assemblies.md) (_Class Library_ project) with class discovery enabled to the solution.
3. Create a new class in the project that inherits from **AnalyticsUserAgentParser** (available in the _CMS.WebAnalytics_ namespace).
4. Override one or more of the following methods:
   - **TryGetBrowserNameInternal**
   - **TryGetOperatingSystemInternal**
   - **TryGetMobileDeviceInternal**

     > **Info:** The methods provide a _userAgent_ parameter representing the visitor's [user agent](https://en.wikipedia.org/wiki/User_agent) string. Your code needs to parse the string and identify the browser type, operating system or mobile device type (typically using a third-party library or database).
     >
     > In cases where the identification is successful, return a _true_ value and set the corresponding _out_ parameter to the value that you wish to log into the analytics data (the name of the browser, operating system or mobile device).
5. Register your custom class as the implementation of the **IAnalyticsUserAgentParser** service using the **RegisterImplementation** assembly attribute.

Depending on the methods that you implement, the corresponding web analytics statistics start working and logging data.

```csharp title="Basic example"

using CMS;
using CMS.WebAnalytics;

// Registers the custom IAnalyticsUserAgentParser implementation
[assembly: RegisterImplementation(typeof(IAnalyticsUserAgentParser), typeof(CustomUserAgentParser))]

public class CustomUserAgentParser : AnalyticsUserAgentParser
{
    /* Implements user agent parsing for the 'Browser types' web analytics statistic
    Note: This example is only a conceptual demonstration.
    In real-world scenarios, you will typically need to use advanced parsing and a third-party library or database.*/
    protected override bool TryGetBrowserNameInternal(string userAgent, out string browserName)
    {        
        if (userAgent.Contains("Chrome"))
        {
            // Browser type parsing from the user agent string was successful
            browserName = "Chrome-like browser";
            return true;
        }
        else
        {
            // Browser type parsing failed
            browserName = null;
            return false;
        }

    }
}

```
