Author

Analytics & GTM Expert

UX-SEO Advocate

Follow me on
Category | Google Analytics
Difficulty |

UPDATE (July 2, 2019): The new script will set the "InternalTraffic" cookie for the top-level domain and all subdomains as well.

Exclude your and your team's traffic no matter where you are or what device you use

Excluding internal/employee traffic, is crucial for the accuracy of your Google Analytics. In this guide, I'll show you how to do it in a more dynamic way than filtering static IPs which in most cases is not enough.

When is this solution useful?

I recommend using this method of filtering internal traffic for any Google Analytics property, but it is especially useful when:

  • You or your team work from multiple locations (office, home office, on-site, etc.),
  • You or your team are constantly on the move or work from different places (hotels, airports, restaurants,etc.),
  • You or your team access your site on mobile devices using mobile data (3g, 4g/LTE, 5g, ...),
  • You or your team use VPN services to access the site from different countries,
  • If your Google Analytics is using IP Anonymization (anonymizeIp) required by the GDPR,
  • Your Internet Service Provider (ISP) changes your IP constantly, which means you have a dynamic IP.

What is a dynamic IP and why it sucks for Analytics?

A dynamic IP address is an IP that is assigned automatically to any of your devices (smartphone, laptop, etc) and is constantly changing. The is done by your ISP and in most cases, it can't be controlled by the user.

The main problem with a dynamic IP is that it is a nightmare to filter out from Analytics tools using standard methods, which causes data to get cluttered with irrelevant traffic produced by the team working on the site.

The best way to determine if you have a dynamic IP is to monitor your public IP for a few weeks. You can easily check your current IP by searching "my IP" in Google.

QUICK TIP: if you see the long IP version (IPv6) it's most likely that you have a dynamic IP.

  • IPv4: 123.45.67.89
  • IPv6: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Requirements

To implement the following solution you will need:

  • GTM container + GA pageview tag: Google Tag Manager account and a basic tag that fires your Google Analytics code
    If you don't have this yet,  follow this tutorial to add it manually to your site or if you use WordPress you can use this plugin.
  • [Optional] Add a test view to your Analytics - If you are not comfortable working with filters in GA yet I recommend you to create a test view first and if you don't have one yet, also create an unfiltered view. These 2 extra views will help you data loss in case you have a misconfigured filter.

Filter to exclude dynamic IPs using GTM and a cookie

Once you have the GTM account and the basic GA tag in place you can continue with the guide.

If you are not familiar with Javascript, cookies, and GTM, the process might look a bit complex so I'll divide the instructions into 5 Steps, so you don't get lost:

  • STEP 1: Set the "Internal Traffic" cookie (2 options):
    • Using a bookmarklet
    • Using GTM
  • STEP 2: Create a Custom Dimension in GA
  • STEP 3: Assign the cookie value to the Custom Dimension in GTM
  • STEP 4: Create the filter for dynamic traffic in GA
  • STEP 5: Test your filter

To avoid problems, use the exact same names recommended on this guide, including spaces. I'll mark the key names in orange.

STEP 1: Setting the Cookie

 

The options are not exclusive, you can use both them for different situations:

  • A) bookmarklet: One-click cookie insertion. I usually use this for myself and people close to the development area, 
  • B) GTM: This method is great for sharing to the rest of the team.

Note that the cookie will be set for the top-level domain and all subdomains so you don't need to repeat the process.

A) Setting a Cookie with a Bookmarklet

STEP 1A

Bookmarklets are extremely helpful mini-applications that provide additional functionalities to your browser right from your favorites bar. I used it all the time to simplify some common tasks.

In this case, I created this bookmarklet to insert the "Internal Traffic Cookie" with just one click.

  • Just drag this button/bookmarklet into your favorites bar +InternalCookie
  • Here is the javascript snippet that the bookmarklet contains:
    javascript:function setCookie(cname, cvalue, exdays, cdomain) { var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = 'expires='+ d.toUTCString(); document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/ ; domain=" + cdomain; } setCookie('InternalTraffic', 'true', 365, window.location.hostname); alert('Cookie: InternalTraffic\nValue: TRUE\nDomain: ' + window.location.hostname)

The second option.

B) Setting the Cookie with GTM

STEP 1B

Now you have to move to Google Tag Manager

  1. Go to the GTM container you are using for your site.
  2. Click on New Tag and call it Set Internal Traffic Cookie
  3. Click on Tag Configuration, and select <> Custom HTML on the right panel 
  4. On the HTML box, copy the following script. This script will set the cookie that you will use later for the filter. Make sure to change the text in red for your top level domain, for example, for my site that would be carloseo.com.
    <script>
    function setCookie(cname, cvalue, exdays, cdomain) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/ ; domain=" + cdomain;
    }
    setCookie("InternalTraffic", "true", 365, "[your-top-level-domain]");
    </script>
  5. In the section below click on Triggering, click on the blue plus sign at the top right corner +
  6. Name the trigger as PV - Internal Traffic, click on Trigger Configuration,and select  Page View.
  7. Set the trigger to fire on Some Page Views and set the condition like this
    • 1st Dropdown select Page URL
      • Note: If you don't see Page URL on the list, on the left panel got to variables, click on the red button configure and enable it.
    • 2nd Dropdown select matches RegEX
    • Text Box enter \/internal\/$|\?internal$
  8. Save the trigger and then Save the tag.

