How to exclude localhost session recordings on Microsoft Clarity?
Asked Answered
R

3

6

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?

Restoration answered 7/4, 2022 at 15:1 Comment(0)
R
11

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)

Rhaetic answered 29/9, 2022 at 9:31 Comment(1)
What's the second if statement inside the function for?Isometry
C
2

To exclude localhost on Clarity, you can use a Segment.

Microsoft Clarity filter button

First click Filter, then look for the path section Create a path match using a regular expression (regex). You can use a Regex with a negative lookup.

Adding a negative regex lookup

After this you can save this filter as a segment for future use

Here is the regex

^(?!.*localhost).*

Explanation:

^ Asserts the start of the line.

(?!.*localhost) is a negative lookahead that asserts that what immediately follows the current position in the string isn't localhost. If localhost is found, the regex will fail.

.* matches any character (except for a newline) 0 or more times.

This will match any string that does not contain the word "localhost".

Curson answered 27/7, 2023 at 14:1 Comment(1)
Maybe they've expanded and enhanced Filters recently -I was able to easily exclude localhost with the Filter setting Path | URL | Visited URL | excludes | localhostPooley
R
0

I have since switched to using Google Tag Manager for managing all the tags. GTM has an option to fire a tag based on hostname pattern match. I have set all tags to fire on production host only.

Restoration answered 3/10, 2022 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.