Get Visitor IP Address with Google Tag Manager

Comments:28

    1. The service I suggested above only provides IP address. If you wanted to get company name instead, you just need to edit the script to pull from a different API that provides company name, such as https://ipinfo.io. So in that case you’d adjust your custom HTML tag like this:
      <script type=”application/javascript”>
      function getIP(json) {
      dataLayer.push({“event”:”ipEvent”,”ipCompany” : json.org});
      }
      </script>

      <script type=”application/javascript” src=”https://ipinfo.io/json?format=jsonp&callback=getIP”>

      Once you’ve done that, just change the Data Layer Variable Name to ipCompany (instead of ipAddress) and it should work.

      1. Hey Andrew, I checked out your site using the same setup described here (I used the Tag Manager Injector to access my GTM container on your site) and it worked fine for me. Sorry, I’m not sure why it doesn’t work for you… are you masking your own IP address with a VPN / proxy / etc.?

    1. Hi Artem, as far as I know there’s no way to do this. AdWords clicks are tracked when the user clicks on an ad on Google, which occurs before they’ve reached your site. Therefore AdWords-specific data (clicks, cost, CPC, etc.) is only associated with a small subset of your GA data. I don’t believe you can connect any GA custom dimension to AdWords clicks.

  1. Thanks for great article!
    Could you ask me how I can transfer these ip-addresses to Google Analytics? Need I create another event?

    1. You’d need to fire a tag to GA that contains the IP address. One way would be like this:
      1) in GA Admin, set up a session-scoped custom dimension called IP Address. Note the index number.
      2) in GTM, add a new Universal Analytics event tag, using the custom event ipEvent as the trigger. The category/action/label names don’t really matter here since it’s just a vehicle to send the IP Address.
      3) attach the data layer variable ipAddress to your new tag as a custom dimension (More Settings > Custom Dimensions). Index number = same index that was assigned above by GA Admin, Dimension Value = {{IP Address}}

      2018-05 update: expanded these instructions here: Track IP Address in Google Analytics

    1. hey Gopi, https://www.ipify.org/ only provides IP addresses. If you want to get location you need to use a different service, for example https://ipinfo.io/json (click to see all the data it returns for you). So you need to change the script to ipinfo.io and pull in whatever location point you’re looking for (city, region, or country). An example of the revised script would be like this:

      <script type=”application/javascript”>
      function getIP(json) {
      dataLayer.push({“event”:”ipEvent”,”ipCity” : json.city, “ipAddress”: json.ip});
      }
      </script>

      <script type=”application/javascript” src=”https://ipinfo.io/json?format=jsonp&callback=getIP”>

      It should work if you paste the above into a Custom HTML tag, and add a new Data Layer Variable for ipCity.

  2. Hi Ana,

    Can you help me with step by step (if possible by screen shot) how will I bring the IP Address values in Google Analytics ?

    1. Hey there, I included step by step instructions for sending the IP address to GA in a comment above. Let me know if that works for you.

      1. Hi Ana,

        Sorry to interrupt you again, I am not able to understand point.3

        3) attach the data layer variable ipAddress to your new tag as a custom dimension (More Settings > Custom Dimensions). Index number = same index that was assigned above by GA Admin, Dimension Value = {{IP Address}}

        I don’t know where to go and how to configure
        Plz help Ana ..

      2. Open up the event tag from step 2, and click More Settings > Custom Dimensions. You’ll see 2 fields open up: 1) Index Number and 2) Dimension Value. For Index Number add the index number that Google Analytics gave you in step 1, then enter {{IP Addresses}} under Dimension Value. Does that clarify?
        If you still have any questions, you can shoot me an email at info@mixedanalytics.com .

      3. Hey Ana,

        If possible is there any other way to achieve the IP address in Google Analytics ? Apart from the steps you’ve mentioned in a comment ?

        Thanks in Advance !!

      4. There are other methods, but you might not get the results you want. For example, you could create an event tag that has event category = ‘IP Address’, event action = {{IP Address}}. Then you’d get a list of IP addresses in GA without needing to set up any custom dimensions, but reporting wouldn’t be as flexible.

  3. Hi,

    Cool post and I guess exactly what I need to block internal traffic by IP-Address. To do this I use a script that will return if someone is internal or external traffic:

    function() {
    var newIP = ‘{{visitorIP}}’;
    //INTERNAL TRAFFIC
    var patt = new RegExp(“^28.28.128.1[0-9]?$”);
    if(patt.test(newIP) == true) {return(‘internal’);}
    return(‘external’);
    }

    When Internal is returned, the Universal Analytics tags will not fire. Only problem is that the ipEvent is fired after the pageview. What always results that a visitor is External and Universal Analytics tag will always fire. Do you know how to fix this?

    1. Yeah, the problem is that it takes some time to query the ip database and return a value. To resolve this you’d need to delay the pageview tag until ipEvent fires. For example, instead of firing your pageview on the All Pages trigger, you could fire it on ipEvent instead.

      The issue with the above is that generally you want to avoid delaying the pageview tag, since page views are the basic unit of GA tracking. If possible, it’s better to just filter out the traffic via GA’s IP filtering functionality (GA Admin > View > Filters > Custom > Exclude Filter Field = IP Address)

      1. Hi Ana,

        Thanks for you reply. Yes, that’s what we did before, but because of the GDPR we want anonymize the IP-Address, which makes it impossible to filter internal traffic by ip-adress, since the last 3 digits are unknown. So we should filter a range of ip-addresses.

        With the IP-Address in the datalayer we can control when the GA tag shouldn’t be fired (when an ip-address is internal). In that way we can anonymize external traffic. That is way we want to do it this way.

        Firing the ga-tag by the ipEvent works well!

      2. Got it. I may have misunderstood something, but I’ll also mention that you can filter out a range of IP addresses in GA Admin by entering regex into the filter condition (just like in your script). There are also some other options to filter out internal users based on a cookie rather than an IP address (here’s a good post on that topic). Either way, glad to hear that using ipEvent as your trigger works for you!

Leave a Reply

Your email address will not be published.