In this article we present a technique to test Sitecore IP Geolocation using C#, Visual Studio Test Framework, and PhantomJS. The test avoids the use of a VPN service and can be used to populate the Sitecore Analytics database with test data.


1 - Sitecore IP Geolocation service must be active. To verify that it is working:

(a) Load any page in Experience Editor and click on the Home ribbon Other -> Explore

(b) Click on the left arrow > Visitor Information - GEO IP and click on TestIP Geolocation Service Connectivity:

2 - Sitecore xDB and the Sitecore Analytics tools should be properly configured, so you can see the visits in Experience Profile.

3 - Since we are simulating things, we need to prepare Sitecore to accept our "robot." Change the following Sitecore configuration files inside the Sitecore installation folder (Website) :

(a) The session duration in Web.config. The timeout is "20" by default. Make it "2", so you can quickly see your data being transferred to Sitecore graphs.

(b) In /App_Config/Include/Sitecore.Analytics.Tracking.config, set these other configurations items as follows:
Analytics Tracking Configuration File part

4 -
We need a page in Sitecore with content that will be presented according to the country we are simulating in our visit. In this other post, Getting started with Sitecore IP Geolocation personalization, there is a step-by-step tutorial on how to create and personalize a page to show a particular piece of content when a visitor comes from a specific geographical area. You can, of course, create more rules to see visitors coming from other countries.

5 - Also, in order to have all the software parts in place within Visual Studio, follow the instructions in this article: Taking screenshots using Webdriver with PhantomJS

To facilitate things, we use unit tests to run our code. PhantomJS is used and we take screenshots of the visited pages to see the results. Because its a headless browser, we cannot see a page rendered by it on the screen, but it lets us take screenshots.

Since we are not using a VPN to simulate the country the visitor is coming from, we need to fool Sitecore. To do it, we are going to inject into the browser's header the X-Forwarded-For variable we set in step 3 with values for IPs from different countries and call our Sitecore page.

Creating the code

1 -
To start with, let's create a list of IP's from different countries. These IPs belong to France, Ireland, Mexico, Rio de Janeiro, Denmark, Portugal, South Africa:

List of IPs used in the test

2 - To inject into PhantomJS, we need to create a PhantomJSOptions object and use it when creating our PhantonJS service. The IP number will be provided by a foreach loop, so we use the IPs list to extract each IP from:

Part of the code that creates PhantomJSOptions

3 - To make things more interesting when viewing the data being gathered by Sitecore, we also inject the site Url the "user" clicked on containing the link to our page. The referrer is fetched at random from the list:

List of referrers for the test
4 - In the rest of the code we open the browser, set it to 1440 wide, load the page we have the rule on, take a screenshot, wait for 3 seconds, navigate to another page and close the browser - the 1000 value is ignored by PhantomJS, and the screenshot always uses the whole page length. The screenshot file names have the IPs used to call the page, thus helping us to see the results.

Part of the code that loads the page


After running the test, look in the d:\temp folder. You should see 6 images. If you've created the test pages explained in the small tutorial linked in step 4, one of them will contain the Brazilian flag.

Pages rendered for each IP.

Next, login in to Sitecore and examine Experience Profile. Click on Last Interaction to show the newest data. You should see something like:

Experience Profile showing results of our calls

If you click on one of the interactions, you can verify that our referrer is there:

Referrer site information

Also in Activity you can see the pages visited and the time spent on the first one:

Visit activity

With this basic code you can create more scenarios by just adding more pages. By including another loop, we can create many calls to produce statistically meaningful data.

The full code is here:

        public void Test_GEO_Location()
            Random rnd = new Random();
            // France, Ireland, Mexico, Rio de Janeiro, Denmark, Portugal, South Africa
            string[] Ips = { "", "", "", "", "", "", };
            string[] referers =
            var options = new PhantomJSOptions();
            for(int cont=0;cont>20;cont++)
            foreach (var ip in Ips)
                var referer = referers.ElementAt(rnd.Next(referers.Count()));
                options.AddAdditionalCapability("", ip);
                options.AddAdditionalCapability("", referer);
                var driver = new PhantomJSDriver(options);
                driver.Manage().Window.Size = new System.Drawing.Size(1440, 1000);
                ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(@"D:\temp\ss-" + ip + "-"+cont+ ".jpg", ImageFormat.Jpeg);
comments powered by Disqus