Developer forum

Forum » Swift » How to Implement Microsoft Clarity Consent Integration in Swift-v2

How to Implement Microsoft Clarity Consent Integration in Swift-v2

Ferri Halfhide
Reply

Hi Team,

We’ve set up Microsoft Clarity for our client to analyze website behavior through heatmaps and session recordings.

We recently received an email from Microsoft Clarity regarding consent requirements, which states the following:

“Starting October 31st, you are required to share user consent signals for sessions originating from the EEA, UK, and Switzerland. If consent is not obtained and signaled using one of the supported methods, certain Clarity features will be impacted.”

To comply with this, I understand that we need to add custom code in our templates to ensure the correct consent signals are sent to Clarity.

I noticed that a similar implementation exists for Google Analytics, specifically in the Google Tag Manager section of the Swift-v2 master template:
https://github.com/dynamicweb/Swift/blob/main/Files/Templates/Designs/Swift-v2/Swift-v2_Master.cshtml#L118-L149

Now I’m wondering:

  • How should we implement this for Microsoft Clarity?

  • In which template(s) should this integration be added?

  • Is there an example or best practice for Swift-v2?

Thanks in advance for your help!


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Ferri

Here is some documentation for Clarity: https://www.youtube.com/watch?v=43U87jaDTLk

Consent mode in Clarity: https://learn.microsoft.com/en-us/clarity/setup-and-installation/consent-mode - this outlines how to signal that consent has been given.

You’re correct that Microsoft Clarity now requires explicit consent signals for users in the EEA, UK, and Switzerland. In the Swift v2 setup, this should be handled similarly to how Google Tag Manager and Consent Mode V2 are already implemented.

1. Where and how to implement

The Swift v2 cookie management logic lives in
/Files/Templates/CookieWarning/Cookies.cshtml.
This template is responsible for the full consent workflow: banner, modal, and consent category management. It already contains JavaScript functions such as setOptInCookie(), acceptCustomSetup(), and helper methods like consentGrantedAnalyticsStorage() and consentGrantedMarketing() that fire Google consent updates through gtag('consent', 'update', …) when a user accepts cookies Files/Templates/CookieWarning/C…

To integrate Microsoft Clarity, you can use the same pattern by adding a Clarity initialization call inside the appropriate consent handler:

function consentGrantedAnalyticsStorage() { if (!window.dataLayer) return; gtag('consent', 'update', { 'analytics_storage': 'granted' }); if (typeof clarity === 'function') clarity('consent'); } 

or, if Clarity is loaded through its snippet:

if (typeof clarity === 'function') { clarity('consent', true); // signal granted consent } 

This ensures that the Clarity tag only activates when consent for analytics (or marketing) is granted.

2. Which template(s) to modify

  • Primary file:
    /Files/Templates/CookieWarning/Cookies.cshtml – this is where the user’s opt-in level and consent logic are handled.

  • Optional inclusion point:
    If your setup uses Swift-v2_Master.cshtml for analytics scripts (as it does for Google Tag Manager), ensure your Clarity script snippet is wrapped in a consent-check condition there too.
    Example:

     
    @if (CookieOptInHelper.HasAnalyticsConsent()) { <script type="text/javascript"> (function(c,l,a,r,i,t,y){ c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i; y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); })(window, document, "clarity", "script", "YOUR_CLARITY_ID"); </script> }

3. Best-practice pattern for Swift v2

  • Use the existing cookie categories (“Statistical”, “Marketing”) to determine when Clarity is allowed.

  • Keep the logic centralized in Cookies.cshtml for maintainability.

  • Signal Clarity consent inside consentGrantedAnalyticsStorage() (for heatmaps and session recordings).

  • Optionally, use Dynamicweb’s CookieOptInHelper if you want server-side template control in master pages.

Votes for this answer: 1
 
Mark Hoogkamer
Reply

Hi Nicolai,
I've implemented this and it works like a charm. Thanks so much for your extensive feedback!

 

You must be logged in to post in the forum