Get IP Address with Google Tag Manager

In this post we will learn how to track IP address with Google Tag Manager and how to set that as a custom dimension. There are a few reasons you may want to access your visitors’ IP addresses using Google Tag Manager (GTM): to block internal traffic, set rules for firing tags, pre-fill form fields, etc. Tracking IP address with Google Analytics is also possible through this method, but not advisable as it may violate Google’s policies regarding personally identifiable information.

This post will show how to collect IP address into a GTM variable, using a script that you can copy and paste.

STEP 1: COLLECT VISITOR IP ADDRESS

For the first step, you will need to pull in the visitor’s IP address. There are many ways to do this, but I’ll describe how to do it using JavaScript since it’s a universal method that will work for nearly any setup.

In GTM, navigate to Tags > New > Custom HTML and paste in the following:

1234567

<script type=”application/javascript”>  function getIP(json) {    dataLayer.push({“event”:”ipEvent”,”ipAddress” : json.ip});   }</script> <script type=”application/javascript” src=”https://api.ipify.org?format=jsonp&callback=getIP”></script>

The tag will look like this:

The above JS snippet pulls each user’s IP address via https://www.ipify.org/, which is a free IP address API, and then pushes it into the data layer for GTM to read.

You can see how it returns the IP address for your own device by clicking this link: https://api.ipify.org?format=jsonp&callback=getIP.

 As mentioned, this method uses JavaScript. An alternate method is to pull IP address using a server-side language like PHP, which is faster and avoids having to depend on an external resource like Ipify. For example, if you were using WordPress, you could paste the following into your header.php file above the GTM script, and then proceed with steps 3 & 4 the same way:

1234567

<script>window.dataLayer = window.dataLayer || [];window.dataLayer.push({‘event’:’ipEvent’,’ipAddress’ : ‘<?=$_SERVER[“REMOTE_ADDR”]?>’});</script>

STEP 2: SET TRIGGER

The simplest trigger is to set this script to fire on All Pages.

However, to avoid querying the ipify API repeatedly and potentially racking up a lot of hits, you could alternately set this script to fire on just the landing page (the first page a visitor sees) of your site. This way it will only collect IP address once, which works for most cases since IP address rarely changes during a session.

To do this, navigate to Triggers > New > Page View and choose the radio button “This trigger fires on Some Page Views”.

STEP 3: PUSH IP ADDRESS INTO A GTM VARIABLE

Navigate to Variables > User-Defined Variables  > New  > Data Layer Variable.

Fill in Data Layer Variable Name = ipAddress. Note that this is the same name that was pushed to the data layer in step 1; here we are collecting that value so we can use it in other tags later.

STEP 4: CREATE A TRIGGER ON THE NEW EVENT

Navigate to Triggers > New > Custom Event.

Fill in Event Name = ipEvent. This is the event name that was pushed to the data layer in step 1. By connecting the GTM data layer event to a trigger, you can use it to trigger other tags.

Following the implementation of the above steps, you will now have a new GTM Trigger (ipEvent) and Variable (IP Address), which you can access in other tags or use to set triggers. This is a screenshot of the output in Preview mode.

It’s a grey area whether IP addresses are considered personal information. Please be aware of your site’s privacy policy, any relevant local laws, and Google’s policies regarding personally identifiable information. The new GDPR rules DO consider IP addresses to be personal data, as described here: https://eugdprcompliant.com/personal-data/.

How to record IP address in a custom dimension

STEP 1: SET UP A SESSION-SCOPED CUSTOM DIMENSION IN GA

In the Google Analytics interface, navigate to Admin > Property > Custom Definitions > Custom Dimensions

Click + NEW CUSTOM DIMENSION and create a new session-scoped custom dimension called IP Address. Save it.

STEP 2: RECORD THE INDEX NUMBER

After hitting Save, you’ll see your custom dimension summary screen. Note the index number that was assigned by Google Analytics. You’ll need this in a minute.

STEP 3: CREATE A UNIVERSAL ANALYTICS EVENT TAG

In Google Tag Manager, click Tags > New > Universal Analytics, and choose Track Type = Event. Fill it out with the following settings:

Category = IP, Action = Sent. These names aren’t important, so you can choose different names if you prefer.
Non-Interaction Hit = True. This is to prevent the hit from affecting bounce rate.
Enable overriding settings in this tag  = checked (or, alternately, you can add the index and dimension value into your Google Analytics Settings Variable instead).
Index = index number from step 2 above
Dimension Value = {{IP Address}}
Trigger = ipEvent

The completed tag should look like this:

CONCLUSION

This post shows how to record IP address via GTM and extends that to show how to pass that IP address into a custom dimension in Google Analytics. Because custom dimensions need to be attached to another hit like a pageview or event, the above method sends the IP address along with a dummy event. Once you complete the above directions, you will be able to access your users’ IP addresses as a Secondary Dimension in any standard report, or as a Primary Dimension in a custom report. You can also use these custom dimensions to target specific audiences for remarketing.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top