This function can be called in the site’s Javascript to determine the location of the visitor of the site.

To be able to call this function, somewhere on the site {% include_plate_scripts "determine_location" %} must be called (e.g. the head section).

Read more about include_plate_scripts here.

The function returns a Javascript Promise, which you can use in the following way:

(() => determineLocation()
  .then(res => console.log(res.country_code))
  .catch(err => console.log(err.message))
)()

The response if successful is a JSON (returned in the then):

{ 
  'country_code': 'NL', 
  'name': 'Netherlands' 
}

The country code is the same as defined in ISO 3166 Standard Country Codes.

If an error occurs (this will be in the catch):

{ 
  'message': '<error message>' 
}

An error is returned in case the network request fails, or if the country could not be determined.