Configure geolocation

Configure geolocation services to display personalized content based on the visitor's location.

Before you begin:

  • Personalization uses Geolocation API to get the visitor location. This API lets you discover, with the user's consent, the user's location. The Geolocation API only works on secure contexts (HTTPS). If your site is hosted on a non-secure origin (such as HTTP), any requests for the user's location will not work.
  • This code snippet is relevant for Personalization Library version 1.2.0 or later.

To configure geolocation:
Add the following code snippet into the HEAD tag of the HTML page of your SPA or MPA.

<script>    
       if (navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(success,error);
          }        
      function success(position) {            
         window.geoposition = {};            
         geoposition.lat = position.coords.latitude;  
         geoposition.lon = position.coords.longitude;
          }        
      function error() {            
        console.log('Cannot get location');        
         }    
</script>