Author

GA4 & GTM Developer

Optimizer Troubleshooter

Follow me on
Category | Google Analytics
Difficulty |

The Challenge of Filtering Internal Employee Traffic with Ever-Changing Dynamic IPs 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.

In today’s dynamic workplace, where teams can work from virtually anywhere and have to deal with ever-changing IPs, filtering internal traffic in Google Analytics 4 using traditional methods can feel like a never-ending task.

But don’t worry, we’ve got your back! This guide will walk you through a more efficient way to filter internal traffic with dynamic IPs that requires zero maintenance. And the best part is that after the initial setup, anyone on the team can use it, no matter their tech skills, location, device, or network.

Traditional vs Dynamic IP Filtering Solution

Most standard solutions rely on IP addresses. You set up a filter or rule that checks the IP of the user accessing the site, and if the IP matches one on your exclusion list, it blocks the traffic in GA4.

The problem is, every time an employee works from a new place, you need to update your IP list. And even if they’re at the same location, their IP will likely change over time, so you’re stuck in an endless cycle of updating filters and tracking IP changes.

The dynamic solution I’m about to share doesn’t bother with IP addresses at all. Instead, it uses a simple directive that any team member can use to signal they’re working on the site, making sure their traffic isn’t counted. While the initial setup might be a tad more complex, once it’s ready, there’s no maintenance required.

Which scenarios does this guide covers

If you check one or more of the following scenarios, the dynamic filtering solution that I'll share below will be a game-changer for you:

a. Multiple work locations

For example:

  • Remote Work: Employees working from home offices.
  • Multiple Offices: Teams spread across various branches or working at client sites.
  • Frequent Travel: Employees accessing the website from hotels, airports, or cafes.

b. Ever-changing dynamic IPs

For Example:

  • Dynamic IP: Most ISPs automatically change IPs every few months, weeks, or even days.
  • Mobile Access: Team members using mobile data (3G, 4G/LTE, 5G) to access the site.
  • VPN Usage: Employees accessing the website via virtual private networks.
  • Privacy Compliance: Implementing IP Anonymization (anonymizeIp) for GDPR and others

FAQ: Static vs Dynamic IPs

Static vs Dynamic IPs

Static IPs are rare. To truly have a static IP that never changes, you have to own it, which is a pricey perk typically reserved for big companies.

For the rest of us, we must deal with dynamic IPs. Some are lucky and their IPs only change every few months, while others might see their IPs change almost every time they connect to the network.

Is it Possible to Have a Quasi-Static IP?

Maybe. It depends on your ISP. Some providers offer static IP addresses as an extra paid service, while others don’t offer this option at all.

Keep in mind that getting a static IP for one location won't be much help if other team members still have dynamic IPs or work from various places like hotels, coffee shops, or airports. So, shelling out for a static IP just for this purpose isn’t worth it.

How Often Do Dynamic IPs Change?

The frequency varies widely by ISP. You might have the same IP address for a couple of months, but it could also change every few weeks or even every few days.

How to Find My IP and Find how often it changes?

There are some online tools and browser extensions available that can help you keep track of your IP address, like as this one. Alternatively, you can simply check your IP address regularly by typing "Show me my IP" in Google and keep track of changes.

Here's a glimpse of how my IP changed over a few 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...
Be the first to comment :)