Author

Analytics & GTM Developer

Optimizer Troubleshooter

Follow me on
Category | Google Analytics
Difficulty |

How to filter employee traffic working remotely in Google Analytics

  • UPDATE (April 20, 2020): Guide restructured and updated screenshots
  • UPDATE (July 2, 2019): The new script will set the "InternalTraffic" cookie for the top-level domain and all subdomains as well.

Internal/employee traffic is one of the most common things that can skew your data if it is not filtered properly. The common way of preventing this is to create static IP filters, however, this solution is not efficient when you have to exclude multiple locations and/or your IP address changes so often that it is hard to keep updating filters.

In this guide, I'll show you a more dynamic way of filtering employee traffic, whether they are working in the office, home, or friend's house, etc, or the device they are using, phone, PC, tablet, etc

When is this solution useful?

If you have a relatively small team using the static IP filters should be enough. However, if you check one or more of the following scenarios the dynamic filtering solution that I'll share below will be much more efficient.

a. You have to filter multiple IPs or locations

Some scenarios:

  • Employees working from home (home office) or remotely,
  • Employees working from multiple locations (different offices, on-client-site, etc.),
  • Employees are constantly traveling (hotels, airports, restaurants,etc.),
  • If your Google Analytics is using IP Anonymization (anonymizeIp) required by the GDPR and other privacy laws.

b. Your IP changes constantly or is a dynamic IP

Some scenarios:

  • You or your team access the website on mobile devices often using mobile data (3g, 4g/LTE, 5g, ...),
  • You or your team use VPN services to access the website from different countries,
  • Your Internet Service Provider (ISP) changes your IP constantly, which means you have a dynamic IP.

What is a dynamic IP and why is bad for Analytics?

A dynamic IP address is an IP that changes constantly and it is assigned automatically by your ISP. This is often the case when working from home because static IPs are usually reserved for offices or companies.

So if you are working from home the question is not whether you have a dynamic IP but how often it changes. Some take a month or more, but in many cases, the IP changes almost every other day.

The best way to determine how often your IP changes is to monitor your public IP by searching "my IP" in Google.

Or using a tracking app for IPs:

Here is how my home IP has changed in the last months:

IP changes constantly or Dynamic IP in Google Analytics

QUICK TIP: I have noticed that the long IP version (IPv6) changes more often than the traditional IPv4.

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

Prerequisites

To implement this following solution you will need:

  1. A GTM container: If you don't have this yet, follow this tutorial to add it manually to your site.  Or if you prefer you can use a plugin:
  2. A Google Analytics Pageview tag: This is the tag that fires your GA code into your site, if you don't have one yet here is a quick video on how to create this tag. After creating this tag you should remove the GA tracking code (universal or gtag) from your website.

    IMPORTANT: If your GA intallation has ecommerce, event tracking, or other customizations you will need to check with the developer that configured them before removing your GA code.

  3. [Optional] Add a test view to your Analytics - If you are not comfortable working with filters in GA and GTM yet I recommend you create a test view first to test this solution first and avoid losing data.

Filter to exclude dynamic IPs using GTM and a cookie

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

These options are not exclusive, you can use both of 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 better for employees that are less technical.

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

a) Inserting the 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)

    Basically this code inserts a little cookie with the value InternalTraffic=true and sets it for 365 days.

b) Inserting the Cookie with GTM

