microsoft / clarity

A behavioral analytics library that uses dom mutations and user interactions to generate aggregated insights.
https://clarity.microsoft.com
MIT License
2.09k stars 208 forks source link

Restrict Usage of Clarity in California #557

Closed wingyang-lee closed 5 months ago

wingyang-lee commented 6 months ago

Hi Microsoft Clarity team, Is it possible to disable the tracking in California (limit the usage of Clarity to specific regions)?

ChrisSchaller commented 5 months ago

You can handle this at the application level, to flesh this out to a feature request will require a more detailed use case. This type of requirement overlaps other issues like DNT (Do-Not-Track) settings that also must currently be handled at the application level.

So after a user opts out of tracking, or your application logic determines they should not be tracked, you can call these functions:

window.clarity('stop');
window.clarity('consent', false)

This is discussed in #75 and documented at Clarity - Cookie Consent

You can conditionally enable/disable tracking when you install the script, this example reads the browser's DNT setting, but you could put in your own location settings:

<script type="text/javascript">

var dnt = window.doNotTrack ?? navigator.doNotTrack;
if (dnt === 1)
    console.log('Clarity tracking disabled due to DNT');
else
{
    (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_KEY_HERE");
}

</script>
AbdelrhmanMagdy commented 5 months ago

At this moment, Microsoft Clarity does not provide direct support for limiting usage to specific regions via the portal. However, you have the option to manage this on your website by either preventing the Clarity script from loading in those regions altogether, or by dynamically stopping Clarity tracking during runtime, as indicated in this issue.