Retrieve client IP address and geolocation in CloudPages

There are many reasons for checking the client IP address, most common include tracking and personalization. What can you find out about the visitors of a webpage from their IP address? You can identify their ISP, figure out approximately where they’re located and see how often they (or someone else sharing their router) visit your website.

In the context of CloudPages, we most often see IP tracking for personalization purposes. By identifying the visitor’s location, you can automatically display text in their local language and control what kind of content they see.

Identify client IP using AMPscript

The X-Forwarded-For (XFF) HTTP header is a standard header for identifying the originating IP address of a client connecting to a web server. You can easily access this header by using the AMPscript HTTPRequestHeader function, which will return a specified header from an HTTP request. Here’s how to retrieve an IP address on a CloudPage using AMPscript:

Your IP: %%=HTTPRequestHeader("X-Forwarded-For")=%%

Identify client IP using SSJS

There are dedicated Server-Side JavaScript HTTP Properties Functions, that allow you to retrieve various types of HTTP Request object properties and platform application values. The client browser passes this information to the server during an HTTP interaction, so this object contains information regarding the browser and session. One of the available properties to use with the HTTP Request object is ClientIP, which returns the IP address of the requesting client as a string value. Here’s how to retrieve an IP address on a CloudPage using SSJS:

<script type="javascript" runat="server">
Platform.Load("core","1");
var ip = Platform.Request.ClientIP();
Write("Your IP: " + ip);
</script>
view raw clientIP.html hosted with ❤ by GitHub

This is the preferred way to retrieve the client IP, as Request.ClientIP() is a dedicated and supported function, while using the XFF HTTP header proved to be unreliable in the past.

HTTP Properties Functions allow you to retrieve other useful information, for example, browser metadata or the URL of the referring web address.

Discover the precise physical location of a given IP address

The functionality of identifying a physical location of a given IP address requires using a third party API service. There are many IP Geolocation API providers and most of them have a free plan available, as well as paid plans for bigger enterprises. My preferred one is ipify.org, which allows you to run up to 1000 queries per month for free. Once you register, you will obtain your personal apiKey, that will be used for making the calls:

<script type="javascript" runat="server">
Platform.Load("core","1");
var ip = Platform.Request.ClientIP();
var apiKey = '' //insert apiKey obtained from https://geo.ipify.org/subscriptions
var url = 'https://geo.ipify.org/api/v1?apiKey=&#39;;
var response = HTTP.Get(url + apiKey + ip);
Write("response.Status: " + response.Status + '<br />');
Write("response.Content: " + response.Content);
</script>

The response will contain information about the country, region, city, latitude, longitude, postal code, timezone and GeoNames Id. Additionally, it will also show autonomous system (AS) info if available.

{
"ip": "111.100.64.10",
"location": {
"country": "JP",
"region": "Saitama",
"city": "Saitama",
"lat": 35.867,
"lng": 139.65,
"postalCode": "337-0042",
"timezone": "+09:00",
"geonameId": 6940394
},
"as": {
"asn": 2516,
"name": "KDDI",
"route": "111.100.0.0/16",
"domain": "http://www.kddi.com/english/index.html",
"type": "NSP"
},
"isp": "Kddi Corporation",
"connectionType": "cable"
}

Click here to see this script in action.

As a side-note, remember that under GDPR, IP addresses are considered personal data. Tracking the IPs of your EEA based users without their consent falls under the rules of GDPR.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s