Get User’s IP in Google Analytics 4 | BCS Get User’s IP in Google Analytics 4 | BCSBCS

Get User’s IP in Google Analytics 4

Google Analytics BCS 5 years ago (2019-12-18) 2448 Views 0 Comments

Update time: June 14, 2024

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.

As we all know, Google Analytics 4 does not provide the IP field, but many users hope that Google Analytics 4 can obtain the user IP, which can be achieved.

Solution

Every time the page is loaded, use js to query the third-party API to obtain the user’s IP, encrypt the IP and write it into a cookie, then read the first-party cookie through GTM and store it in a custom dimension.

 

Setup Process

Step 1 : Custom HTML

In GTM, click 「Tags」——「New」—— 「Choose a tag type to begin setup…」 ——「Custom HTML」 and then make the following configuration:Get User's IP in Google Analytics 4

 

Code:

<script type="application/javascript">
   $.get("https://ipinfo.io", function(response) {
             console.log("My public IP address is: ", response.ip);
          
     var cookieName= "ipnum";
     var cookieName2= "ipcity";
    var ipnumber  =response.ip;
     var ipcity  =response.city;
    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;
        }, "json")
</script>

Tag firing priority is set to 9999 to ensure that this Tags executes faster than Pageview and obtains the IP.

Step 2 : 1st Party Cookie to get IP

In GTM, click 「Variable」——「New」—— 「Choose a variable type to begin setup…」 ——「1st Party Cookie」 and then make the following configuration:

Get User's IP in Google Analytics 4

Set IP City in the same way:

Get User's IP in Google Analytics 4

 

Step 3 : Set User Properties

In GTM, click 「Variable」——「New」—— 「Choose a variable type to begin setup…」 ——「Google Tag: Event Settings」 and then make the following configuration:

Get User's IP in Google Analytics 4

 

Then add the Common Event Parameter to the Shared event settings of Pageview Tracking in GA4:

Get User's IP in Google Analytics 4

 

Step 4 : Preview

Click 「Preview」 in GTM, enter the debugging state, return to Tag Assistant, find the basic code configuration of GA4, and see if the IP field is accurately obtained:

Get User's IP in Google Analytics 4

 

 

 

Setp 5 : Custom Definitions

The next step is to register this dimension in GA4 so that this dimension can be used in GA4.

In GA4,click「Admin」——「Custom definitions」——「Create custom dimension」, then do the following configuration:

Get User's IP in Google Analytics 4

 

  • Scope: To select user level, this means user level custom dimensions
  • User Property: fill in the ip, that is, “Property Name” in GTM must be consistent.

Set up User City in the same way. Once it is set up, you can see the data in GA4 the next day.


If you don't understand, You can leave a comment below.
Like (1)
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