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



Show first post

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