Integrating the Microsoft Clarity Consent API

Alexandre Dias Da Silva
Alexandre Dias Da Silva
  • Updated

Microsoft Clarity is a powerful tool for analyzing user behavior. However, due to regulatory developments, it is crucial to adapt its implementation to ensure compliance.

In December 2024, Microsoft shared an important update regarding Clarity. By early 2025, websites using Clarity in the EEA, the UK, and Switzerland will need to obtain explicit user consent before setting cookies.

By following the procedure below, you will:

  • Configure Clarity to avoid collecting user data by default.
  • Add additional code to control Clarity tracking based on user consent choices made via your cookie banner.

Step 0: Prerequisites

Before proceeding, ensure that Microsoft Clarity is included in your Axeptio cookie banner.

If not, refer to our guide here to fix this: How to add a new Cookie in my tags list ?

Step 1: Disable cookies in Clarity

By default, Clarity tracks visitors using cookies.

To disable cookies, go to Settings > Setup > Advanced settings, then uncheck Cookies.

Clarity cookie setting.

Step 2: Add the code to your site

Our sample code below links the choices made by your visitors in your Axeptio cookie banner to the Clarity consent API:

<script>
  void 0 === window._axcb && (window._axcb = []); // Initialize Axeptio
  window._axcb.push(function (axeptio) {          // Get cookie consent choices
    axeptio.on("cookies:complete", function (choices) { // Triggered after user choices
      if (choices.clarity) {                      // If Clarity is accepted
        window.clarity('consent');               // Start Clarity tracking
      } else {                                   // Else
        window.clarity('consent', false);        // Disable tracking
      }
    });
  });
</script>

You can add this code in several ways, such as:

  • Directly, in the <head> of your site.
  • Through Google Tag Manager, in a custom HTML tag: use the “Consent Initialization - All Pages” trigger to ensure this script runs before any other services.

Step 3: Test

Follow the steps below to confirm that Clarity does not set cookies on your visitors’ browsers:

1. Clear cookies on your website.

2. Visit your website.

3. Reject the cookies through your Axeptio cookie banner.

4. Ensure that Clarity has been loaded (check for /collect calls).

5. Check the cookies in your browser and confirm that there are no entries for _clck or _clsk.


By following these steps, your site will comply with the new regulatory requirements while continuing to take advantage of Microsoft Clarity’s advanced features.

For more information, refer to Microsoft Clarity’s official documentation:

 

Was this article helpful?

1 out of 1 found this helpful