This website uses cookies to customize ads and analyze traffic. If you continue browsing, we'll consider you accept their utilization. More information.

How to Get a Visitor's IP Address in Google Analytics

Web Analytics -

To store an IP address in Google Analytics, just add the following code to your pages, before the traditional Google Analytics code:

 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
 elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
 $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];} else {

Then, follow these instructions depending on if you use Universal Analytics or the traditional Google Analytics.


Create a new dimension as explained here, set "IP" as name and "Session" as scope. Then use this Google Analytics code, replacing the UA parameter:

      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

    ga('create', 'UA-XXXXXXX-X', 'auto');
    ga('send', 'pageview', {
      'dimension1':  '<?=$ip;?>'

To see the data:
1) Create a report in "Custom Reporting".
2) Select IP as first dimension. It will appear under "Custom Dimensions".
3) Select Sessions as metric.


Add this code to your webpages before </body>:

<script type='text/javascript'>
 _gaq.push(['_setCustomVar', 1, 'IP', '<?=$ip;?>', 1]);

To see the data:
1) Create a report in "Custom Reporting".
2) Select Custom Variable (Value 01) as first dimension.
3) Select all the web metrics you want to see (visits, pageviews, time on site, etc.),

Once the first IP addresses are retrieved by the code, you can see the IP addresses of all your visitors, and then combine them with traditional Google Analytics metrics.


If you cannot use PHP, you still can register your visitors' IP addresses via Javascript. However, you'll be depending on external code which, while functional now, could fail in the future. Use it under your own responsibility. The code is:

<script type="text/javascript" src=""></script>
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

    ga('create', 'UA-XXXXXXX-X', 'auto');
    ga('send', 'pageview', {
      'dimension1':  myip


Thanks to Matt Holovach for this solution!
1) Disable your current Google Analytics plug-in or delete it if you manually included it in the headers.php file.
2) Add the following code into your functions.php file:

