Get Visitor IP Address with Google Tag Manager


    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 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” :});

      <script type=”application/javascript” src=””>

      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, only provides IP addresses. If you want to get location you need to use a different service, for example (click to see all the data it returns for you). So you need to change the script to 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” :, “ipAddress”: json.ip});

      <script type=”application/javascript” src=””>

      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 .

      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}}’;
    var patt = new RegExp(“^[0-9]?$”);
    if(patt.test(newIP) == true) {return(‘internal’);}

    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!

  4. Hi Ana,

    Thank you for the guide above. It has worked well, but now I have the same problem as mentioned above with the GDPR IP anonimization.
    How can I set up the Google Analytics tag not firing for a certain IP adress? Which tag has to fire beforehand and which triggers I have to use. Im not sure how to set this up in GTM.

    Thank you,

    1. Hi Katy, you should be able to modify your GA pageview tags so the trigger is ipEvent rather than All Pages. This ensures that the IP address variable will be populated when the tag fires. To exclude certain IP addresses, edit the ipEvent trigger as follows:

      This trigger fires on Some Custom Events
      Fire this trigger when an Event occurs and all of these conditions are true
      ipAddress does not match RegEx {{enter your blocked IP Addresses here}}

      Let me know how it goes!

      1. Hi Ana,
        thank you very much. It is so simple – I don´t know why I didn´t come to this solution. Maybe I was thinking too complicated.
        It works perfectly. Thank you and have a nice day!!

        All the best,

Leave a Reply

Your email address will not be published.