Enable activity tracking

Businesses like to know what visitors are doing on their sites. Data about how visitors browse their site tells marketers which features are most vital, and lets them personalize content and marketing for specific contacts.

This guide shows you how to enable standard activity tracking in Xperience by Kentico and show or hide tracking scripts depending on the visitor’s cookie level.

Activity tracking series

This guide is the start of a series on Activity tracking.

This is the first guide in the series, which will also cover multiple methods of logging custom activities, preventing activities from being logged in certain cases, and logging activities from external sites which are not managed through Xperience.

Activity tracking functionality ties in with consents, which are covered in detail in the Data protection series. We recommend starting there to understand the components of a full-fledged marketing solution in Xperience.

Before you start

This guide requires the following:

The examples in this guide require that you:

Code samples

You can find a project with completed, working versions of code samples from this guide and others in the finished branch of the Training guides repository.

The main branch of the repository provides a starting point to code along with the guides.

The code samples in this guide are for .NET 8 only.

They come from a project that uses implicit using directives. You may need to add additional using directives to your code if your project does not use this feature.

Enable tracking

Xperience’s activity tracking features help you monitor the actions of visitors on your site.

  • Visitors correspond to ContactInfo objects, which represent rows in the OM_Contact table of the database.
  • The actions visitors take on the site correspond to ActivityInfo objects, which represent rows in the OM_Activity table.

Xperience tracks form submit activities automatically. If no contact exists when a visitor submits a form, the system will create a new contact associated with their submission.

Legitimate interest

It is important to ensure that one of the following conditions is met:

  1. Forms only collect data that legally qualifies as legitimate interest.
  2. Visitors who have not consented to tracking are unable to submit forms.
    This guide series includes an example of this.

Other activity types, however, require you to enable tracking during the application’s initialization.

  1. Landing page - logs the first page a visitor opens when viewing the website, typically the destination of a link clicked by the visitor.
  2. Page visit - logs a page the visitor has viewed while traversing the website.

To track these types of activities, follow the steps outlined on the Set up activities documentation page.

Once you enable activity tracking, Xperience will log the Landing page and Page visit activities for anyone who visits pages where the tracking script is present.

Add a view component

To avoid legal issues related to tracking consent, you must modify the layout view to render Xperience’s tracking script only when the contact has consented to tracking.

To avoid embedding business logic in the view, you can create a method that determines whether the current contact has consented to tracking, and use it in a view component that conditionally renders the script.

Tracking consent

This section assumes that you have completed the training guides in the Data protection section. If not, make sure consents have been configured to set the CMSCookieLevel to All (1000) or higher only when the visitor has consented to marketing tracking.

The view component needs to check whether or not the current contact can be tracked. However, this functionality will be used in more places than just the view component, over the course of this guide series, so let’s create a reusable method for it.

  1. In the TrainingGuides.Web/Features/DataProtection/Services/CookieConsentService.cs class created in the Data protection training guides, add a new boolean method called CurrentContactCanBeTracked.

    If you have not completed the data protection series, you can create a new service and register it with dependency injection.

  2. Try to parse an integer value from the CMSCookieLevel cookie.

  3. If the integer exists and is greater than or equal to 1000, return true.

  4. Otherwise, return false.

    C#
    CookieConsentService.cs
    
    
     ...
     /// <summary>
     /// Checks if the current contact's CMSCookieLevel is All (1000) or higher
     /// </summary>
     /// <returns>True if CMSCookieLevel is greather than or equal to 1000, false otherwise</returns>
     public bool CurrentContactCanBeTracked()
     {
         bool isAllOrHigher = false;
         string cookieLevelString = cookieAccessor.Get("CMSCookieLevel");
    
         if (int.TryParse(cookieLevelString, out int cookieLevel))
             isAllOrHigher = cookieLevel >= 1000;
    
         return isAllOrHigher;
     }
     ...
    
     
    Alternatively, if you’ve completed the data protection series, you can check the value of the more granular trainingguides.cookieconsentlevel cookie. You can access its name in code through the CookieNames.COOKIE_CONSENT_LEVEL constant and compare its value to those in the CookieConsentLevel enumeration.
  5. Add a corresponding signature to the ICookieConsentService interface.

    C#
    ICookieConsentService.cs
    
    
     public interface ICookieConsentService
     {
         ...
         bool CurrentContactCanBeTracked();
         ...
     }
    
     

Add a view model

The view component that conditionally renders the tracking script will be simple— it only needs to know if the script tags should be written to the page’s markup or not.

