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.
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:
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：
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：
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:
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:
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.
Open developer tools in preview and look at cookies:
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:
Find cd1, this is the first custom dimension, you can see the IP behind.
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.