My fairly intelligent colleague Amin Shawki (he gets a big head with too many compliments) wrote a great blog post on how to filter internal traffic with Google Analytics by using a user defined value. This goes beyond the ability to filter out set IP addresses or IP ranges to filter out dynamic IP addresses.

Basically, by creating a new, private page on our company website (we use WordPress, so it’s extremely easy), and adding a small bit of code to that page, we can set a cookie on our browsers to identify us as ‘internal’ to Google Analytics. You don’t need WordPress for this to work, all you need is a private page on your site where you can add some code. From there, you can create an advanced segment or profile filter to filter our internal traffic. One note, the _setVar method is being deprecated by Google and they recommend using custom variables.

Filter Internal Traffic Using Custom Variables + Google Tag Manager

So this got me thinking recently, how would I filter internal traffic using Google Tag Manager and custom variables? Like my company’s website, my blog uses WordPress as well. And I use Google Tag Manager to implement Google Analytics. So how would I go about doing that? Well, it’s actually pretty simple using a data layer and private page.

Taking Amin’s idea a step further, I built a private page on my blog and utilized the data layer to set a custom variable any time a page view is recorded. Now, there are numerous ways to add a data layer to a page. I took the easy way and simply added it to the “Text” section of my new private page.

Data Layer in WordPress Page

You can see I added the data layer right into the “Text” section of the private page.

 

The purpose of the data layer is to pass information to Google Tag Manager. In this example, I’m passing a variable called ‘visitorType’ with a value of ‘internal’ to Google Tag Manager. But I’m not done. This variable isn’t being pushed anywhere besides the data layer. I’ll need to hop over to my Google Tag Manager container to pull the variable from the data layer and pass it into Google Analytics as a custom variable.

We’re going to tell Google Tag Manager to fire a custom variable any time it sees a dataLayer object with the ‘visitorType’ variable in it. Within my Google Tag Manager container, I’m going to go into my classic Google Analytics tag (ga.js) and go to More Settings > Custom Variables > New Custom Variable. I’ll need to do two things:

1. Choose slot 1 (or 2, 3, 4, or 5) and call this custom variable visitorType. Also select Visitor level for scope. We want this custom variable to set a cookie that lasts all subsequent sessions.

Adding custom variable in Google Tag Manager

2. Give it a value by creating a macro. This macro is going to populate the value of the custom variable by pulling it directly from the data layer. Click on “+{{macro}}” to the right of the Value form and select “Add New Macro”. Here’s what your macro should look like:

Adding a macro to Google Tag Manager

 

This macro is populating the value of the custom variable with the value of the visitorType variable in the data layer. Hopefully I haven’t lost you! If I have, please feel free to post in the comments and I can help clarify.

After you click “Save”, create a new container version and publish it. Now you have GTM ready to fire a custom variable to Google Analytics on your private page.

Set The Custom Variable

So now navigate to the private page you created so you can set the custom variable on your own browser. Do the same thing with any other browser you may use, as cookies are browser-based.

Note: the custom variable you set for yourself will be deleted if you delete your cookies.

Create your Custom Segment to Exclude Internal Traffic

The last thing to do is create an advanced segment in Google Analytics to filter internal traffic. I set up the custom variable in slot 1, so here’s what my custom variable would look like:

Advanced Segment to Filter Internal Traffic

Congratulations, you just learned how to filter internal traffic using custom variables and Google Tag Manager! This is extremely important for people with dynamic IP addresses, as the IP address changes.

As always, if you have questions or comments, I’d love to hear them. Post them below in the comment section. Anyone else using a different method to filter internal traffic with Google Analytics? I’d love to hear about it!

Want to read more posts like this? Check out my company’s blog here.

Comments
Harvey Specter
Posted at 5:43 am September 23, 2014
Glenn Weidner
Reply
Author

HI Andy,

This post intrigues me. However, it seems that I might need your help. 1) I am using Universal Analtyics, 2) Google Analytics seems to have changed since this post was written with respect to Custom Variables.

Can you recommend a way to filter our traffic for external IP addresses or mobile employees using the current version of Google Analytics and Google Tag Manger (where Universal Analytics is being used).

I’d really appreciate any advice.

Thank you!

    Harvey Specter
    Posted at 10:36 am September 23, 2014
    Andy Gibson
    Reply
    Author

    Hey Glenn,

    This concept can still be achieved using Universal Analytics and Google Tag Manager. Instead of using custom variables, you would use custom dimensions. You’ll need to see up the custom dimensions within the Universal Analytics interface (Settings > Property > Custom Definitions > Custom Dimensions) and then update your Universal Analytics pageview tag to use the custom dimensions instead of custom variables. Same process in GTM, just different terminology.

      Harvey Specter
      Posted at 6:19 am July 24, 2015
      pinpin
      Reply
      Author

      hello Andy, i am still confuse a little bit in setting up the custom dimension in google tag manager. what should i input at the field “index” should i just input “1” ?

        Harvey Specter
        Posted at 8:05 pm August 3, 2015
        Andy Gibson
        Reply
        Author

        It depends on what Custom Dimension slot you want to use. For free Google Analytics, you have 20 Custom Dimension slots to choose from. If you don’t have any set up, then use “1”

Harvey Specter
Posted at 6:49 am September 25, 2014
Ahamed Rushdhi
Reply
Author

Hi Andy,
Shouldn’t the data layer be added above the Google Tag Manager Script for it to work on page load?

I followed your instructions and the GA doesn’t seem to be firing at all on the private page I created.

Really appreciate if you could clarify this for me.

Thanks
Rushdhi

    Harvey Specter
    Posted at 9:17 am September 25, 2014
    Andy Gibson
    Reply
    Author

    Yes, the data layer should go above the GTM container tag. Check your firing rule for GA to make sure it’s set to All Pages and also double-check GTM is on the private page.

Harvey Specter
Posted at 5:21 pm November 26, 2014
W.
Reply
Author

Andy,
I can see in GTM debugging that (Dimension1 = internal ) is fired from my private page. but when I connect to other pages, I can see anything in my cookies that would prevent tell GTM and GA that I am internal.
Am I missing anything?

    Harvey Specter
    Posted at 5:42 pm December 9, 2014
    Andy Gibson
    Reply
    Author

    With Universal Analytics, the Custom Dimensions are collected server-side. The hit where the Custom Dimension is set will show up in GA Debugger, but hits afterward won’t show the Custom Dimension until the value changes. It’ll still show up in GA.

Harvey Specter
Posted at 5:00 pm December 10, 2014
Sandro
Reply
Author

Hi Andy,

how can I be sure that my GA is really filtering the Segment I’ve created to filter?

Regards

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>