In theory, a simple boolean could be used as the model for its view. However, to make the code more readable and allow greater flexibility, create a view model with a boolean property.

Xperience’s ActivityLoggingScriptV2 extension takes optional parameters that could correspond to view model properties, if your scenario requires more flexibility.

  1. Create the following folder structure in the TrainingGuides.Web project: ~/Features/Activities/ViewComponents/Shared

  2. Add a new file called ContactTrackingAllowedViewModel.cs in the Shared folder.

    Over the course of this guide series, the view model will be shared across multiple view components.

  3. Define a boolean property to indicate whether or not tracking is allowed.

C#
ContactTrackingAllowedViewModel.cs

namespace TrainingGuides.Web.Features.Activities.ViewComponents.Shared;

public class ContactTrackingAllowedViewModel
{
    public bool ContactTrackingAllowed { get; set; } = false;
}

Define the view component

Now define the view component class that will use the CurrentContactCanBeTracked method from earlier in this guide to determine whether the tracking scripts should be rendered.

  1. Add a new folder called TrackingScripts to the TrainingGuides.Web/Features/Activities/ViewComponents directory.
  2. Create a new file called TrackingScriptsViewComponent.cs.
  3. Use constructor injection to attain an ICookieConsentservice object.
  4. In the Invoke method, define a new ContactTrackingAllowedViewModel  that uses CurrentContactCanBeTracked method created in the previous step to set its ContactTrackingAllowed property.
  5. Return a view, providing the path to a file you will create in the next section and the view model.
C#
TrackingScriptsViewComponent.cs


using Microsoft.AspNetCore.Mvc;
using TrainingGuides.Web.Features.Activities.ViewComponents.Shared;
using TrainingGuides.Web.Features.DataProtection.Services;

namespace TrainingGuides.Web.Features.Activities.ViewComponents.TrackingScripts;

public class TrackingScriptsViewComponent : ViewComponent
{
    private readonly ICookieConsentService cookieConsentService;

    public TrackingScriptsViewComponent(ICookieConsentService cookieConsentService)
    {
        this.cookieConsentService = cookieConsentService;
    }

    public IViewComponentResult Invoke()
    {
        var model = new ContactTrackingAllowedViewModel()
        {
            ContactTrackingAllowed = cookieConsentService.CurrentContactCanBeTracked()
        };

        return View("~/Features/Activities/ViewComponents/TrackingScripts/TrackingScripts.cshtml", model);
    }
}

Implement a view

With the view component’s Invoke method handling the majority of the work, the view model must simply react to the provided model.

  1. Define a file in the TrackingScriptsfolder called TrackingScripts.cshtml.
  2. Set the view’s model to ContactTrackingAllowedViewModel.
  3. Within an if statement that check’s the model’s ContactTrackingAllowed property, use Xperience’s ActivityLoggingScriptV2 extension method to render tracking scripts to the page’s HTML.
cshtml
TrackingScripts.cshtml


@using TrainingGuides.Web.Features.Activities.ViewComponents.Shared
@using Kentico.Activities.Web.Mvc

@model ContactTrackingAllowedViewModel

@if (Model.ContactTrackingAllowed)
{
    @Html.Kentico().ActivityLoggingScriptV2();
}

Add the tracking script to Layout view

This registered service allows you to evaluate whether the current visitor has consented to tracking in the layout view. You can use it to render the tracking script accordingly.

  1. Ensure that the TrainingGuides.Webnamespace is included among the tag helpers in your _ViewImports.cshtml file.

    cshtml
    _ViewImports.cshtml
    
    
     ...
     @addTagHelper *, TrainingGuides.Web
     ...
    
     
  2. Open ~/Views/Shared/_Layout.cshtml and add a @using directive for TrainingGuides.Web.Features.Activities.ViewComponents.TrackingScripts.

    cshtml
    _Layout.cshtml
    
    
     ...
     @using TrainingGuides.Web.Features.Activities.ViewComponents.TrackingScripts
     ...
    
     
  3. Include TrackingScriptsViewComponent at the end of the header with the tag helper <vc:tracking-scripts/> .

    cshtml
    _Layout.cshtml
    
    
     ...     
         <vc:tracking-scripts/>
     </head>
     ...
    
     

Thanks to the logic in the view component’s Invoke method, Landing page and Page visit activities will not be logged for visitors who have not consented to tracking.

What’s next?

Xperience now tracks standard activities, and the next guide in this series will expand this to include custom activities. It will go over creating custom activity types, and logging them with both client-side and server-side approaches.