How to Set IP Address as a Custom Dimension in GA4

Google Analytics BCS 6 years ago (2019-12-18) 2867 Views 0 Comments

Updated: December 19, 2025

Warning: Google Analytics 4 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 is well known, Google Analytics 4 does not expose a native IP address field.

However, in practice, many users still want to associate IP-related information with GA4 data for analytical or technical purposes.

This can be achieved indirectly by using JavaScript, first-party cookies, and Google Tag Manager (GTM)—while ensuring the data is encrypted and handled carefully.

Solution Overview

The overall approach is as follows:

  • On every page load, use JavaScript to call a third-party API to retrieve the user’s IP address.
  • Encrypt the IP address.
  • Store the encrypted value in a first-party cookie.
  • Read the cookie value in GTM.
  • Send it to GA4 as a user-scoped custom dimension.

 

Setup Process

Step 1 : Custom HTML Tag

In GTM, click 「Tags」——「New」—— 「Choose a tag type to begin setup…」 ——「Custom HTML」 and then configure the tag as follows:How to Set IP Address as a Custom Dimension in GA4

 

Code:


Set the Tag firing priority to 9999 to ensure this tag runs before the GA4 Page View tag, allowing the IP value to be available in time.

 

 

Step 2 : Read the IP from a First-Party Cookie

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

How to Set IP Address as a Custom Dimension in GA4

Set IP City in the same way.

 

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:

How to Set IP Address as a Custom Dimension in GA4

 

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

How to Set IP Address as a Custom Dimension in GA4

 

Step 4 : Preview and Debug

Click 「Preview」 in GTM, enter debug mode:

How to Set IP Address as a Custom Dimension in GA4

Return to Tag Assistant, locate the GA4 configuration or Page View tag, and verify that the IP-related field is being populated correctly.

 

 

Setp 5 : Custom Definitions

To make the dimension available in GA4 reports.

In GA4,click「Admin」——「Custom definitions」——「Create custom dimension」, configure the dimension as follows::

How to Set IP Address as a Custom Dimension in GA4

 

  • Scope: User, this means user level custom dimensions
  • User Property: ip (This must exactly match the Property Name defined in GTM.)

Repeat the same process for User City, if applicable.

Once configured, the data will become available in GA4 reports starting 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