1

Some days ago I introduced the new gtag version of Google Analytics to my private website. Now I try to figure how to filter out my own traffic. Exclusion based on IP is not possible because I enter my website with different browsers from different places. So I wanted to exclude my traffic via cookie. I'm just not able to make it work. The documentation is telling I should work now with dimensions. I tried it but it is not working for me.

I set up a dimension "usertype" google analytics dimension

and I added a filter to exclude pattern "internal" for dimension "usertype" google analytics filter

I created a new page for my website and put the following code

<html>
<head>
    <!-- Global Site Tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-XX"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments)};
        gtag('js', new Date());

        gtag('config', 'UA-XXXXXXX-XX', {'custom_map': {'dimension1': 'usertype'}});

        gtag('event', 'kill_ga', {'usertype': 'internal'});
    </script>
    <!-- Google Analytics -->

    <meta http-equiv="refresh" content="5; url=/" />
</head>
<body>
We'll transfer you soon
</body>
</html>

As I said before. I'd like to somehow mark my own traffic as internal and exclude it via filter. Could someone help me to achieve this please?

dj_thossi
  • 774
  • 1
  • 9
  • 18

2 Answers2

2

There are many approaches to blocking your own traffic, I think using the usertype is one approach. Historically, I've solved it with a simple cookie check:

You'd ask any developer/tester to create a cookie on your site by 1) visiting your site, then 2) entering something like this into their browser's JS console:

Set the persistent cookie, "prevent_ga" equal to true

document.cookie = "prevent_ga=1"

Then check the cookie before invoking GA on your site:

var check_cookie = document.cookie.match(/^(.*;)?\s*prevent_ga\s*=\s*[^;]+(.*)?$/)  
if (!check_cookie) {  
    // do gtag() things   
}  
Pat Grady
  • 307
  • 1
  • 2
  • 12
0

I used it finally in a completely different approach.

As you can see on https://www.smest.it I solved with JavaScript directly on my page.

In footer of any page:

<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script>
    var gaProperty = 'XXXXXXX-XX';
    var disableStr = 'ga-disable-' + gaProperty;

    if (document.cookie.indexOf(disableStr + '=true') > -1) {
        window[disableStr] = true;
    }

    function gaOptout() {
        document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
        window[disableStr] = true;
    }
</script>

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-XX"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){
        dataLayer.push(arguments)
    }

    gtag('js', new Date());
    gtag('config', 'UA-XXXXXXX-XX', { 'anonymize_ip': true });
</script>

In my privacy policies, I have the following link.

<a onclick="alert('Google Analytics is now deactivated');" href="javascript:gaOptout()">Deactivate Google Analytics</a>

This will turn off analytics completely.

dj_thossi
  • 774
  • 1
  • 9
  • 18