How To Run Tests On Just Your Target Audience

by Melvin Ram on May 10, 2011

I’m a big fan of testing and one tool that I like to use is Visual Website Optimizer (VWO). It allows you to quickly and easily create variations of your pages, split the traffic to these variations and see which ones perform better.

One of the challenges we faced when using VWO on our website was that a large enough percentage of our visitors come from countries that we’re not likely to do business in. This means that any tests we run will be skewed and not truly representative of how my core audience would interact with our website. VWO allows you to segment your tests by a variety of factors but country isn’t one of them yet… so I came up with my own temporary fix until VWO offers a better way.

Integrating MaxMind with Visual Website Optimizer inside WordPress offers a service that allows you to convert an IP address into a country code. For $20 per 200,000 queries, you’re able to basically detect which country your visitor is coming from. When you purchase a license from MaxMind, they’ll provide you a license key. When in your functions.php file of your theme, you can add the following:

function what_country_is_visitor_from($license_key, $ipaddress){
  $query = "" . $license_key . "&i=" . $ipaddress;
  $url = parse_url($query);
  $host = $url["host"];
  $path = $url["path"] . "?" . $url["query"];
  $timeout = 1;
  $fp = fsockopen ($host, 80, $errno, $errstr, $timeout);
  if ($fp) {
    fputs ($fp, "GET $path HTTP/1.0nHost: " . $host . "nn");
    while (!feof($fp)) {
      $buf .= fgets($fp, 128);
    $lines = explode("n", $buf);
    $country = $lines[count($lines)-1];
  } else {
    # enter error handing code here
  return $country;

function add_vwo_code_based_on_country(){ 
  $license_key = "YOUR LICENSE KEY";
  $ip = $_SERVER['REMOTE_ADDR'];

  $country = what_country_is_visitor_from($license_key, $ip);

  $tracked_countries = array("US", "AU", "NZ", "CA", "UM", "GB");

  if ( in_array($country, $tracked_countries) && ( !(is_user_logged_in()) ) ) { add_vwo_code(); } 


function add_vwo_code(){ ?>

  <!-- Start Visual Website Optimizer Code -->
    Code provided by VWO
  <!-- End Visual Website Optimizer Code -->

<?php }

add_action("wp_head", "add_vwo_code_based_on_country");

In the above code, there are 3 parts you’ll need to customize and they have been highlighted in green. The first is the license key that MaxMind provides, the second is the array of countries that you’d like to test and the third is the code that VWO provides to add to your site.

Now even though I’ve shown just how to use VWO, you could replace the tracking code of other systems and achieve the same effect. Happy testing!

{ 2 comments… read them below or add one }

Paras Chopra May 11, 2011 at 7:07 am

Hi Melvin,

Thanks for writing this tutorial on VWO. We’re actually working on integrating geo-targeting in VWO itself — just need to look at scalability perspective. But expect this feature to be adding in a month or so.

Founder, VWO

Melvin Ram May 13, 2011 at 7:25 am

Sweet! Looking forward to it. Also, just as a note, Optimizely uses language as a way to determine location but it doesn’t work so I wouldn’t recommend that approach. The MaxMind IP based approach seems to be working really well.

Leave a Comment

Previous post:

Next post: