Access Geolocation service programmatically

946 Views Asked by At

I am trying to use Sitecore 8.1's Geolocation service to detect customer's location and serve correct site context.

Is there an API from them that I can use programmatically? Any sample code?

4

There are 4 best solutions below

0
On BEST ANSWER

You really shouldn't. The service isn't meant to be called synchronously in your page requests - holding up the site page delivery until you have an answer. It simply won't perform very well.

Sitecore Marketplace offers the GeoLite Resolver (Github link: https://github.com/adoprog/Sitecore-Geo-Lite-Resolver), and while this is fairly old it can still be included and compiled in your Sitecore 8 projects just fine.

It basically uses an offline light copy of the MaxMind database - good enough to solve country resolution (but not city) - and can respond very fast (<10ms) to a lookup.

0
On

You will need the Sitecore.Analytics.Lookups namespace. Sitecore.Analytics.Lookups.GeoIpManager.GetGeoIpData should get you started.

Remember that there are already some rules and conditions available in the box based on GeoIP data. It's an option to (re-)use those as well.

0
On

The Sitecore Marketplace also has the MaxMind GeoIP2 Custom Lookup Provider module as well which uses the MaxMind GeoIP2 City Database API without requiring xDB https://marketplace.sitecore.net/en/Modules/M/MaxMind_GeoIP2_CustomLookupProvider.aspx

0
On

Above is correct, you should avoid calling GeoManager, which is given as an example in many places, but messes with the analytics. You should be calling LookupManager.GetInformationByIP(ip);, which resolves the IP and uses the GeoIP cache.

We ran into this same issue when we were trying to leverage the Geo IP data in a Sitecore pipeline, as the GeoData is not yet available at that point by default.

Documented this at: http://edgebytes.com/2016/09/08/calling-sitecore-geo-ip-location-service/