Kentico Xperience 13 documentation and ASP.NET Core

Most documentation about running Xperience applications under ASP.NET Core can be found in a dedicated section: Developing Xperience applications using ASP.NET Core. The rest of the documentation still applies, but some code samples and scenarios might need slight modifications for Core projects.

All major differences between the MVC 5 and Core platforms are summarized in Migrating to ASP.NET Core.

×

Logging web analytics based on consent

By default, web analytics log data for all visitors on the live website. Developers can customize the system to log web analytics only for visitors who give some form of consent.

The system's API provides the WebAnalyticsEvents.CheckAnalyticsConsent global event, which triggers before any type of web analytics are logged for visitors (before processing of visitor personal data). By assigning a handler to the event, developers can perform the following:

  • Define a condition according to the website's consent requirements. The condition can be based on consents managed within Xperience or on any other data available in the context of the request.
  • Set the HasConsent property of the handler's CheckAnalyticsConsentEventArgs parameter based on the result. If the property is false, the system does not process or log web analytics for the given request.

Example

The following example demonstrates how to disable logging of web analytics for all visitors (contacts) who have not accepted a consent with the WebAnalytics code name.

Xperience Enterprise required

The example uses the contact tracking and consent features to evaluate whether a visitor has given consent to log web analytics. As a result, it only works for sites with a Kentico Xperience Enterprise license.

  1. Open your live site project in Visual Studio.
  2. Create a new Class Library project in the solution.
  3. Add the Xperience API libraries to the project:
    1. Right-click the solution in the Solution Explorer and select Manage NuGet Packages for Solution.
    2. Select the Kentico.Xperience.Libraries package.
    3. Install the package into the project (the version must match your MVC project's Kentico.Xperience.AspNet.Mvc5 package and the exact hotfix version of your Xperience administration project).
  4. Edit the custom project's AssemblyInfo.cs file (in the Properties folder).

  5. Add the AssemblyDiscoverable assembly attribute:

    using CMS;
    
    [assembly:AssemblyDiscoverable]
  6. Create a custom module class within the project.
  7. Override the module's OnInit method and assign a handler method to the WebAnalyticsEvents.CheckAnalyticsConsent.Execute event.
  8. Perform the required actions within the handler method:
    • To disable logging of web analytics for a given request, set the HasConsent property of the handler's CheckAnalyticsConsentEventArgs parameter to false.
    • You can evaluate whether to log web analytics based on any custom condition. Access data from request contexts or use the ContactId property of the handler's CheckAnalyticsConsentEventArgs parameter to get the ID of the contact object for which the analytics are being logged.
using CMS;
using CMS.DataEngine;
using CMS.Core;
using CMS.DataProtection;
using CMS.ContactManagement;
using CMS.WebAnalytics;

// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomConsentModule))]

public class CustomConsentModule : Module
{
    // Module class constructor, the system registers the module under the name "CustomConsent"
    public CustomConsentModule()
        : base("CustomConsent")
    {
    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();

        // Assigns a handler to the WebAnalyticsEvents.CheckAnalyticsConsent.Execute event
        // This event occurs before the system logs web analytics for visitors and processes their personal data
        WebAnalyticsEvents.CheckAnalyticsConsent.Execute += Analytics_CheckConsent;
    }

    private void Analytics_CheckConsent(object sender, CheckAnalyticsConsentEventArgs e)
    {
        // Gets the contact for which the analytics are being logged
        // Note: The contact is null for visitors who have not given consent for contact tracking
        ContactInfo contact = ContactInfo.Provider.Get(e.ContactId);

        // Gets the consent that will be evaluated
        ConsentInfo consent = ConsentInfo.Provider.Get("WebAnalytics");

        // Disables web analytics logging for all visitors (contacts) who have not accepted the "WebAnalytics" consent declaration
        IConsentAgreementService consentService = Service.Resolve<IConsentAgreementService>();
        e.HasConsent = (contact != null && consent != null) && consentService.IsAgreed(contact, consent);
    }
}

Performance recommendations

The system triggers the CheckAnalyticsConsent event frequently when displaying pages to visitors (multiple times per web request in certain cases). Running demanding operations within the event's handler method may have a negative impact on your site's performance.

We strongly recommend caching the results of your consent conditions. To learn about the custom caching options provided by the Xperience API, see: Caching in custom code

Tip – Logging analytics based on tracking consent

For visitors who have not given consent for the tracking of contacts and activities (see Working with consents), the ContactId property of the handler's CheckAnalyticsConsentEventArgs parameter is 0.

You can use the tracking consent as your condition for logging of web analytics. In this case, you do not need to define or evaluate an additional "web analytics consent". You only need to create a condition that checks whether the ContactId value is greater than 0, i.e. that the web analytics are being logged for an existing contact.


Was this page helpful?