Geo IP lookup API in Ektron

A useful feature of Ektron CMS is the ability to target content at specific people based on their geographic location. This is commonly done within the Targeted Content widget.

However there are often times when you need the ability to do this in code outside of a Pagebuilder widget. Fortunately the Ektron API provides you with a simple way of determining where the user is coming from:

Ektron.Cms.UserLocationData userLocationInfo = Ektron.Cms.UserContext.GetLocationInfo("192.168.1.1");

if (userLocationInfo.CountryCode == "GB")
{
// User is from Great Britain
}

The simple example given above determines if a user resides in Great Britain or not by checking the CountryCode property of the UserLocationData object.  The GetLocationInfo() method takes a string parameter that holds the IP address that you want to look up.  Typically you would get this address by calling something like Request.UserHostAddress.

There are many other properties available from the UserLocationData object such as:

  • CountryName
  • City
  • Latitude
  • Longitude
  • Postalcode

The CMS makes use of three data files that are stored in your App_Data folder to get this information.  Those files are:

  • GeoIPDomain.dat
  • GeoIPOrg.dat
  • GeoLiteCity.dat

These data files are from an organisation called MaxMind so it is possible to update the data files from there and also to purchase a more complete file that contains advanced information (like what company a user works for).

3 comments

  1. Hi there, I have been looking for an answer to why the GetLocationInfo returns null even though I am passing in a valid IP address. Is there an additional reference or using statement that I need to add to my code behind? thanks!

  2. How are you obtaining the IP address? I did have this problem once before and it turned out to be that the server I was running on was not able to determine the correct external IP address. I think it was due to a proxy server or load balancer. Have you tried hard coding the IP address as a test? Please note it does need to be an external IP, not an internal IP like 192.168.x.x.

  3. Hi Rob, Yes indeed that is exactly what was happening, I was going through a VPN connection and getting a 192.168.x.x IP address. I popped in a real IP address and am seeing results now. Much thanks for your help!

Leave a comment