Quick Links
Background links
Most modern web analytics features include some sort of geographic decoding. Geographic IP (GeoIP) location companies like Maxmind, strive to collect and update their IP to location database quite frequently and they normally provide either weekly or monthly updates depending upon the subscription. GeoIP data is by no means perfect. When resolving a visitor's ip to their public entrance point on to the Internet to a location, the process has sliding scale of accuracy from 99.5% for the country to 85% for the city and so on down to postal code. Most domestic and mobile user usually can only be resolved to their IPs or mobile provider but sufficiently large corporations will have their own IP infrastructure. What GeoIP data excels at is identifying the country and region(state) levels which for B2B marketers provides enough accuracy for their geographic segmentation campaigns.
In Marketo's case, they use a paid service from Maxmind, and data is mapped into the lead's Inferred data fields
So how do we as marketers use this data? Well one of the easiest is pre-select the country and state for the lead when they fill out one of your forms. Ideally, we would not even ask for country or state but in the case where either the data is incorrectly map because of a few points-of-presence in the case of large companies or hotel and traveling visitors may be mis-identified, the best practice is to populate the geographic fields with the GeoIP data and then let the lead confirm before their submit.
There are two scenarios where different approaches are required.
When the form is hosted in the Marketo landing page environment either as a landing page or a iframe on one of you organisation's page, the result is the same. Because Marketo already has a GeoIP service, all that we have to do use it. By using both form pre-population and the Inferred fields, we can use this information to set the default selections for the state and country.
Specifically, you need to add the Inferred Country and Inferred Region State as hidden fields to your form. If you look at the source of any Marketo landing page that contains a form, then you will notice the pre-population string is inserted just after the <head> tag.

Marketo pre-population script
<script type="text/javascript">
var mktoPreFillFields = {"Email":null,"FirstName":null,"InferredCountry":decodeURIComponent("Canada"),"InferredRegionState":decodeURIComponent("Ontario"), "LastName":null"};
</script>
If you have some javascript experience, you might notice that the mktoPreFillFields variable is really a JSON string and you can therefore manipulate it quite easily.
Marketo pre-population string
<script type="text/javascript" language="Javascript">
// convert the JSON => Object
var geoip = eval("(" + mktoPreFillFields + ")");
// pull out the inferred fields for use later
var geo_country_code = geoip.InferredCountry,
geo_country_region= geoip.InferredRegionState;
</script>
In this case, we will have to provide the Geoip data ourselves but luckily there are some very good APIs that completely isolate you from the complexities of GeoIP lookups.In Maxmind's case, they provide quite a number of server-side implementation of the API. For this example I will be using the PHP version, but it would work equally well in any of the languages.
PHP GeoIP code
<?php
include_once("include/geoipcity.inc");
include_once("include/geoipregionvars.php");
$sVistorAddr = $_SERVER["REMOTE_ADDR"];
$aVistorAddr = explode('.', $sVistorAddr);
if (((int)$aVistorAddr[0] == 10 || ((int)$aVistorAddr[0] == 172 && (int)$aVistorAddr[1] >= 16 && (int)$aVistorAddr[1] <= 31) || ((int)$aVistorAddr[0] == 192 && (int)$aVistorAddr[1] == 168)) == false) {
$gi = geoip_open("GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,$sVistorAddr);
$sCountryCode = $record->country_code3;
$sCountryName = $record->country_name;
$sCountryState = $record->region;
geoip_close($gi);
unset($gi);
} else {
// were on the internal network - just set it
$sCountryCode = "CAN";
$sCountryName = "Canada";
$sCountryState = "ON";
}
?>
GeoIP data into javascript
<script type="text/javascript" language="Javascript">
var geo_country_code = <?php echo $sCountryCode ?>,
geo_country_region= <?php echo $sCountryState ?>;
</script>
Now that we have the geoip data in a pair of javascript variables, it is time to update the dropdowns on the form with a bit of jQuery.
Update the form
jQuery(document).ready(function($){
// allow in-domain javascript access
// see http://www.nczonline.net/blog/2009/09/15/iframes-onload-and-documentdomain/
document.domain='hollebone.ca';
// use the marketo field to populate the form
$('#Country option[value='+geo_country_code+']').attr('selected', 'selected');
$('#State option[value='+geo_country_region +']').attr('selected', 'selected');
})
This is a really easy one. Simply include the fullCountryState.js from the Marketo dynamic country and state (login required) community. Remember to either load it on to one of you public servers or up into the Files and Images area in the design studio. For my own purposes, I converted the country string to ISO-3166-1 Alpha3 codes
Include the fullStateCountry.js file
<script type="text/javascript" language="Javascript" src="fullStateCountry.js"></script>
Connect with me on: