Get Visitor IP Address with Google Tag Manager

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.


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:

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

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

The tag will look like this:


The above JS snippet pulls each user's IP address via, 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:

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:
window.dataLayer = window.dataLayer || [];
    'ipAddress' : '<?=$_SERVER["REMOTE_ADDR"]?>'


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".

Choose Referrer does not contain {{your domain name here}}.



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.



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.


54 thoughts on “Get Visitor IP Address with Google Tag Manager”

    • 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.

      • Hello Ana,

        Is there any way to know who browse my website is from which company OR and other details. As I checked above but not able to understand.

        Can you please also upload an article on that?

      • Hi Sanjay, this information is reported in Google Analytics. You can navigate to Audience > Technology > Network to see a list of service providers. Choose Secondary Dimension = Network Domain for additional information. Hope this helps!

      • Hello Ana,
        I was tried the above suggestion which is about getting company name but I can't get. Please give me the clear solution to resolve this one.

      • Are you using Google Analytics? If so you don't need to do anything at all as it's included in GA by default. Just navigate to Audience > Technology > Network to see a list of company names, and choose Secondary Dimension = Network Domain for some additional information.

    • 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?

    • 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

    • 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 ?

    • 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.

      • 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 ..

      • 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 [email protected] .

      • 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 !!

      • 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?

    • 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)

      • 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!

      • 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,

    • 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!

      • 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,

  5. Hi Ana,
    This is great for running reports, but I'm wondering if you've thought of tackling an automated process, from GTM to a Google Sheet? I know for someone like me, the ability to automate the process, populate an IP or email into a client specific google sheet would be like hitting the jackpot. I've seen some links out there, but they don't quite work because they are "fill in" sections on a website. I'll link a video here, maybe you can get it to work because I'm totally stumped. I've been at this for days and I know I'm missing a simple step. Maybe a fresh set of eyes would help! Here's the link, let me know what you think.

    • Hi Jeff, I just tested the process outlined in the article you linked and it worked for me. The only change I made was to the selector values in the custom JS variables, since my form was set up a bit differently from the one in the example. Therefore I suggest verifying that your selectors work (go to your site, enter some values into the "name" and "email" fields on your form, open developer tools, and paste your code into the console. Make sure it returns your name and email). In my case, I used the following adjusted code:

      Email address:
      var x = document.querySelector("[name='your-email']").value;
      return x||"";

      var x = document.querySelector("[name='your-name']").value;
      return x||"";

      If you share your site URL I can take a look and give more specific advice.

  6. Can you apply a mask to the IP value returned by ipify to anonymize the address slightly? eg for IPv4 addresses?

    • You can't adjust how the value gets returned from the IP service, but you could certainly add a new script that masks the output and returns it into a new variable.

  7. This probably breaches Googles PII (personally identifiable informtion policy) and most likely breaches GDPR in Europe.

    • I don't see how using Google Tag Manager to access IP addresses could violate anything. GTM is just a tagging tool, it doesn't store or record any user data. In fact it can help people comply with GDPR since it allows you to fire different tags for users with IP addresses in the EU, without having to store those IP values in any database.

  8. Hi Ana,

    I am getting unexpected hits to my website from different country in my daily GA report. And would like to know the IP address from where i am getting these hits to different pages of my website. This is actually increasing the bounce rate of my website. Is there any way to track that particular IP Address and how would i stop this un-suspicious activity. Please i need your help...

    • I suggest investigating other attributes of the visit, for example by choosing Secondary Dimension = service provider, browser, or hostname. Then you can create an exclude filter to remove any unwanted traffic from your reports.

    • Hi there, this post is about Google Tag Manager, which is a tagging tool, not a reporting tool. If you wanted to see a list of IP addresses you would need to pass your GTM variable into a reporting or analytics tool. But you generally don't need to use GTM to see IP addresses anyway, since virtually every website automatically tracks IP addresses by default and reports them in the site server logs. With that said, if you want to see how to pass IP addresses from GTM to Google Analytics, you can check this post:

      • Hello, thanks for reply, it says when publishing GA custom event – “Unknown variable “IP Address” found in a tag. Edit the tag and remove the reference to the unknown variable.” So i guess there is some step missing? Thank you once again, this would be very helpful.

      • The error message means you're missing the variable called 'IP Address'. Make sure you've included it as described in step 3.

  9. H Anna would it be possible to block visitors from a page based on IP address. I run a site in the UK, the conversion page is a form that collects an estimate resquest. But im getting loads of form fills from outside the UK. I dont mind them browsing, but dont want to deal with the from fill replies from people who cannot be customers. Is it possible to block or serve an overlay on this page based on their address? To say sorry this service is only for customers based in the UK? Using a method like this. thanks in advance Lee

    • You can't view the IP addresses from the steps here, this is only to temporarily access them in Tag Manager, which doesn't record anything. In Tag Manager you can then attach these values to any tag you want (e.g. as described in this post), or, conversely, use this information to block certain tags from firing.

Comments are closed.