An Independent Sitecore Learning Resource

Menu

Getting Time Zone from a Sitecore GeoIP Response

Getting Time Zone from a Sitecore GeoIP Response

Author

Brandon Bruno

Published

7/10/2018

Tags

Sitecore's GeoIP service provides great flexibility when implementing location-based personalization. It provides lots of useful data about a user's potential location, including ZIP code, city, state, longitude, and latitude.

A client recently asked if the GeoIP service could return a user's time zone - after all, time zone maps one-to-one to location, so it seems like something that can be standardized and easily available via GeoIP.

Is Time Zone Available Via Sitecore GeoIP?

In short: no.

Returning a time zone based on UTC would be ideal for many scenarios, but there's some conflicting information on whether or not this is possible with Sitecore GeoIP. Sitecore documentation is pretty clear: time zone is available via GeoIP information (read this article from 2018: Getting started with Sitecore IP Geolocation Service). Case closed, right?

The Sitecore community - including this forum discussion - generally agrees that time zone is not a standard field available from the GeoIP service. To verify who was right, I cracked open the Sitecore.CES.GeoIp DLL to see what's going on under the hood. These were Sitecore 9.0.1 DLLs (the latest and greatest as of this writing).

After doing some digging, I learned that Sitecore's GeoIP service returns a JSON string, which is then parsed into a domain object and (typically) attached to the Analytics tracker. I found the code that maps that JSON data to domain models representing GeoIP information (Sitecore.Analytics.Model.WhoIsInformation). That little bit of mapping code:

	
protected virtual WhoIsInformation Map(JObject jsonObject)
{
	Assert.ArgumentNotNull((object) jsonObject, nameof (jsonObject));
	
	return new WhoIsInformation()
	{
		BusinessName = jsonObject["organization"].Value<string>(),
		City = jsonObject["city"].Value<string>(),
		Country = jsonObject["country"].HasValues ? jsonObject["country"][(object) "isoCode"].Value<string>() : (string) null,
		Dns = jsonObject["domain"].Value<string>(),
		Isp = jsonObject["isp"].Value<string>(),
		Latitude = jsonObject["latitude"].Value<double?>(),
		Longitude = jsonObject["longitude"].Value<double?>(),
		MetroCode = jsonObject["metroCode"].Value<string>(),
		PostalCode = jsonObject["postCode"].Value<string>(),
		Region = jsonObject["subDivisionIsoCode"].HasValues ? jsonObject["subDivisionIsoCode"].Last<JToken>().Value<string>() : (string) null
	};
}
	

Sure enough, there is nothing in that mapping code that references time zone. Things are looking bleak. A Google search-and-a-half later, I discovered that a few other folks have looked into this and already discovered that Sitecore's GeoIP service does return a time zone value, but as the above mapping code demonstrates, Sitecore isn't consuming it.

Getting Time Zone From the Raw GeoIP JSON Data

Martin English tackled this very problem and wrote a quick overview on how to capture and save that time zone information from the raw JSON response. I highly recommend reading and studying his approach.

But like most "Does Sitecore do 'X'?" questions, the answer is typical Sitecore: no, it doesn't really do 'X' out of the box, but with a little modification and a pipeline or two, yes, it can do 'X'.

Do you have questions or comments about this post? Find me on Twitter: @SitecoreSpark