Search code examples
ms-clarity

How to exclude localhost session recordings on Microsoft Clarity?


How to exclude development sessions from Clarity recordings? I couldn't find any options in their settings. Also, is it possible to restrict session recordings to certain countries?


Solution

  • The easiest way is to not include the script on localhost with a simple check on the current hostname:

    if (!window.location.host.includes('localhost')) {
     // code
    }
    

    Full snippet like this:

     <script type="text/javascript">
        if (!window.location.host.includes('localhost')) {
        (function (c, l, a, r, i, t, y) {
            if(window.location.host.includes('localhost'))
            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", "XXXXXXX");
        }
    </script>
    

    (Replace XXXXX with your key/code from clarity)