[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

Userlevel 6

I used MaxMind originally for this scrip actually without realizing it was a paid service (someone must have registered unbouncepages.com). As soon as I realized it was paid I started looking for alternatives and found GeoIP-DB. I’ve tested it with a few services that emulate traffic from different locations and it’s faired well:
https://geopeeker.com/fetch/?url=http%3A%2F%2Funbouncepages.com%2Fgeoip-db-location-example%2F

http://shotsherpa.com/adventure/596d295e8c3dcf3249252873

IP location though in general won’t always be 100% accurate as there is no way to account for VPNs, proxys, and a whole bunch of other stuff as you know I’m sure. And of course you’ll always be reliant on someone else’s database of existing IP addresses and the corresponding information.

In the limited testing I’ve done with GEOIP-DB though I haven’t seen it miss yet. Full transparency though I haven’t tested this in rural areas or locations where ISP coverage is limited. So it’s not going to be the best solution for everyone.

There are other ways to achieve a similar end result though (granted with a bit more work). For example, you can target your ads based on region if you’re running PPC and use Unbounce’s DTR feature to swap text out based on the specific ad or region.

As with anything though you’ll want to test to make sure it’s doing what you need and expect it to do.

Userlevel 6

Sure does! http://unbouncepages.com/test-location-convertable/

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.

Nice results! Glad to see you tested this as much as you did.

I am on a company IP that spans all of our locations (15 cities). GeoIP-DB matched me to the correct location. Not sure if it was just luck, or if GeoIP-DB has some more sophisticated geo-mapping than simply the IP address.

I know Google’s Geolocation API service uses cell tower data as well. But that wouldn’t help in this instance since I am on a desktop.

Userlevel 6

hey @Samir I got this working for you on the page. This one is actually my fault as the instructions aren’t as clear as they could be. The map code needed to be wrapped in the existing AJAX function. I made that change for you and the map is displaying now 🙂

Userlevel 6

Hey @Samir no need to apologize. The city location is actually working for me currently. Here’s a screenshot: https://www.screencast.com/t/BirsIib1

Where are you testing from? It might be that for whatever reason your specific location isn’t part in the GeoDB database of IP addresses. Or if you’re on a network that uses a proxy or a VPN setup that could certainly have an impact.

I ran the page through the location simulations via geopeeker.com and it looked to be working as expected there. Check the results here: https://geopeeker.com/fetch/?url=http%3A%2F%2Funbouncepages.com%2Ftest-geo%2F

I hope this at least supplies some peace of mind for you. The code does appear to be working correctly, so the either the IP you’re on isn’t part of the GeoDB database or it’s being masked/blocked and isn’t visible.

Try running the URL through some similar location testing services to see if you get similar results. Let me know if there is anything I can do to help!

Userlevel 6

Hi @AlanJ unfortunately the specific timezone of the region the IP is located in isn’t something made available by the IP database from GeoIP-DB. It is possible to get the local timezone of the client browser though using some more custom javascript.

I don’t have any prepared workarounds for this at the moment I’m afraid. If I can somehow manage to find the time to take a shot at it I will, but can’t promise when that will be. In the meantime this Stackoverflow article has some potential solutions: https://stackoverflow.com/questions/6939685/get-client-time-zone-from-browser

@Rob, I can not believe it. It just works! Thanks. Thanks. Thanks!

Userlevel 6

Hi @EthosData! You’d need to write some more code to conditionally display the phone number based on the country. Something like this might work:

if (country == “name of country”) {
// display phone number
} else if (country == “name of other country”) {
// display different phone number
}

The above is just some psuedo code to get you started. You’ll need to target the html that is displaying the phone number and change the content with javascript.

Hope this helps!

Userlevel 6

Hi @Justin_Frech I had a look and there were some syntax errors in the code (a few stray < > and a couple of missing ‘$’). I’ve fixed those for you and saved and it appears to work in Preview. Have a look and if you’re happy you can republish the page.

Is it possible to update a button with the user’s location?

For example, a button would read “Request a Demo in {cityText}”

Userlevel 6

Hi @mike22rtn This is an awesome suggestion! It should be possible with some tweaks to the code above to target a button label. It might be tricky to only swap a section of the label due to how the Unbounce builder generated buttons. It would be easier perhaps to use a text link and you can edit the source code directly in the builder and add some <span> tags to target specific text.

A sneaky little workaround to still have it look like a button would be to layer the text link over the button and remove the button’s actual label. To the visitor the text link would still look like the button label.

I’ll try and find some time to add this into the existing workaround in the near future as it shouldn’t be too difficult

For what it’s worth, after testing 3 different providers, I’m using ipgeolocation.io, and they’re definitely the best. Everything is SSL

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 🙂

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!

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!

Badge

Wow! I have been looking for something like this forever. Thank you so much for putting this together.  

I have implemented it on my page, but not having any luck with it working. I’m sure i’ve done something wrong. 

Any chance someone can take a quick look? www.trojansteel.com

 

Badge

Wow! I have been looking for something like this forever. Thank you so much for putting this together.  

I have implemented it on my page, but not having any luck with it working. I’m sure i’ve done something wrong. 

Any chance someone can take a quick look? www.trojansteel.com

 

Ok, I have been able to make this work after some tinkering. Only question now- how do I make it where it shows City, State?

Hey Rob!

It appears the signup for https://geolocation-db.com/ doesn’t work anymore.
I tried multiple times and it keeps timing out. Can you recommend any good free alternatives?

Thanks!

Userlevel 4
Badge

Hey @Marketing Mark ,

 

Thanks for your comment. Were you able to complete the signup process in the end? I just tried myself and was able to get my API key and URL first time (I signed up through my Google account if that helps).

For the life of me i cannot get this working, maybe the script is outdated now?

Userlevel 7
Badge +3

Hey @heatfactory

It would be easier for us to troubleshoot this for you if you include a link to your page. 

You can also copy your page and publish to the unbouncepages domain you have available to you if you don’t want to share the actual page.  

Hey @Marketing Mark ,

 

Thanks for your comment. Were you able to complete the signup process in the end? I just tried myself and was able to get my API key and URL first time (I signed up through my Google account if that helps).

Nope, still unable to sign up. It keeps timing out… i also tried using a google account.

Userlevel 4
Badge

Hey @Marketing Mark ,

 

Thanks for your comment. Were you able to complete the signup process in the end? I just tried myself and was able to get my API key and URL first time (I signed up through my Google account if that helps).

Nope, still unable to sign up. It keeps timing out… i also tried using a google account.

Thanks for confirming @Marketing Mark . I am sorry that you are still having this issue. I wish I had some alternatives for you, but this is something I am far from an expert on. My only suggestion would be to try signing up with a different device on a different network, or to turn off your VPN if you are using one.

This is awesome @Rob! I can’t wait to see the various ways this gets implemented on landing pages.

Question: Will this work on Convertables, too?

Reply