[How-to] Use Geotargeting to Display a Visitors Location on a Page


Userlevel 6
  • Former Unbouncer
  • 198 replies

Want to take the personalization of a page to the next level? Maybe you want to display a visitor’s location on the page or pre-fill a city/ region form field.

Now you can using a FREE IP lookup database called GEOLOCATION DB and all it takes is a simple AJAX call and some basic JS. Even better, it’s written for you!

You can see this in action (built in Unbounce) here:
http://unbouncepages.com/geoip-db-location-example/

This also works in both a pop up and a lightbox: http://unbouncepages.com/test-location-convertable/


How to Install in Unbounce

Click Here for Instructions

🚨
This is not an official Unbounce feature. This functionality is based entirely on third party code, and has only been tested in limited applications. Since this isn’t a supported Unbounce feature, our support team will be unable to assist if things go awry. So if you are not comfortable with HTML, Javascript and CSS, please consider consulting an experienced developer.


UPDATE

Step 0

Geolocation DB now requires users to signup in order to use their service. It’s a free signup that will give you a unique API key that must be included in the URL in the code that makes the request to get the location of a visitor.

Once you’ve signed up for a free account you will wan to use the jsonp URL that is provide. It should look something like this:
https://geolocation-db.com/jsonp/[YOUR API KEY]”

Step 1.

Copy this JavaScript and add it to the page “before body end tag”:
REMEMBER TO UPDATE THE URL

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
   jQuery.ajax({
    url: "https://geolocation-db.com/jsonp/[YOUR API KEY]",
    jsonpCallback: "callback",
    dataType: "jsonp",
    success: function( location ) {
      var country = location.country_name;
      var region = location.state;
      var city = location.city;
      var lat = location.latitude;
      var long = location.longitude;
      
      // add classes to display location info on page
      //City
      jQuery(".cityText").text(city);
      
      //Region
      jQuery(".regionText").text(region);
      
      //Country
      jQuery(".countryText").text(country);
      
    }
  }); 
</script>

Step 2.

Decide on the text you’d like to use to dynamically display the location information (the AJAX call will make available the country, region, city, latitude, and longitude information from a visitors IP).

Double click the text box and click the “view source” button in the bottom right. Wrap the text you wish to be dynamic with a span and assign the relevant class to display the location information. Here’s a screenshot of what that would look like to display the city info:

####The Default classes for the script are as follows. You can change them if you wish, just be sure to update the javascript to match!

City = "cityText"
Region = "regionText"
Country = "countryText"

Step 3. OPTIONAL Prefill Form Fields with Location Information

You can pre-fill a form field or preselect a dropdown (as long as the option exists) by adding some code to the script. Make sure the ID of your field in the form matches the ID in the code below (you can edit either or both to match):
> $("#cityField").val(city);
> $("#provinceField").val(region);
> $("#countryField").val(country);

Step 4. OPTIONAL Add a Google Map Displaying the Visitors City

Add a custom HTML widget to the page and paste in the following div tag:
<div id="map-container"></div>

Then paste in the following code into the script. Note you can change the size of the map by adjusting the “height” attribute of the iframe:

var frame = '<div class="embed-container maps"><iframe width="100%" height="481px" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=AIzaSyBvWSn-wEccZ_YSMG-xW7CfyaecyV1j0uA&q='+city+'+'+region+'" allowfullscreen></iframe></div>'
      
      $("#map-container").html(frame);


Want to take your Unbounce landing pages + Convertables™ to the next level?
:spinbounce: Check out the Ultimate List of Unbounce Tips, Scripts & Hacks


86 replies

Hi @Rob, thanks for the Geotargeting script- works great on my LP. I want to push the location into a hidden field so that I can collect that data upon lead submission.

Could you please break that down step by step, what do I name the hidden field, then where exactly do I add the: $("#city").val(city);

Thanks so much- I am learning a lot, but don’t know enough to get past step by step and copy and paste yet… 🙂

Thank you,
Ed

Userlevel 7
Badge +4

Hi there! Did you get this to work for you?

I can send you the tweaked code if you need it.

Cheers! 🚀

Hi Stefano,

This is completely unrelated but after testing the API using postman, I can see that the data is showing on my landing page…but I am not able to produce the same results for my colleague in Thailand. Would you mind taking a look? the webpage is http://lp.storehub.com/tqpage-test/

Their ip address is [96.30.104.13]

Thank you 😁

Userlevel 7
Badge +4

Hi there!

Is your colleague on a VPN or using an adblocker?

Hi @Stefano,

Nope but it could be the proxy that maybe causing this issue. Just a speculation. Can this script work with other API services? for example from ipgeolocation.io.

I mean technically the script should work the same with what ever API url and the only thing that needs to change is the ajax script on top right? 😁

