Skip to main content
Javascript Integration
Team avatar
Written by Team
Updated over 5 years ago

Javascript Code Snippet


Each Geo Location service you create in our system will come with its own code unique javascript snippet. Be sure to copy the snippet code provided in your setup.

This piece of javascript code needs to be placed in your website footer or just before the closing </body> tag.

The script is an asynchronous non blocking script, hence it will process while the rest of your web page loads and will not slow down your site.

<script>
var geotargetlylocation1530720411779 = document.createElement('script');
geotargetlylocation1530720411779.setAttribute('type','text/javascript');
geotargetlylocation1530720411779.async = 1;
geotargetlylocation1530720411779.setAttribute('src', '//geo-targetly.com/geolocation?id={serviceID}');
document.getElementsByTagName('head')[0].appendChild(geotargetlylocation1530720411779);
</script>


Once the javascript code is placed in your website, it will contact our server to obtain visitor location data which will become available via javascript functions or directly in our HTML elements.


Javascript Functions

To utilize geolocation data throughout your website's javascript, there are various geolocation functions made available. Below are the list of functions

geotargetly_country_name(); //gets visitor country name
geotargetly_country_code(); //gets visitor country code
geotargetly_region_name(); //gets visitor region or state name
geotargetly_region_code(); //gets visitor region or state code
geotargetly_city_name(); // gets visitor city name
geotargetly_lat(); // gets visitor latitude
geotargetly_lng(); // gets visitor longitude
geotargetly_ip(); // gets visitor IP address

geotargetly_loaded(); // waits for data to become available


HTML Classes

To display visitor location data directly in your site's content, you can use the class names below in any HTML element. The visitor location data will be automatically injected into your site where this element is present.

<span class="geotargetly_country_name"></span>
<span class="geotargetly_country_code"></span>
<span class="geotargetly_region_name"></span>
<span class="geotargetly_region_code"></span>
<span class="geotargetly_city_name"></span>
<span class="geotargetly_lat"></span>
<span class="geotargetly_lng"></span>
<span class="geotargetly_ip"></span>


Example Usage

<head>

</head>

<body>

<!-- display visitor location data-->
We provide free delivery to <span class="geotargetly_country_name"></span>


<!-----Geo Targetly Geo Location Script---->

<script>

var geotargetlylocation = document.createElement('script');geotargetlylocation.setAttribute('type','text/javascript');geotargetlylocation.async = 1;geotargetlylocation.setAttribute('src', '//geotargetly-1a441.appspot.com/geolocation?id=-KvIN0Sr95DvkojmR1CJ');document.getElementsByTagName('head')[0].appendChild(geotargetlylocation);

//----Geo Targetly Geo Location Function Assignment----

function geotargetly_loaded(){  
  var country_name = geotargetly_country_name();  
  var region_code = geotargetly_region_code();  
  var state_name = geotargetly_region_name();  
  var country_code = geotargetly_country_code();  
  var city_name = geotargetly_city_name();  
  var lat = geotargetly_lat();  
  var lng = geotargetly_lng();  
  var ip = geotargetly_ip();

  if (country_name = "United States"){      
     //do something  
  }
}
</script>

</body>

Did this answer your question?