Enabling web analytics

To set up logging of Web analytics for your website:

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 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:

  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.

      MVC projects created by the installer 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: 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.

      
      
      
        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.

    • The logging script runs asynchronously, so we recommend adding it at the end of the <head> tag in the page code.

      Required namespaces
      
      
      
        @using Kentico.OnlineMarketing.Web.Mvc
        @using Kentico.Web.Mvc
      
      
        
      
      
      
            @* Registers scripts that provide logging of web analytics data *@
            @Html.Kentico().WebAnalyticsLoggingScript()
      
      
      
        
  1. Open your live site project in Visual Studio.

  2. Edit your application’s startup class (Startup.cs by default).

  3. Enable the web analytics feature by calling the UseWebAnalytics method of the IFeaturesBuilder delegate passed to the IServiceCollection.AddKentico method within ConfigureServices.

    
    
    
     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 <head> tag in the page code.

      Required namespaces
      
      
      
        @using Kentico.OnlineMarketing.Web.Mvc
        @using Kentico.Web.Mvc
      
      
        
      
      
      
            @* Registers scripts that provide logging of web analytics data *@
            @Html.Kentico().WebAnalyticsLoggingScript()
      
      
      
        

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.

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 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 (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

      The methods provide a userAgent parameter representing the visitor’s 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.

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;
        }

    }
}