Testing Geo Localisation in Sitecore

Sitecore’s DMS allows for content to be delivered to the user based on a number of different rules. One of the most common being based on location, and to test your DMS personalisation across different locations it is imperative to programmatically¬†set your Sitecore location. This article explains how change your location within Sitecore.

So far I have used this in Sitecore 6.5, 6.6, 7.0, 7.1 and 7.2. And it is assumed that Sitecore Analytics are installed!

The Sitecore DMS by default determines your location by doing an IP lookup using a Maxmind GeoIP lookup service. So to test your DMS in a specific location you need to change your IP Address a specific point in the Sitecore Pipeline. Follow these steps:

Create a new Sitecore Process class, and modify the IP as desired, see below:

namespace coralweb.blog
{
  using System.Net;
  using Sitecore.Analytics;
  using Sitecore.Analytics.Pipelines.StartTracking;

public class OverrideIPAddress
{
   const string NewTestIP = "66.102.15.255"; // or your desired ip, maybe from put this is config

   public void Process(StartTrackingArgs args)
   {

     if (Tracker.CurrentVisit != null)
     {
       IPAddress address = new IPAddress(Tracker.CurrentVisit.Ip);

       if (Tracker.CurrentVisit.GeoIp == null || address.ToString() == "0.0.0.0" || address.ToString() == "127.0.0.1")
       {
         byte[] spoofIP = IPAddress.Parse(NewTestIP).GetAddressBytes();
         Tracker.CurrentVisit.GeoIp = Tracker.Visitor.DataContext.GetGeoIp(spoofIP);
       }
    }
}
}
}

Locate the config file ~/App_config/Includes/Sitecore.Aanlytics.config and add your new process, setup in step 1, after the InitializeTracker processor

<startTracking>
 <processor type="Sitecore.Analytics.Pipelines.StartTracking.RaiseStartTracking,Sitecore.Analytics" />
 <processor type="Sitecore.Analytics.Pipelines.StartTracking.InitializeTracker,Sitecore.Analytics" />
 <processor type="coralweb.blog.OverrideIPAddress, coralweb.blog" />

 <processor type="Sitecore.Analytics.Pipelines.StartTracking.UpdateGeoIpData,Sitecore.Analytics" />

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>