---
title: Logging web analytics based on consent
related:
  - https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/web-analytics.md
  - https://docs.kentico.com/13/configuring-xperience/data-protection/gdpr-compliance/working-with-consents.md
  - https://docs.kentico.com/13/custom-development/handling-global-events.md
  - https://docs.kentico.com/13/custom-development/handling-global-events/reference-global-system-events.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).

By default, [web analytics](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/web-analytics.md) 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](https://docs.kentico.com/13/custom-development/handling-global-events.md) 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](https://docs.kentico.com/13/configuring-xperience/data-protection/gdpr-compliance/creating-consents.md) 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.

> **Info:** **Xperience Enterprise required**
>
> The example uses the [contact tracking](https://docs.kentico.com/13/on-line-marketing-features/managing-your-on-line-marketing-features/contact-management/working-with-contacts.md) and [consent](https://docs.kentico.com/13/configuring-xperience/data-protection/gdpr-compliance/creating-consents.md) 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. [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, or re-use an existing assembly.
3. Create a [custom module class](https://docs.kentico.com/13/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md) within the project.
4. Override the module's **OnInit** method and assign a handler method to the **WebAnalyticsEvents.CheckAnalyticsConsent.Execute** event.
5. 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.

```csharp

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

```

> **Note:** **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](https://docs.kentico.com/13/configuring-xperience/configuring-caching/caching-in-custom-code.md)

> **Tip:** **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](https://docs.kentico.com/13/configuring-xperience/data-protection/gdpr-compliance/working-with-consents.md)), 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.
