Module: Activities and contacts
2 of 9 Pages
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.
Let’s examine how to enable standard activity tracking in Xperience by Kentico and show or hide tracking scripts depending on the visitor’s cookie level.
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:
- Forms only collect data that legally qualifies as legitimate interest.
- Visitors who have not consented to the storage of their personal information are unable to submit forms.
This series includes an example of this.
Other activity types, however, require you to enable tracking during the application’s initialization.
- Landing page - logs the first page a visitor opens when viewing the website, typically the destination of a link clicked by the visitor.
- 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.
Create a method to check the cookie level
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 series, so let’s create a reusable method for it.
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.
Use dependency injection to resolve an
ICurrentCookieLevelProvider
object, and use it to retrieve the current cookie level.If the cookie level is greater than or equal to 1000, return
true
.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 greater than or equal to 1000, false otherwise</returns> public bool CurrentContactCanBeTracked() => cookieLevelProvider.GetCurrentCookieLevel() >= 1000; ...
Alternatively, if you’ve completed the data protection series, you can check the value of the more granulartrainingguides.cookieconsentlevel
cookie. You can access its name in code through theCookieNames.COOKIE_CONSENT_LEVEL
constant and compare its value to those in theCookieConsentLevel
enumeration.Add a corresponding signature to the ICookieConsentService interface.
C#ICookieConsentService.cspublic 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.
Create the following folder structure in the TrainingGuides.Web project: ~/Features/Activities/ViewComponents/Shared
Add a new file called ContactTrackingAllowedViewModel.cs in the Shared folder.
Over the course of this series, the view model will be shared across multiple view components.
Define a boolean property to indicate whether or not tracking is allowed.
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 to determine whether the tracking scripts should be rendered.
- Add a new folder called TrackingScripts to the TrainingGuides.Web/Features/Activities/ViewComponents directory.
- Create a new file called TrackingScriptsViewComponent.cs.
- Use constructor injection to attain an
ICookieConsentservice
object. - In the Invoke method, define a new
ContactTrackingAllowedViewModel
that usesCurrentContactCanBeTracked
method created in the previous step to set itsContactTrackingAllowed
property. - Return a view, providing the path to a file you will create in the next section and the view model.
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.
- Define a file in the TrackingScriptsfolder called TrackingScripts.cshtml.
- Set the view’s model to
ContactTrackingAllowedViewModel
. - Within an if statement that check’s the model’s
ContactTrackingAllowed
property, use Xperience’sActivityLoggingScriptV2
extension method to render tracking scripts to the page’s HTML.
@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.
Ensure that the TrainingGuides.Webnamespace is included among the tag helpers in your _ViewImports.cshtml file.
cshtml_ViewImports.cshtml... @addTagHelper *, TrainingGuides.Web ...
Open ~/Views/Shared/_Layout.cshtml and add a
@using
directive forTrainingGuides.Web.Features.Activities.ViewComponents.TrackingScripts
.cshtml_Layout.cshtml... @using TrainingGuides.Web.Features.Activities.ViewComponents.TrackingScripts ...
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.