Get Visitor IP Address with Google Tag Manager

Comments:23

    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.

Leave a Reply

Your email address will not be published.