ANALYTICS

Blocking your tags firing from your own IPs with Google Tag Manager


David Vallejo
Share

The subject about how to exclude your own visits from GA has been commented before loads of times, most of time filters are used to keep out our own visits from Google Analytics , but some other tools may not have the possibility to filter out our own visits. This is why we're going to use Google Tag Manager to block all our tracking pixels, conversions pixels, custom tags, or whatever tag type we want.

We're going to lay on a Lookup Table type macro and the dataLayer to get this sorted out :) . Firstly, as JavaScript is not able to get the user's IP address, we're going to push the current user's ip address to the dataLayer. We could use some JS based services to get the user's IP but for using it this way we'll need to delay our tags ( as we need to wait to receive the data from some external service, which could be down, or not reachable from user's connection. So we'll need to pass the request to our developers or IT team to implement a new key into the default dataLayer.

As our site is certainly using some kind of server-side language, this should be a problem at all :) . Let's see an example about how could we do this using PHP :

dl_optout

Any other language will be able to do this

Ruby

request.env["REMOTE_ADDR"]

ASP

Request.ServerVariables("REMOTE_ADDR")

PHP

$_SERVER["REMOTE_ADDR"]

Python

os.environ['REMOTE_ADDR']

As you see all languages give you access to the "REMOTE_ADDR" header variable, but this will fail if the user's behind a proxy. So usually we'll want to check for "HTTP_X_FORWARDED_FOR" and it's not present we'll use "REMOTE_ADDR", this is up to the developers to print the visitor IP Address is the best way they can.

Now, let's setup 2 new macros, one of them will take care of reading our user_ip value from the dataLayer, the other will be a lookup table macro that will will use this first macro for the condition:

Macro that grabs the visitor ip address from the dataLayer
Macro that grabs the visitor ip address from the dataLayer
Lookup Table that makes the magic happen
Lookup Table that makes the magic happen

Now we have 2 ways to go, either using a blocking rule to our tags (we'll be using this if we want to block certain tags as we should now Blocking Rules prevail over Firing Rules ) , adding a new condition to our existing rules (if we want to block all tags depending on a generic rule, like "All Pages" :

dl_optout_rule_all
dl_optout_rule_notall

There we go, now we could easily avoid sending data to Google Analytics from our Office IPs , but for any other tag that we're using withn Google Tag Manager.