Geolocation ajax API

POST /utils/geolocate.json.

Geolocate visitors based on their IP address - designed specifically for locale targeting in Evance. 

International websites may have a number of regionally targeted locales, offering translated or unique content for regional users. For example, a UK website may have a US locale in English for US and Canadian visitors (/en-us) and a Spanish version targeting visitors from Mexico (/es-us). 

This article does not stipulate the user experience (UX) for visitor geolocation, it only describes how to obtain geolocation and targeting data from the Ajax API. 

An easy-to-implement open source JavaScript library is available on Github at evanceit/theme-geolocator-js which implements the API calls below.

Request

Send a POST request to /utils/geolocate.json.


    jQuery.post('/utils/geolocate.json', function(response) {
        console.log(response);
    });

Accepted parameters

You may supply the following optional parameters with your POST request:

url

You may supply a URL path (e.g. /path/to/my-page) to be localised by the API.
If omitted the website's homepage path (/) will be used.

Typically, you would use this facility to offer users a localised version of the current page - a better user experience than being taken to the homepage.


 

Response

A successful response from the API will include a JSON object containing success, status and data properties.

success
A Boolean true or false.
If true the response will contain a data property.
If false and error report will be provided (see "Error repsonses" below).
status
An integer, which should match the HTTP status code of the response.
data

The geolocation data will include the following sub-properties:

  • data.location
  • data.locale.current
  • data.locale.recommended
  • data.locale.preferred

All of these are described in more detail below.


 

data.location

The location object represents the visitor's country based on their IPv4 or IPv6 address. 

countryCode
The ISO-3166-1 alpha-2 code representing the geolocated country. 
countryName
A string containing the name of the geolocated country.
acceptLanguage

Indicates the preferred languages of the visitor based on their web browser settings. This is supplied as an array of translation code strings in lowercase (see example response below). 

isTargeted

A Boolean indicating whether the geolocation results in a recommended locale which is targeted manually to a country using locale territories. This can be used to correlate results to settings in Evance, but isn't of importance for the user experience.


data.locale.current

Contains information about the current locale if you localised the URL of your API request based on visitor's current locale. This is useful if you wish to include information about the current locale within a geolocation prompt, asking the user if they wish to change from the current locale to the recommended locale.

The current locale's information is also available using the website.locale property in EVML

Locale information includes:

id

The ID of the locale is a lowercase string containing the languageCode and the countryCode (e.g. en-gb). With the exception of the website's default locale the ID of the locale matches the URL prefix for regional content. So a locale with an ID of 'en-us' would have a homepage of '/en-us'.

countryCode
The ISO-3166-1 alpha-2 code for country associated with the locale e.g. 'GB'.
countryName
The name of the Country e.g. 'United Kingdom'.
languageCode
The ISO-639-1 code for the language e.g. 'en'.
languageName
The name of the language e.g. 'English'.
currencyCode
The ISO-4217 code representing the currency of the locale e.g. 'GBP'.
timezone
The time zone string for the locale e.g. 'Europe/London'.
url

A URL to set the user's locale preference and then redirect them to a localised version of the url supplied in the POST request. You can implement this within your theme as you prefer, whether that's as an automatic redirect, or as prompt (recommended).


data.locale.recommended

Contains a locale recommended by Evance for the user. This may be calculated based on the user's browser settings or manually targeted based on settings within Evance. The structure of the locale object is identical to that of the data.locale.current object (see properties table above).

data.locale.preferred

Contains the preferred locale for the user, or null if the user has not yet selected one (i.e. a new visitor). The preferred locale's ID is contained within an ev-locale cookie. This cookie is set automatically when sending a user to the url property returned in in any one of the locale objects. Evance provides this facility for your convenience, however you may wish to create a custom implementation. The structure of the locale object is identical to that of the data.locale.current object (see properties table above). 

Example JSON response

The response below represents a repeat visitor who is visiting a UK locale, but has been recommended the US locale based on their location. However, we acknowledge their preference to remain on the GB locale.


{
    "success": true,
    "status": 200,
    "data": {
        "location": {
            "countryCode": "US",
            "countryName": "United States",
            "acceptLanguage": [
                "en-us",
                "en"
            ],
            "isTargeted": true
        },
        "locale": {
            "current": {
                "id": "en-gb",
                "countryCode": "GB",
                "countryName": "United Kingdom",
                "languageCode": "en",
                "languageName": "English",
                "currencyCode": "GBP",
                "timezone": "Europe/London",
                "url": "/locale?direct=Lw%253D%253D&set=en-gb"
            },
            "recommended": {
                "id": "en-us",
                "countryCode": "US",
                "countryName": "United States",
                "languageCode": "en",
                "languageName": "English",
                "currencyCode": "USD",
                "timezone": "America/New_York",
                "url": "/locale?direct=Lw%253D%253D&set=en-us"
            },
            "preferred": {
                "id": "en-gb",
                "countryCode": "GB",
                "countryName": "United Kingdom",
                "languageCode": "en",
                "languageName": "English",
                "currencyCode": "GBP",
                "timezone": "Europe/London",
                "url": "/locale?direct=Lw%253D%253D&set=en-gb"
            }
        }
    }
}


Geolocation limitations

Geolocation in Evance is based upon the user's IP address (IPv4 or IPv6) to country-level only and is not 100% accurate. Our research has shown that geolocation accuracy significantly worsens when geolocating to state/region or city level. Since Evance's locale system is based on Country, we have no plans to geolocate beyond country level. 

Because IP addresses change regularly we update our IP address tables monthly. 

Website owners/administrators should be aware that visitors may bypass intended geolocation targeting when using a VPN.