Thursday, May 12, 2016

Caution Getting GeoIp Cache Data from Sitecore.Analytics.Lookups.GeoIpManager

UPDATE 6/29/2016: DON'T USE THIS CODE OR THE OTHER IP LOOKUP CODE
After running xDB for 3 weeks in an active environment, my GeoIps collection in Mongo grew to a ridiculously large size (17GB w/ 26 million documents).  This was due to my suggestion below about changing the .ID value before doing a lookup.  It was adding that entry as a new value in the MongoDB.  I think the function Sitecore.Analytics.Lookups.GeoIpManager.GetGeoIpData() is only intended to be used by new IP addresses (ones not associated with a contact/visit yet) and for looking up the ID that links GeoIp data with another xDb record. I advise not following the suggestion to use this function as a "cache lookup".

The Sitecore xDB will cache IP address information for you, so you can quickly load the cached GeoIp information from your local analytics database instead of costing you a lookup from your GeoIp service.


This worked great while I was testing one IP address at a time.  When I ran some unit tests against this code, my lookups were not validating correctly.  The IP address that I had a known country of origin of was not matching that same country data from the analytics lookup data.  It was finding cached IP information, but I was only getting the country value from the very first lookup (or probably whatever lookup happened within 1 second ago, because every now and then 1 test would succeed and 5-7 would fail, then another one would succeed).

The geoIpOptions.MillisecondsTimeout property appears to be caching the cached data lookup.  I'm not sure how this is useful, unless you planned on doing a bunch of lookups of the same IP address, but then why would you perform that lookup since you probably could cache that information elsewhere?  Anyways, I'll assume I am overlooking a logical reason for that.

To fix my problem, where I could perform multiple calls to the Sitecore.Analytics.Lookups.GeoIpManager.GetGeoIpData(geoIpOptions) function and get unique results with each call was to set another property on the passed-in geoIpOptions object.  There is a property called geoIpOptions.Id.  It defaults to Guid.Empty.  So whatever logic is happening in that call ends up returning the same cached value for each of my lookups, even though the geoIpOptions.Ip value was different each time.

Default the Id value to a unique Guid.NewGuid() each time and the cached analytics lookup now works great.

Sitecore.Analytics.Lookups.GeoIpOptions geoIpOptions =
         new Sitecore.Analytics.Lookups.GeoIpOptions();
geoIpOptions.Id = Guid.NewGuid();
geoIpOptions.Ip = netAddress;


2 comments:

  1. See this: https://cristinadragos.wordpress.com/2016/11/04/sitecore-8-1-geolocation-how-to-detect-country-by-ip/

    "etting the Id like on the highlighted row was suggested to me by Sitecore Support and, by reading other articles on this topic, I came to the conclusion that this is what ensures a proper usage of the Sitecore Analytics data. Before having this line, the results were not accurate, and it had to do with the caching."

    ReplyDelete
    Replies
    1. Thanks for the tip! Setting the ID guid to the same value for the same IP would fix the issue I was seeing, where my xDB was filling up. Glad to know that this function exists... it definitely will come in handy.

      Delete