function get_the_user_ip() {
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//check ip from share internet
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//to check ip is pass from proxy
} else {
return apply_filters( 'wpb_get_ip', $ip );
add_shortcode('show_ip', 'get_the_user_ip');

add_action('wp_head', 'add_google_analytics');
function add_google_analytics() { ?>

      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

    ga('create', 'UA-XXXXXXXX-1', 'auto');
    ga('send', 'pageview', {
      'dimension1':  '<?=get_the_user_ip();?>'
<? } ?>


This script usually does't work in Analytics plugins for Wordpress, since Wordpress plugins usually don't run PHP scripts. If you're using Wordpress, remove your traditional Analytics plugin and replace it with a plugin that allows running PHP, such as Insert PHP.

Related Posts
  • How to Detect a Visitor's Country by his IP Address

  • How to Filter Internal Traffic from Dynamic and Fixed IP Addresses

  • How to Create Thematic Clusters with Google Analytics API

  • How to Create Graphs with Google Analytics API

  • How to Get Number of Tweets about a Website on Twitter
  • Send Comment


    Soujanya - 2016-07-25
    Hi,I have followed the same steps as mentioned from INSTRUCTIONS FOR UNIVERSAL ANALYTICS. I implemented the code in my HTML,but I am not getting IP addresses. Please let me know where I am doing wrong. Thanks in advance.

    Daniel Piñero - 2016-07-25
    Please send an email to me (check the email address under my picture), letting me know your website address.

    Prem - 2016-07-25
    Hi Guys am not getting the list of ip's in GA for my website ,am using the following JS code <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//','ga'); ga('create', 'UA-8xxxxxx-x', 'auto'); ga('send', 'pageview', { 'dimension1': '<?=$ip;?>' }); </script> any quick reply will be highly appreciable

    Daniel Piñero - 2016-07-25
    Please send an email to me (check the email address under my picture), letting me know your website address.

    Fabio - 2016-07-17
    Hi, I implemented the code and started collecting the IP addresses on google analytics however, I have an issue. The IP addresses recorded appear to be not correct. In other words, I made a test. I asked a friend of mine to connect from Germany and google analytics properly recorded a village in Germany. At the same time, the IP address associated with this user does not correspond to be an IP from Germany, rather from France (I copied and paste the IP in few geolocation tools available on internet). Is there a bug in the code or did I do something wrong?

    Daniel Piñero - 2016-07-17
    Hello Fabio. What I would do next is this: ask your friend to check some "What's my IP Address" tool, see what IP address comes out, and see if it matches with the IP address you're getting.

    Matt Holovach - 2016-06-13
    John, I'm sorry that you had an issue. I was able to replicate my work on two different Wordpress sites so I was assuming that it would work for everyone. Sorry again for any issues this may have caused.

    John - 2016-06-05
    Hello Matt, I also tried your code posted, minus the PHP tags and got a Parse error:syntax error on my WP install. I also deactivated the GA plugin on WP. However when I use the other code above: if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip=$_SERVER['HTTP_CLIENT_IP'];} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];} else { $ip=$_SERVER['REMOTE_ADDR'];} along with the javascript before the </body> tag, it doesn't screw up my WP site: <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//','ga'); ga('create', 'UA-XXXXXXX-X', 'auto'); ga('send', 'pageview', { 'dimension1': '<?=$ip;?>' }); </script> Time will tell if the IP is recorded or not. I'll report back, but it was the only code I could use that would not mess up my WP site.

    Vinayak Mudgal - 2016-05-06
    Thanks Man!

    Matt Holovach - 2016-03-19
    Michael, WORDPRESS UPDATE If you read this add the entire code including the script to the functions.php file. Obviously you will need to change the UA code on the analytics to match your code. FYI, You will probably have to remove the opening PHP tag <? and the closing tag ?> from the above code. Make sure you have FTP access to the files in case it doesn't work for you and temporarily bricks your wordpress account. That way you can manually edit the functions.php until it works properly. It shouldn't take trial and error but it might if you have a customized Wordpress functions.php file.

    Van - 2016-03-18
    IP addresses began accumulating - is there any way to time stamp these visits? Thanks!

    Daniel Piñero - 2016-03-18
    If you're still interested, contact me via e-mail. Let me know URL of your website and give me access to your Google Analytics account, to fully understand the problem.

    Michael - 2016-03-18
    Hi, I am using the Wordpress solutions for Traditional Analytics that Matt provided and not sure if I am doing it correctly. Do I add the PHP to my functions and then the two other javascripts to the footer page above the </body> tag? THIS IN FUNCTIONS: <? function get_the_user_ip() { if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { //check ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { //to check ip is pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return apply_filters( 'wpb_get_ip', $ip ); } add_shortcode('show_ip', 'get_the_user_ip'); add_action('wp_head', 'add_google_analytics'); function add_google_analytics() { ?> THESE IN FOOTER.PHP <script type='text/javascript'> _gaq.push(['_setCustomVar', 1, 'IP', '<?=$ip;?>', 1]); </script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//','ga'); ga('create', 'UA-XXXXXXXX-1', 'auto'); ga('send', 'pageview', { 'dimension1': '<?=get_the_user_ip();?>' }); </script>

    Daniel Piñero - 2016-03-18
    The code, as written by Matt, is stand-alone. You shouldn't add anything before or after that code. First, you should remove the Google Analytics code in headers.php, and only insert Matt's code in the functions.php file.

    Akhil - 2016-02-22
    Hello, great post! Is this, what we are doing with codes, permitted by Google's policies ? Because no one would like to trick Google for getting the IP Addresses. Thanks

    Daniel Piñero - 2016-02-22
    I think this is open to interpretation, depending on who you read. Google doesn't allow storing personal data. IP addresses may or not be considered personal data, depending on the lawyer you talk to. It's very open to interpretation, but I don't think Google will be prosecuting you for this. They have better things to do. I've seen worst things being stored in some Google Analytics accounts, such as phone numbers.

    Matt Holovach - 2016-02-21
    I was able to get the code to work in Wordpress. First disable your current Google Analytics plug-in or delete it if you manually included it in the headers.php file. Then add the following code into your functions.php file. I needed to get this to work for a Wordpress client so I futzed with it for hours but I got it to work. So hopefully it will work for you. function get_the_user_ip() { if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { //check ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { //to check ip is pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return apply_filters( 'wpb_get_ip', $ip ); } add_shortcode('show_ip', 'get_the_user_ip'); add_action('wp_head', 'add_google_analytics'); function add_google_analytics() { ?> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//','ga'); ga('create', 'UA-73954742-1', 'auto'); ga('send', 'pageview', { 'dimension1': '<?php echo get_the_user_ip();?>' }); </script>

    Daniel Piñero - 2016-02-21
    Thanks Matt. I've just added your solution to the main post, hoping it helps people who are using Wordpress.

    Tomas - 2016-02-21
    Hi Daniel, I implemented it on Universal Analytics in Wordpress bud I dont see any IP, instead there is just this part of code: <?=$ip;?>. Do you have any ideal why is that? Thanks a lot Daniel.

    Tomas - 2016-02-21
    Hi Daniel, here is website´s URL, if that helps. Thank you.

    Daniel Piñero - 2016-02-21
    Hi. I don't see any Google Analytics code in your website. Please let me know.

    Rockinfreakshow - 2016-02-21
    Hi Daniel, I tried using the same to my Tumblr and I got the same <?=$ip;?> value in my IPs report. It seems to be a common prob for evry one.. Can you help out with exactly what needs to be fixed. thnx in advance:)

    Daniel Piñero - 2016-02-21
    I don't think this will work in Tumblr. However, let me know the URL address so I can check it out.

    Alan W - 2016-01-26
    Hi Daniel, The 'dimension1': '<?=$ip;?>' code did not return the ip number under Drupal 7. However, the following code did the trick: 'dimension1': '<?echo $_SERVER[&quotREMOTE_ADDR&quot];?>' Thanks for the great tip.

    Don - 2016-01-26
    Hi Daniel, Does this still work with the current google analytics (2016)? I had to update my site code from them so just curious. Can the php go anywhere in the page before the analytics code? Thanks.

    Daniel Piñero - 2016-01-26
    Yes, it will work with current Google Analytics. Just use the Universal Analytics segment. And yes, the PHP can go anywhere before the Analytics code.

    Achmad Rizal Muttaqin - 2015-12-24
    Hi Daniel, Thanks for this great post. I just followed your guide on my project. How can I see if it works? And I dont see any value in custom reporting. Would you please check our website? and please let me know what is the problem.

    Daniel Piñero - 2015-12-24
    Are you maybe using Universal Analytics?

    Leo Asafrana - 2015-12-24
    Hello Daniel; Great information. Working fine. It is one of the best tutorial to integrate and get IP address details! Thanks.

    Rahul - 2015-12-17
    Hi Daniel, Thanks for this great post. I am able to integrate all this into my HTML webpage. My html page is parsing the php code successfully however, I cannot see any value for my custom dimension. Do you have any idea what should I be checking next?

    Daniel Piñero - 2015-12-17
    I think you should be checking if you have actually created a custom dimension inside Google Analytics. If you need direct help, please let me know your website's address.

    Neil - 2015-10-20
    David, Thanks for posting this. I am using square space and Universal Analytics. both the php and script get pasted into every page? my site is Really appreciate it!

    Daniel Piñero - 2015-10-20
    Only one script is necessary. PHP is preferable, but if you're site is not able to run PHP, then you can try with Javascript.

    David - 2015-09-20
    Hi Daniel- Again, thanks for taking the time to help out with this! My server does run php successfully for other pages, but I did as you suggested and ran the javascript. Same result! Does running the java still require the snippet for traditional or universal analytics? I'm using traditional. And does retrieving the java results involve the same customization? Thanks! David

    Daniel Piñero - 2015-09-20
    Hello David. Can you try to do a simple "echo" in your test webpage, and see if it works? My first impression is that PHP is not running well on that webpage. Maybe you have to change the .htm extension.

    David - 2015-09-13
    Hi Daniel- This looks great! Unfortunately I am also only getting <?=$ip;?>. I'm sure it is probably something I'm doing, but any guidance would be helpful. I'm just testing on a single page for now:

    Daniel Piñero - 2015-09-13
    Hi David. Seems like your webpage is not executing the PHP code. Can your server run PHP? If not, you can always try the Javascript solution I propose in this same post.

    Lia - 2015-08-28
    Hello Daniel, Thank you for your posting. I followed your comments but IP dimension only shows <?=ip;?> Would you please check our website? and please let me know what is the problem.

    Daniel Piñero - 2015-09-04
    Hello Lia. The code has had a bug for a few days. Please just add the $ symbol before ip, inside the Google Analytics code, or copy and paste the code inside the post again.

    RK - 2015-08-28
    Hi Daniel great post thanks. Just a quick one, could this be used with Google Tag Manager? Thanks

    Daniel Piñero - 2015-08-28
    Hi RK. It seems like you should create a new variable. I know very little about Google Tag Manager, so I'd suggest you to check this: Seems like, in this case, it would be easier to apply the Javascript solution.

    Sean Zee - 2015-08-19
    Hello Daniel, My web pages are .htm extension and I was wondering if the PHP code that goes before the traditional Google Analytics code will run as intended in a htm or html file.

    Daniel Piñero - 2015-08-20
    Hello Sean. The PHP code will run only in servers with PHP. However, I've just updated this post with a Javascript alternative. Please check it out.

    Matt Holovach - 2015-07-27
    Daniel, Thank you very much for your article. I have used it effectively for a little while. My main use is to exclude people who have clicked on my PPC ads multiple times and it has helped tremendously for my clients. My only issue is it works for Desktop and Tablet devices, but not for mobile devices. This might be on me, because I don't utilize responsive design I utilize a tablet/ desktop site and a separate mobile site. Both sites utilize the same code above. But for some reason the mobile clicks aren't being tracked in my reports.

    Daniel Piñero - 2015-07-27
    Hello Matt. It's very interesting. I think this may be related to some problem with Javascript. Can you let me know one of the mobile websites where you have included this code?

    Baumgart - 2015-07-24
    I attempted to use your code to collect IP address data, but the data is not being reported. Visiting IP addresses are accumulating, but the report only reads &quot<?=$ip;?>&quot. I visited some other sites that are using your code, but each of them used your code differently. Any help with this problem would be greatly appreciated.

    Daniel Piñero - 2015-07-24
    Please let me know your website's URL.

    Fabio - 2015-07-24
    Hi! I would like to use your code however I would need a clarification. I have google analytics universal and following your instructions, eventually I am supposed to replace ga('send', 'pageview'); in the Analytics main code. What do you mean exactly by Analytics main code? is it the google code part of my foot website? Thanks in advance.

    Daniel Piñero - 2015-07-24
    Yes, that's it.

    Anouk Kolkman - 2015-06-21
    Thanks for the code! Really useful!! I've got a question: Do you need to change &quotdimension1&quot to the name of the dimension (like IP), or is dimension1 also working? I'm working on the site: . The code is right before the body tag.

    Daniel Piñero - 2015-06-21
    Hi Anouk. You don't need to change "dimension1", it has to be like that. But you have to confirm that you're using the right code for your analytics type: universal or traditional. I'd rather insert the code in the header section.

    Mark - 2015-06-03
    .Thank you ! Like the other user, must be doing something wrong as in Google universal analytics, the IP listed is as &quot<?=$ip;?>&quot. If this a common problem? Would you be able to suggest some solutions that could help, without me having to submit some code? Kind regards Mark

    Daniel Piñero - 2015-06-03
    Please let me know the URL.

    Ori Olapade - 2015-06-03
    I am getting counts but the IP add is listed as <?=$ip;?> Created a custom dimension IP with ID 5.Here's my code for IP capture: ga('send', 'pageview', { 'dimension5': '<?=$ip;?>' }); Please help

    Daniel Piñero - 2015-06-03
    Please let me know the URL.

    Matt - 2015-04-17
    This is against Google's TOS (see section 7 about using personal information that google or a third party could use to identify an individual)

    Ariawan - 2015-03-13
    why my report doesn't show anything? is it need time for process?

    Daniel Piñero - 2015-03-13
    Hi Ariawan. Yes, you may need some extra time, maybe 2 hours. If you still don't see anything, please let me know via e-mail.

    Larry - 2015-03-06
    This is great information. Thank you ! Like the other user, I must be doing something wrong as in Google analytics, I am getting counts, but the IP addy is listed as &quot<?=$ip;?>&quot. Thoughts? Thanks

    Daniel Piñero - 2015-03-06
    Hi Larry. Please send an email to me with the details of your URL. Kind Regards, Daniel.

    Michael - 2015-02-20
    I tried to implement it on my site. I built it in google sites Should it work ok as well? If do how much time it should take till I will see the data in the google analytics?

    Daniel Piñero - 2015-02-21
    I've already talked to Michael about this issue. For all people reading this: you need to run PHP, you cannot use a platform like Google Sites.

    Gopi - 2014-12-31
    Great post. It's working! Really helpful.

    Daniel Piñero - 2014-12-31
    Thanks for your comment! Have a great 2015!

    Bolas Intelli - 2014-12-22
    I had tried this recently for Working fine. It is one of the best tutorial to integrate and get IP address details

    Raz - 2014-12-19
    Thanks for this solution. I implemented it with Universal Analytics. It shows the visitors, but instead of IP address it shows <?=$ip;?>. Was this part of the code supposed to be replaced with something else?

    Daniel Piñero - 2014-12-19
    Hi Raz. Can you please send an email to me, specifying the exact outcome you're getting? Tags are usually stripped from the comments.

    toufic zaarour - 2014-11-21
    hello there! nice post! i have a django site (Python) that i have tried to implement your code with. i have a question: will i see the IP addresses immediately or will it take some time to appear in my custom report in Google analytics?

    Daniel Piñero - 2014-11-21
    It should take a couple of hours to appear. The only thing is that this code is for PHP. I'm not totally sure this will work on Python.

    Lokal - 2014-11-12
    Is this info still valid?

    Daniel Piñero - 2014-11-12
    Yes, it's still valid.

    RR Nair - 2014-09-04
    Thanks for the reply Daniel. But still I am not getting any data on my report. Adding the javascript and php codes below as used in website. Am I getting it wrong? <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//','ga'); ga('create', 'UA-19859118-1', 'auto'); ga('require', 'displayfeatures'); ga('send', 'pageview'); </script> <?php function getRealIpAddr() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip=$_SERVER['HTTP_CLIENT_IP'];} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];} else { $ip=$_SERVER['REMOTE_ADDR'];} return $ip;} $ip = getRealIpAddr(); ?> <script type='text/javascript'> var dimensionValue = '<?=$ip;?>'; ga('set', 'dimension1', dimensionValue); </script> </head>

    Daniel Piñero - 2014-09-04
    Hi. Unfortunately, these replies don't admit code with tags. Please send them to me via email, as well as the URL of the website. I'll wait for your answer. Kind regards.

    RR Nair - 2014-08-05
    I have added the code to site and it is retrieving the IP. I have configured the Custom Dimension and has used the Universal Analytics code, but this is not showing any data on my report. Any ideas of what might have gone wrong?

    Daniel Piñero - 2014-08-05
    Hi. The way Custom Reports are done is slightly different. I just updated this post. Please try to change your Custom Report and let me know if it works.

    Ryel - 2014-07-28
    Hi, Yes I did follow all the steps for new GA as I'm using new GA. Is &quotdimension1&quot you mean the custom dimension name too? I do not have other custom dimension just this one so it just dimension1 too right. What's correct type of scope for the custom dimension? Hit?

    Daniel Piñero - 2014-07-28
    Can you pass me the resulting Javascript, as you see it in the source code? Let's see if I can help.

    Ryel - 2014-07-28
    Hi, I have tried on this but somehow it's not recording any IP address. Report is still &quotThere is no data for this view.&quot after few days. IP is seen on the code if I view the source of the page but I think it's not sending data to analytics. What could be the problem? What type of scope for the Custom Dimension? I set it to session or user but still the same. Does this still work on latest GA?

    Daniel Piñero - 2014-07-28
    Hi Ryel. If you're using the latest GA (Universal Analytics), you have to: 1) create a new dimension manually inside Google Analytics, as explained here:; 2) use the code I give under "Universal Analytics". You should change "dimension1" to the exact dimension number you just created.

    Denny - 2014-07-20
    I'm not seeing &quotvisits&quot in the metrics dropdown. What would be a substitute?

    Daniel Piñero - 2014-07-20
    "Sessions" is the new definition for "Visits". Kind regards.

    Subu - 2014-03-07
    i have implemented the code and let see how it works. Thanks for the PHP Code