Google Tag Manager Practical Guide:Get User’s IP | BCS Google Tag Manager Practical Guide:Get User’s IP | BCSBCS

Google Tag Manager Practical Guide:Get User’s IP

Google Tag Manager BCS 4 years ago (2019-12-18) 1991 Views 0 Comments

As we all know, Google Analytics does not provide the IP field, and it is expressly prohibited to upload information that can identify users. But many users want to be able to obtain the IP dimension for analysis. It is actually achievable, but do not upload the IP in plain text and do some decoration, it can still be implemented. Another useful function of obtaining IP is that it can be used for the analysis of spam traffic. For example, if there is a lot of access to an IP and the bounce rate is high, then this can be judged to be the amount of brushing.

Principle: Use JS to get the user’s IP information through a third-party API query, then write the IP encryption into a cookie, and then read the first-party cookie through GTM to store it with a custom dimension.

Setup Process

1、Write IP to cookie

In the GTM, query the API through a custom html, and then add .741 to the obtained IP and write the IP into the cookie. The cookie name is IPnum. code show as below:

<span style="font-size: 12pt;"><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript">
	var cookieName= "ipnum";
	var cookieName2= "ipci";
    var ipnumber  =returnCitySN["cip"]+".741";
    var ipcity  = returnCitySN['cname'];
    var cookiePath  = "/";
 
    
    var expirationTime = 172800*15;                           
    expirationTime = expirationTime * 1000;
 
    var date = new Date();
    var dateTimeNow = date.getTime();
    date.setTime(dateTimeNow + expirationTime);
 
    var expirationTime = date.toUTCString();
 
    document.cookie = cookieName+"="+ipnumber+"; expires="+expirationTime+"; path="+cookiePath;
    document.cookie = cookieName2+"="+ipcity+"; expires="+expirationTime+"; path="+cookiePath; 
</script>
</span>

 

Click “Tags” → “New” → “Select a tag type to set” → “Custom HTML” in the GTM, and then copy the code into it  select All Pages for the trigger, and complete The configuration is shown in Figure:

Google Tag Manager Practical Guide:Get User's IP

2、Get IP in variables

Use the first-party cookie to obtain the IP in the variable, Click Variables>New >1st-Party Cookie  in the GTM, and then configure as shown in Figure:

Google Tag Manager Practical Guide:Get User's IP

 

3、Create custom dimensions

Click  Admin >Custom definition >Custom dimension  in Google Analytics to create a new custom dimension to store the IP, and record the corresponding numerical index after the new creation, as shown in the figure:

Google Tag Manager Practical Guide:Get User's IP

Scope selects hit type or session type, but cannot select Users type, because some users are dynamic IP.

4、Configure Google Analytics basic tracking code

We set it through Google Analytics settings, so we need to add the following settings to Google Analytics settings:

Google Tag Manager Practical Guide:Get User's IP

Find the custom dimension, enter 1 for the Index, and the corresponding value is the index value of the custom dimension created in Google Analytics. Select the variable IP Number for the dimension value.

5、Preview

Open developer tools in preview and look at cookies:

Google Tag Manager Practical Guide:Get User's IP

See that the IP has been written into the cookie.

Let’s look for the data sent to google analytics and find the collect field. This is the data sent to Google Analytics:

Google Tag Manager Practical Guide:Get User's IP

 

Find cd1, this is the first custom dimension, you can see the IP behind.

Succeeded.

 

 

Warning: Google Analytics is not allowed to send PII information. If you want to send it, please encrypt it first. This article is for demonstration purposes only. This blog does not collect user IP.


If you don't understand, You can leave a comment below.
Like (0)
Post my comment
Cancel comment
Expression Bold Strikethrough Center Italic

Hi, you need to fill in your nickname and email!

  • Nickname (required)
  • Email (required)
  • Website