Filtering remote employees in Google Analytics using Google Tag Manager

The distributed employee problem

One challenge faced by nearly all SaaS companies is how to keep company employees from skewing analytics data, particularly web analytics. While internal employees have frequent interactions with web products and marketing sites, their activity is often quite different from users and prospective customers. As a result, internal data can skew analytics reports and, in the worse case, cause stakeholders to make improper design decisions.

The most common way to filter internal users is by using an IP address filter in Google Analytics to filter your office IP addresses; indeed, this approach was implemented before I arrived at RightScale five years ago. This is a great solution if your workforce always works from the same location. With the increasingly distributed nature of tech professionals, however, this approach becomes insufficient. This was the exact problem we encountered at RightScale, where a third of the company are remote workers, and locals often find themselves working from home or the road.

Do not track page

Our first attempt was made several years ago, before we started using Google Tag Manager. We created a page on our main company site that would install a cookie identifying the user as a RightScale employee, and directed our employees to visit the page. We then set up filters in Google Analytics to filter out users with that cookie.

The problem was that our employees would often fail to visit the page, use a new browser, or clear their cookies. In addition, the Google Analytics filters were limited in the scope of what they could block (only Google Analytics).

Using Google Tag Manager with your internal portal

After introducing Google Tag Manager (GTM) over a year ago, I decided to revisit the problem of tracking internal traffic. With all of our web properties using Google Tag Manager to track not only with Google Analytics, but with other 3rd party tags as well, it seemed the correct place to implement a solution. I also realized that using an internal portal that all employees visit regularly (in this case the company wiki) would relieve our employees of the burden of remembering to visit a ‘Do not track’ page.

After careful research, I decided to try two approaches, both of which are outlined below. While both have advantages and disadvantages, I found that the 1st-party cookie option worked best for us.

Note that the first step in both cases outlined below is to add the Google Tag Manager Snippet to your internal portal. In our case, I created a new container for our Wiki and had our IT team embed it into our Confluence Wiki. If you are not currently using GTM in your organization, I strongly advise you to view the getting started information that Google provides and set up GTM containers and tags for your web properties.

GTM Wiki Container

GTM Wiki Container

Filtering users with Custom Dimensions

Advantages

  • Does not require any custom code

Disadvantages

  • Harder to implement on multiple web properties
  • Only works for Google Analytics data, not other GTM tags
  • Data is still reported to Google Analytics (though filtered)

Instructions

The simplest (though not necessarily easiest) way to filter users is to add a new custom dimension to track your users. This method makes the most sense if you only use one Google Analytics property, and do not need to filter employees from additional 3rd party tracking software like Marketo.

To do this, you must navigate to the Google Analytics Admin section and select the properties that you use for the sites and/or products that you want to filter. Select ‘Custom Dimensions’ and click the button to create a new custom dimension. Choose (and remember) an index, and set the scope to user. Note that you will need to do this for each property that you wish to avoid tracking; if you have more than one property, you may want to use the 1st-party cookie method outlined below.

Create Custom Dimension

Create Custom Dimension

Next, you will need to create two filters in each view for each Google Analytics property that you don’t wish to track. Select the property and view in the Admin section, then select Filters on the left. Create a new filter with a ‘Custom’ filter type. Select ‘Exclude’, and choose the Custom Dimension you created above in the filter field. For filter pattern, use ‘true’, then save the filter.

If your views aren’t already filtered to a specific hostname, you will likely also want to add a filter so that users visits to your internal portal do not show up in reports for your properties. To do this, create a new exclude filter for the hostname of your internal portal (e.g. “intranet.acme.com”).

Create a GA filter

Create a GA filter

Now, navigate to your GTM container for your internal portal. You will need to add a new tag. For firing rules, use the default “All pages” rule. For tag type, select “Universal Analytics” and enter your Google Analytics Tracking ID (or use a macro to store the string). For track type, select ‘Page View’, then expand More settings and Custom Dimensions. Under Index, enter the index for the custom dimension that you just created in Google Analytics. For your Dimension, enter true. Then, save the tag. After testing with the preview option, publish your tag.

No Track Pageview tag

GTM Tag with Custom Dimension

GTM Tag with Custom Dimension

At this point, anyone who visits your internal portal and then visits your web properties will be filtered from the analytics reports. Unfortunately, your other tags for things like 3rd party marketing tracking will still fire for your internal employees. In addition, your employee visits will still be sent to Google Analytics, they will just be filtered out.

Filtering employees with a 1st party cookie

Advantages

  • Works across multiple Google Analytics properties
  • Does not require any configuration in Google Analytics
  • Can filter employees from any GTM tag
  • No data reported to Google Analytics

Disadvantages

  • Requires some custom code

Using a cookie to filter internal users is the most robust and flexible method. It works easily across any number of Google Analytics properties, can be used to filter employees from other 3rd party software, and does not report any data at all to Google Analytics for employees. The main downside is that it will require one fairly small custom HTML tag to be added to your internal portal with GTM. If your IT team doesn’t object, a 1st party cookie is probably the best solution for you.

The first step is to create a custom HTML tag to insert the cookie into visitors’ browsers. To do this, navigate to the GTM container for your internal portal and create a new rule. The rule should be ‘event’ ‘equals’ ‘gtm.dom’. This rule when fire when the page has finished loading; you will need it for your next tag.

Create a DOM load event

Create a DOM load event

Now you will need to create the tag which will generate the cookie. Name your tag and add your DOM loads rule to the tag. Select ‘Custom HTML’ for the tag type and paste the following code, replacing ‘acme.com’ with your own domain (do not add www or any subdomain):


<script>
try{

// Create a cookie for internal users
var domain = ‘acme.com';
var theDate = new Date();
theDate.setMonth(theDate.getMonth() + 12);
document.cookie = “internal_employee=true;expires=” + theDate
+ “;domain=” + domain + “;path=/”;

} // end try

catch(e){

console.log(“GTM Error: ” + e.toString());

} // end catch
</script>

Then save the tag, test, and publish it. Users should receive your new cookie called ‘internal_employee’ when visiting your internal portal. You can test this in the Google Chrome browser by opening the inspector (Ctrl/Cmd+Shift+c), clicking the resources tab, expanding ‘cookies’, and viewing the cookies for your domain.

Internal employee cookie tag

Internal employee cookie tag

Now, navigate to any GTM containers which have tags that you want to prevent from recording internal users. In those containers, you will need to create a macro and a rule. First create the macro to retrieve the cookie value. In the new macro page, select ‘1st Party Cookie’ for the Macro Type and enter internal_employee for the Cookie Name.

Get Cookie Macro

Get Cookie Macro

Now, create a simple rule using that cookie. The rule should be set to trigger when ‘{{Get internal_employee cookie}} equals true’ (use the name of the macro you just created)’.

Is employee rule

Is employee rule

Finally, you can choose any tags that you don’t wish to fire for your internal employees. Select the tag, and add the rule you just created as a blocking rule. This will work for Universal Analytics tags, as well as any third party tags that you might have created.

Add rule to existing tags

Add rule to existing tags

That’s it! Your internal traffic will now be filtered without any extra effort on the part of your remote employees.