Sitecore Geolocation Service API

 Sitecore provides location details of the user using Sitecore Geolocation API. It gives the detail of the Time zone, City, IP Address, region, Country, Post Code, Longitude and Latitude, etc. It is advantageous for marketers or developers in terms of visitor information, tracking, and personalization.  Using this information, you can apply the personalization rules for different geographical locations, tracking goals and page events.

In this blog, I will let you know about How to enable the Sitecore Geolocation Service API and by using this API fetch the details of the user location using an IP Address.

Enable Sitecore Geolocation Service API: First, you need to raise a request to Sitecore to activate the Sitecore IP Geolocation service. For the same, you can refer to Sitecore Documents. See the document: Set up Sitecore IP Geolocation

Retrieve Geolocation Data:

  1. First, you need to make sure that XB and Analytics tracker is working fine in your environment.
  2. Get the IP address from the Current Tracker as mentioned below:
  3.   
    if (Tracker.IsActive == false)
     {
      	Tracker.StartTracking();
     }
    return (Tracker.Current?.Interaction?.Ip != null) ?
    	new IPAddress(Tracker.Current.Interaction.Ip).ToString() : string.Empty;
      
  4. Once you will find the IP address of the user you can fetch the other details of the user. Sitecore provides below methods for this purpose:
    • Sitecore.Analytics.Lookups.LookupManager.GetInformationByIp(…)
    • Sitecore.CES.GeoIp.Core.Lookups.LookupManager.GetWhoIsInformationByIp(...)
    • Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetRequiredService<Sitecore.CES.GeoIp.Core.IGeoIpManager>().GetGeoIpData(...)

Now the question occurs, which one you will use?

Sitecore.Analytics.Lookups.LookupManager.GetInformationByIp(string)

This method is obsolete and deprecated in Sitecore 9.1.0. You can use this the method is mentioned below:

var geolocation = Sitecore.Analytics.Lookups.LookupManager.GetInformationByIp(ipAddress);
if (geolocation != null)
 {
   var city = geolocation.City;
   var postCode = geolocation.PostalCode;
   // other details
 }

Sitecore.CES.GeoIp.Core.Lookups.LookupManager.GetWhoIsInformationByIp(string): 

You can use this method if are working on Sitecore 9+ or 9.2 version as mentioned below:

var geoObject = var geolocation = Sitecore.CES.GeoIp.Core.Lookups.LookupManager.GetWhoIsInformationByIp(ipAddress);
if (geolocation != null)
 {
  var city = geolocation.City;
  var postCode = geolocation.PostalCode;
  // other details
 }

You need to add the below package from NuGet Package Manager to access the above method:

sitecore-geolocation-api

Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetRequiredService<Sitecore.CES.GeoIp.Core.IGeoIpManager>().GetGeoIpData(string): 

If you are working on Sitecore 10+ then you can use this API method.

var geoIpManager = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetRequiredService<Sitecore.CES.GeoIp.Core.IGeoIpManager>();
var geoIpFetchedData = geoIpManager.GetGeoIpData(ipAddress);
if (geoIpFetchedData.Status == GeoIpFetchDataStatus.Fetched && geoIpFetchedData.WhoIsInformation != null)
 {
   var  country = geoIpFetchedData.WhoIsInformation.Country;
   // other details
 }

Note:

  1. Don’t be getting confused about the above three methods, all are internally using the GeoIpManager Service.
  2. GetWhoIsInformationByIp will be removed in a later version of Sitecore but it will be present for backward compatibility. 

Happy Sitecoreing ðŸ˜Š

Comments

Popular posts from this blog

Sitecore Installation Error: Failed to Start Service 'Sitecore Marketing Automation Engine'

Import CSV Data in Sitecore Using PowerShell: Part-3

Sitecore Technology MVP Journey 2022