When using Microsoft Clarity in a region where users need to opt in to placing cookies in their browser, you must call the Microsoft Clarity consent API to indicate that the user has given permission to be tracked using cookies.
In order to do this, you can pass the consent state from Termly CMP to Microsoft Clarity in order to ensure proper compliance and functioning of Clarity.
The example below shows how you can send the user's consent state on any page to Clarity.
Usage
<script> function onTermlyLoaded() { Termly.on("consent", (data) => { console.log(data); if (data.consentState && data.consentState.analytics) { window.clarity("consent"); } else { console.log("Clarity tracking disabled due to missing consent."); } }); } </script> <script type="text/javascript" src="https://app.termly.io/resource-blocker/{WEBSITEUUID}?autoBlock=on" onload="onTermlyLoaded()" ></script>
The function inside the first <script> tag executes once the Termly Consent Banner script has loaded. It listens for the "consent" event and checks whether the user has granted permission for analytics cookies before enabling Microsoft Clarity tracking.
- If analytics cookies are accepted, window.clarity("consent") is called.
- If analytics cookies are denied, Clarity tracking is not activated.
You can modify this logic by inspecting the data object returned by the function to check for other consent categories before allowing Clarity tracking.
Related Resources
Getting Consent State and Handling Consent Changes with Termly