Inserting the Internal Traffic Cookie into the browser.

  • a) Bookmarklet: Click the bookmarklet while you are on your site.
  • b) GTM: Open any URL of your site with the text ?internal at the end -> example.com/?internal

You need to do this:

  • In all the browsers you use to work on your site (mobile and desktop).
  • Whenever you clear the cookies on your browser.

STEP 2: Create a Custom Dimension in Google Analytics

STEP 2

Custom dimensions are a hefty feature to add additional information in Google Analytics. In this case, we will use it to determine if your visit is local or is an external user. To create it:

  1. Go to the admin section and on the PROPERTY column select Custom Definitions > Custom Dimensions. 
  2. Next click on the red button +New Custom Dimension   
  3. Configure the dimension as follows
    • Name: InternalTraffic (You can put here any name, the index of the dimension is the important part)
    • Scope: User
    • Check the active box Create custom dimension
  4. Click Create and in the next screen click Done. Back in the list screen, you will see your new custom dimension and next to it the Index, remember this number because you will need it later.

STEP 3: Set Value for the Custom Dimension "Internal Traffic."

STEP 3
  1. In the GTM main screen click on Variables, under User-Defined Variables click NEW.
  2. Name your Variable Internal Traffic Cookie and click on Variable Configuration 
  3. Select 1st Party Cookie from the list on the right, enter InternalTraffic as Cookie Name and SaveCreate Dynamic Internal Traffic Variable GTM
  4. Now open the tag that inserts the Analytics tracking code on your site.
  5. Under the tag, configuration click on More Settings and expand Custom Dimension
    Note: if you don't see the More Settings option check the override settings box. 
  6. Click +ADD CUSTOM DIMENSION, on Index enter the number of your custom dimension (the number I told you to remember :) and on Dimension Value enter {{Internal Traffic Cookie}} and save the tag.
  7. Lastly, click on PUBLISH in the top right corner of the main GTM window.Publish the GTM container

Note: If you want to test your tag before submitting the changes you can use the preview feature.

STEP 4: Create the Internal Traffic Filter for Dynamic IPs in GA

STEP 4

The last part of this solution is to create the filter in Analytics. Before setting the filter to your master view, I recommend you to try it first on your test view.

  1. Again go to the Admin Section of your Analytics, click on Filters under the View Column.
    Note: Don't use the option "All Filters" under the Account Column because you won't see the custom dimension there.
  2. Click the red button +NEW FILTER and enter "Exclude Internal Traffic (Dynamic IP)."
  3. Select filter type Custom and configure it as follows:
    • Filter Field search for Internal Traffic (This is the custom dimension you created before)
    • Filter Pattern enter true. Filter for dynamic IPs in Google Analytics

STEP 5: Activate and Test Your Filter

STEP 5

This should be done for every browser you use to work on the site mobile or desktop.

  1. Activate the filter either with the bookmarklet or by adding \?internal at the end of the URL of your site (example.com/?internal)
  2. Open the real-time report of the view where you applied the filter. You will see this in your report 
  3. Then go to any other page of your site (preferably one that is not commonly visited).
  4. If you don't see the next pages in the real-time report, then your filter is working! Congratulations :)

Next time you access the site from that browser your visit won't be counted in Analytics.

The cookie is set to last one year so unless you clear your cookies, you don't have to trigger it again.

Troubleshooting your filter

If you haven't work with cookies and GTM before chances are that you may miss something and the filter won't work.

The first thing to check is that the cookie was added to your browser:

  1. On Chrome.
  2. At the top right, click the three dotsMore and then Settings.
  3. Search for Content settings and click on it.
  4. Click Cookies at the top and then click on See all cookies and site data
  5. On the search box (the one for cookies, not the main one) enter your site name and click on it. (If you don't see it then there are no cookies for your site).
  6. Then find on the list InternalTraffic and expand it on the section content it should say true

If you don't see the cookie go back to the step 1.

In this page, you can see how to check cookies on other browsers.

If your cookie is in place and is set to true and the filter doesn't work, check the setting in this order.

  • Make sure your filter in GA is correctly configured, Exclude > Field: Internal traffic > Filter pattern: true (step 4)
  • Make sure you are adding the custom dimension with the right index in your GA Pageview tag. (step 3)

This should cover the most common issues.

Let me know in the comments what do you think of this solution?

Be the first to comment :)