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



Show first post

86 replies

Userlevel 6

Sorry for the delayed reply here @toniomorais. Here’s the original example page I created using MaxMind (it works because unbouncepages.com is registered with their service): http://unbouncepages.com/location-example/

The first step to get it working is to include the maxmind js library via a CDN link. Place this in the head of the page:

<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>

Then you can use the following code to apply the location data to the page. See the comments in the code for a more in depth explanation of what each part does:

<script>
  var getCity = function(el) {
    var cityContainer = $(el);
    
    var updateCityText = function (geoipResponse) {
      //get city, if on city available use the default "your city" 
      var cityName = geoipResponse.city.names.en || 'your city';
      
      //get region
      var regionName = geoipResponse.most_specific_subdivision.names.en;
      
      // combine city and region on page
      var city = cityName;
      if (cityName && regionName ) {
            city += ', ' + regionName;
        }
      
      // swap city on page
      cityContainer.text(city);
      
      // optional fill form fields with city value
      $("#city").val(cityName);
      $("#province").val(regionName);
      
      // Google map
      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='+cityName+'+'+regionName+'" allowfullscreen></iframe></div>'
      
      $("#map-container").html(frame);
         
    };
 
    var onSuccess = function (geoipResponse) {
      updateCityText(geoipResponse);
      console.log(JSON.stringify(geoipResponse, undefined, 4));
    };
    
    // If error show in console
    var onError = function(error) {
      console.log(JSON.stringify(error, undefined, 4));
    };
    geoip2.city(onSuccess, onError);
  };
  
  // Add ID or class of element to swap cit with (i.e. the textbox you'd like to display the location info in)
  getCity(".swap");
  
</script>

Hope this helps getting you started. Let me know here or feel free to direct message me if you hit any snags.

The sample URL looks to not be working correctly and I feel like the script doesn’t work anymore. Can anyone confirm this for me?

Hello,

We have 7 phone numbers (covering 7 world regions) and we would lke to use this to identify what country a visitor is in to show the corresponding phone number.

How would you do this?

Thank you @Rob !!

Thank you @Rob

Hey @Rob , Just tried this out and as far as I can tell it should be working. Can you take a look and let me know what’s going wrong? I’m really trying to pre-populate zip code into a field but I can’t seem to get even the display text to work. http://specials.zendyhealth.com/test-bed/

I just tested it and it thought I was 113 miles away, so maybe not for me 🙂

ok, but there is any way to display a section (not just some small text) only to one country?

thanks

Hey @Rob - this is great!

Is there any way to use it to show specific fields? Use case example is GDPR consent check box for EU visitors.

Would love it if we can show/hide this field depending on where visitor is located.

Is there a way to make the “fallback” text say something other than “not found” in the event the script can’t find a city, region or country name?

whoa !

a big thanks for this big tips !!! 😃

I was using this feature successfully for the past couple months. Now my LPs are getting “Mixed Content” errors using this feature.

https://quote.hometownheatingandair.com/furnace-repair/a.html

I can’t get this to work on an exit popup. I’m not sure why. Any suggestions?

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

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

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,

Thanks for the quick response!

Reply