Sitecore 8.1 Geolocation: How to Detect Country by IP

The detection of a visitor’s IP is a very common requirement and there are many tools out there for it. But working with Sitecore prompts us to always look for the recommended IP detection methods, thus, in the past, we were using the MaxMind database for this. In the latest versions, Sitecore makes it easier for us and comes with an integrated module for IP Geolocation: https://doc.sitecore.net/sitecore_experience_platform/81/setting_up__maintaining/ip_geolocation.

Starting with the Sitecore 8.1 initial release, the IP Geolocation service is automatically installed, and it just needs to be enabled. Because it is strongly related to the xDB, the Analytics and Tracking must be enabled too in order for the geolocation to work. After all the setup is done, all we need is the proper code for benefiting from the gathered data. The purpose of this article is to show how to get the visitor’s country based on the detected IP address.

I have created a simple method that takes the IP address from the Analytics Tracker, see below:

private static IPAddress GetIpAddressFromTracker()
{   
    return Tracker.Current != null 
        && Tracker.Current.Interaction != null 
        && Tracker.Current.Interaction.Ip != null 
        ? new IPAddress(Tracker.Current.Interaction.Ip) : null;
}

Then, I created a method that uses the IP address to access the relevant Analytics data and to get the visitor’s country. The result of this method is the ISO code of the country, which should be enough to work with further on:

public static string GetCountryForCurrentIp()
{
    IPAddress ipaddress = GetIpAddressFromTracker();
    string country = string.Empty;

    if (ipaddress != null)
    {
        var geoIpOptions = new GeoIpOptions
        {
            Ip = ipaddress,
            MillisecondsTimeout = 1000,
            Id = GeoIpManager.IpHashProvider.ComputeGuid(ipaddress)
        };

        var geoIpResult = GeoIpManager.GetGeoIpData(geoIpOptions);

        if (geoIpResult?.GeoIpData != null)
        {
            country = geoIpResult.GeoIpData.Country != null 
                && !geoIpResult.GeoIpData.Country.Equals("N/A") 
                ? geoIpResult.GeoIpData.Country : string.Empty;
        }
    }

    return country;
}

Setting the Id like on the highlighted row was suggested to me by Sitecore Support and, by reading other articles on this topic, I came to the conclusion that this is what ensures a proper usage of the Sitecore Analytics data. Before having this line, the results were not accurate, and it had to do with the caching.

The geoIpResult.GeoIpData object is of type WhoIsInformation and contains a lot of other interesting data like city, region, latitude, longitude, that you can now access in the same manner as the country. Easily!

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s