STEP 1B
  1. Go to your GTM container,
  2. Click on New Tag and call it HTML - Set InternalTraffic cookie
  3. Click on Tag Configuration, and from the right panel select the Tag Type <> Custom HTML .
  4. In the HTML box, copy the script below. This is a simple script that will set the cookie that you will use later for the filter.

    Important! Make sure to change the text your_top_level_domain for your top domain (withouth http:// and www) for example, for this 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>
    GTM - Set internal traffic cookie for filter
  5. On the section below click on Triggering and then click on the blue plus sign at the top right corner +
  6. Name the trigger as PV - InternalTraffic, click on Trigger Configuration and select  Page View from the right panel
  7. Configure the trigger as follows and set the condition like this
    • 7.1 This trigger fires on Some Page Views
    • 7.2 First dropdown select Page URL
      • Note: If you don't see Page URL on the list, click Choose Built-In Variable at the bottom of the dropdown, and from the list that appears click on Page URL to activate it.
    • 7.3 Second dropdown select matches RegEX
    • 7.4 Text Box enter \/internal\/$|\?internal$ Internal Traffic cookie Trigger for GTM
  8. Click save the trigger and again click on save for the tag.
 

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 an internal or an external user.

To create a custom dimension:

  1. Go to the admin section of your GA and on the PROPERTY settings (2nd column) select Custom Definitions > Custom Dimensions. Create Internal Traffic Custom Dimension in Google Analtycs
  2. Next click on the red button +New Custom Dimension   
  3. Configure the dimension as follows
    • 3.1 Name: InternalTraffic
    • 3.2 Scope: User
    • 3.3 Check the Active box Configure Internal Traffic Custom Dimension in Google Analtycs
  4. Click Create and in the next screen click Done.
  5. Back in the Custom dimensions list, you will see your new custom dimension and next to it the Index number, note down this number because you will need it later. Index Number for Internal Traffic Custom Dimension in Google Analtycs

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

STEP 3
  1. In the left panel of the GTM main screen, click on Variables,
  2. Scroll down to the User-Defined Variables section and click NEW at the top right
  3. Click on Variable Configuration and select 1st Party Cookie
  4. Name your Variable Cookie - InternalTraffic (important! use the exact same name)
  5. As Cookie Name enter InternalTraffic (important! use the exact same name)
  6. Save the variable
  7. Now open your Google Analytics pageview tag or your Google Analytics Settings variable.
  8. Under the tag, configuration click on More Settings
    Note: if you don't see the More Settings option check the override settings box. 
  9. Expand Custom Dimension,
  10. Click +Add Custom Dimension to add a new item (only one),
    • 10.1 In the Index box enter the Index number of your custom dimension; the number I told you to remember from the step 2-5 ;)
    • 10.2 In the Dimension Value box enter {{Cookie - InternalTraffic}}  (important! use the exact same name) and save the tag. Add internal traffic variable to GA pageview tag
  11. Lastly, click on Submit in the top right corner of the main GTM window. Submit GTM changes
    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 Google Analytics

STEP 4

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

  1. Go to the Admin Section of your Analytics, click on Filters under the View settings (3rd Column)View filter in Google Analytics
    Important: 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 +ADD FILTER Add filter button Google Analytics
  3. Configure the filter as follows:
    • 3.1 Filter name: Exclude Internal Traffic - Dynamic
    • 3.2 Filter type: Custom
    • 3.3 Type: Exclude
    • 3.4 Filter Field: go to the bottom of the list and select InternalTraffic (This is the custom dimension you created before)
    • 3.5 Filter Pattern enter trueCreate a filter for dynamic IPs in Google Analytics

STEP 5: Activate and Test Your Filter

STEP 5
  1. To activate the cookie and the filter:
    • a) Bookmarklet method: Click the bookmarklet you added while you are on any page of your website.
    • b) GTM method: Open any URL of your site with the text ?internal at the end -> carloseo.com/?internal
  2. Open the real-time report in Google Analytics (Important! make sure you are seeing the view where you applied the dynamic filter AND your IP is not filtered by default)
  3. Then navigate through different pages of your website.
  4. If you don't see those pages in the real-time report, then Congratulations your filter is working! :)
    Note: you may see the page with ?internal at the end. This is fine as long as the next pages are not showing.

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, however, some browsers like safari have shorter limits, so it is a good idea to refresh the cookie from time to time.

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 GA filter 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)
  • Check if your Google Analytics code is not duplicated. This often happens when you have GA firing through GTM and also the standard GA code inserted at the same time. You can use the Tag Assistant extension to review your implementation.

This should cover the most common issues. Let me know in the comments what do you think of this solution?

Need help developing, optimizing, or troubleshooting your Analytics system?

  • Filters for data quality
  • User interaction tracking (events, goals)
  • E-commerce tracking
  • Conversions, Goal & Funnel Configuration
  • Sub-domains & Cross-domain tracking
  • Dynamic IP filtering
  • Google Tag Manager implementation
  • AMP tracking/integration
  • Integrations (Google Ads, Search Console, Facebook Ads, etc)
  • Personalized reports (Data Studio dashboards)
  • Monthly reporting
  • And more...