Published: 26 June 2017

The IP Geolocation service provides information about the physical location of website visitors when you use Sitecore DMS or xDB.

When working with this function you need to be careful NOT to this call directly:

var geoInfo = LookupManager.GetInformationByIp(clientIp.ToString());

This code always requests information directly from the service by passing the IP Information in the memory cache and in the database, which may lead to excessive charges for the service usage.

When IP information is requested using Sitecore, the following steps are performed:

  • Lookup of IP information in the memory cache;
  • Lookup of IP information in the database;
  • Request to the Geolocation service.

Therefore, you always need to check whether the correct IP information is stored in the Analytics database:

Sitecore 7.5 - Sitecore 8.x: the "GeoIps" collection, Collection database (MongoDB)

Sitecore 6.4.1 - Sitecore 7.2: the "GeoIps" table, Analytics database (SQL)

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 for the geolocation to work. After all the setup is done, all we need is the proper code for benefiting from the gathered data. So you would approach this using something like this; 

//Get user Geo Location based on IP address (we are using an ExtensionMethod here)
var clientIp = new HttpRequestWrapper(HttpContext.Current.Request).GetClientIpAddress();

Then using that IP address you can do something like this, using GeoIPManager in Sitecore.Analytics.Lookups

if (clientIp != null)

{

    var geoIpOptions = new GeoIpOptions

    {

        Ip = clientIp,

        MillisecondsTimeout = 1000,

        Id = GeoIpManager.IpHashProvider.ComputeGuid(clientIp)

    };

    var geoIpResult = GeoIpManager.GetGeoIpData(geoIpOptions); 

    return geoIpResult;

}

The geoIpResult.GeoIpData object is of type WhoIsInformation and contains a lot interesting data like country, city, region, latitude, longitude etc.

 

Lee Swainsbury

Server-Side Developer

READ MORE FROM LEE SWAINSBURY

Page Name: {% PageName %}

Page Template: {% PageTemplate %}

CampaignID: {% AgentReferrer.ID %}

CampaignName: {% AgentReferrer.Name %}

CampaignPhone: {% AgentReferrer.Phone %}

Item Location: {% PageLocation %}

Search Session Exists: False