can anyone let me know if the code above works for ipgeolocation.io? or must I write an extra line or change something for it to work

Userlevel 6

Hey @justinoon! The code could potentially work with https://ipgeolocation.io/ but will require a bit of work. You’ll need to change the value of the “url” field in the ajax request to be the endpoint for https://ipgeolocation.io/

url: "URL FROM IP GEO LOCATION",

You’ll also want to review how the data being returned from IP Geo Location is named and formatted. I’ve had a quick look at their docs and the data is formatted as JSON which is good. You will need to make some small edits though based on the naming conventions. For example location.state would need to be changed to location.state_prov.

It does look like the naming conventions are very similar though so I don’t think there will be any huge updates to the code required. One thing I did notice though is that to authorize with https://ipgeolocation.io/ you’ll either need to use an API key (which this script does not have built in) or use a specific request origin (ie. domain). In order to get it working with the script here I’d recommend going with the request origin option.

Hope this helps a bit, let me know if you have any follow up Qs 🙂

Did you ever figure this one out? I am looking to do the same thing…

Can you share the code you are using for this?

Hi Rob - Thanks for sharing this script. Just installed and it’s working great.

Two quick questions if you’re ok to answer that would be awesome:

  1. Where exactly would I “paste the following code into the script”? This is in reference to the “Add a Google Map…”

  2. Likewise where exactly would I add the “Prefill Form Fields…” script.

I’ve got it to work (basic geo script) so thanks! But, I’m not a coder so dunno how to merge the other two scripts you’ve kindly provided.

Thanks a bunch!

Eddie

Userlevel 6

@Akkysia great questions! This can be a little bit tricky if you’re not familiar with javascript, but you’ll want to place both the map and prefill snippets at the end of the function but not outside of the closing brackets });

Here’s a screenshot of what it looks like on the example page. You can see that the prefill and map snippets are near the bottom but still inside that function that’s pulling in the location from GeoIP-DB

Hope this helps!

Hi Rob - It does. Let me get that onto the page and switch it on! Thanks again!

It seems that AJAX Requests are blocked and this not achievable anymore. Please let me know if I’m wrong!

Hey everyone, are you guys also having issues with the Geo IP not working or is it just me?
Thanks,

Userlevel 7
Badge +3

If you open up the geoip page, you’ll see a notice that they had to move domains.

The documentation on their new site provides the new address that’s needed to make a call to their service.

Userlevel 6

Thanks for checking on this Hristian, great catch! I’ve updated the original post with the new URL that needs to be used (https://geolocation-db.com/json) and updated the link to their website in the post description. Thanks again!

Thanks for the quick response!

Hey Rob, Actually after updating to the new URL, script stopped working. I found out that my issue was the Wifi I was using prior, and not the URL. Do your own investigation, but I believe the old URL works fine.

Hi,

Is there a way to use Google Ads’ {loc_physical_ms} instead?

I understand the location ID would somehow need to be “translated” into an actual location name but not sure how to implement this.

Cheers

Hi @Rob ,

Thanks but unfortunately neither scripts work for me (Maxmind and Geolocation DB).

I know I’m late on the thread but could you please kindly assist?

Cheers

Userlevel 6

Hi @Michael_Granger I think the issue may been due to me updating the URL in the script which I believe may have broken the functionality. I’ve swapped it back to the previous URL and that should fix the issue.

The URL to use in the script is “https://geoip-db.com/jsonp/

So the (first three lines of the) code would look like:

 $.ajax({
    url: "https://geoip-db.com/jsonp/",
    jsonpCallback: "callback",

Let me know if that fixes it for ya!

Hi Rob,

Thanks a lot for assisting, it now works!

Is there any way to show a default text in lieu of “Not Found”?

Thanks again.

Cheers,

Mike

Userlevel 6

Good question Michael! To show some default text in the event that a location isn’t returned in the response you could use an “if” statement placed after the variables are set from the response from GeoLocation DB. So for example:

if (!city || city==“Not Found”) {
$("#cityField").val(“default text”);
}

The above code says if there is no value for city (!city) OR if the value for city is “Not Found” then display “default text” in the city form field.

You can add this immediately after the code setting the variables like so:

  var country = location.country_name;
  var state = location.state;
  var city = location.city;

if (!city || city==“Not Found”) {
$("#cityField").val(“default text”);
}

One thing to note is that this would need to be done for each field separately. Also the “Not Found” is case sensitive so you’d want to make sure that matches the exact not found message returned by Geolocation DB.

Hope this helps!

Hi Rob, I am having trouble using the geotargeting on my landing page. I copied the script provided and made all the necessary changes. It still doesn’t work. Wondering if you could help. Thanks!!

Hi, Rob Could you provide a code that would mark a radius on the map around the mapped city.

Thanks

Reply