Geo IP API lookup provider for Sitecore

As an alternative to the geo IP lookup provider that ships with Sitecore I have created my own provider using IP-API which is a free web service a while back and I have used it already for a couple years.

IP-API offers free lookup’s using their web services for up to 150 requests per minute. That means it will be good enough for your Sitecore setup if you have 150 or less new user sessions created per minute as there is some caching implemented that will ensure that each IP look up will only be done once. The caching is using the Sitecore built-in GeoIpCacheManager.GeoIpCache.

There is also a paid subscription for 160 Euro per year if you want unlimited amount of lookup’s which is a good value for money.

The IP-API based lookup provider is now available on Github as well as a Sitecore module on the market place.

If you want to create your own lookup provider you can start by creating a new class that derives from Sitecore.Analytics.Lookups.LookupProviderBase. Don’t forget that you also need to add a project reference to the Sitecore.Analytics library. For that I am using the Sitecore nuget server which is pretty cool.

Then you can use your own logic to lookup geo location information based on IP address by overriding theĀ GetInformationByIp(string ip) from the base class. Don’t forget to use the caching provided by Sitecore so you will not lookup the same IP address multiple times:

    GeoIpCache geoIpCache = GeoIpCacheManager.GeoIpCache;
    WhoIsInformation information = geoIpCache.Get(ip);
    if (information != null)
    {
        return information;
    }

Also, make sure you handle errors gracefully in case your logic fails:

    try
    {
       // Your logic to lookup
    }
    catch (Exception ex)
    {
       HandleLookupErrorArgs handleLookupErrorArgs = new HandleLookupErrorArgs(ex);
       CorePipeline.Run("ces.geoIp.handleLookupError", handleLookupErrorArgs);
       if (handleLookupErrorArgs.Fallback == null)
       {
           throw;
       }

       if (handleLookupErrorArgs.CacheFallback)
       {
           geoIpCache.Add(ip, handleLookupErrorArgs.Fallback);
       }

       return handleLookupErrorArgs.Fallback;
   }

Last but not least you will also need to create a Sitecore config patch to replace the built-in Sitecore lookup provider which can be done like this:

  <sitecore>
    <lookupManager defaultProvider="default">
      <providers>
        <add>
          <patch:attribute name="type">Swissworx.Modules.Analytics.Lookups.IpApi.LookupProvider, Swissworx.Modules.Analytics.Lookups.IpApi</patch:attribute>
        </add>
      </providers>
    </lookupManager>
  </sitecore>

Leave a Reply

Your email address will not be published. Required fields are marked *