Solved

Geo-Location Script?

  • 19 October 2012
  • 26 replies
  • 155 views

I need to greet my users with their geo-location such as
" we have special pricing for users in (city) , (state)"
tried maxmind geoip could not get it to work.
any other that will work in unbounce ?

icon

Best answer by Justin 15 August 2017, 20:28

View original

26 replies

Hey Johan, MaxMind should work, did you try their JavaScript product? http://www.maxmind.com/en/javascript

Yes try the javascript version with no luck

Hey Johan, I just whipped up a working demo here:

http://unbouncepages.com/maxmind-geoi…

Here’s what I did on my Unbounce page:

  1. included the MaxMind City JavaScript, located in the document head (see http://dev.maxmind.com/geoip/javascript)

  2. included a Custom HTML component that used document.write to call MaxMind’s geoip_city() function to output the detected city name

Does that help? You’d likely want to set things up so that the MaxMind script gets pulled after your doc loads (say, in a jQuery .ready() handler), which also means you’d need to use something other than document.write(). Does that help at all?

This is very cool. It also works with german ip _s

Hey Joerg, glad you found that useful!

Thank you Carl, you are a gentleman and scholar !

Johan, you’re too kind…

I’m just starting to play with WP and still have a lot to learn. I was wondering if there was a script that will do this for a wp site?

I can get the GEO to work in an HTML element, but not inside of a form. So I have a field for “state”, and I’d like to auto-detect the state of the user. I include my code as

value="" inside of a form input, but it just output the js code itself, not the value of the actual state.

Hello W. Brooks,

I’m guessing the problem is that the script is attempting to insert the value into the form before the form has a chance to completely load in the browser. I was able to insert the value into a form field with the following script.

The key is to use $(document).ready() because that tells the script to wait until the entire html document is loaded before executing. I’m using jQuery to do that so be sure to have the jQuery checkbox selected. In this example I created a form field that I named city so be sure that you change the above code to select the field by it’s name, also my form id is #lp-pom-form-9 yours may have a different id.

To get the form ID click on the form and look near the bottom of the right hand panel. You will see the id. Make sure that the id also contains the # as that is required by jquery.

I hope this helps. If you need more assistance with this please let me know.

Kind regards,

Slan

Can you post the script you used?

ooops sorry. It escaped my code and I didn’t even notice!!

$(document).ready(function(){   
$('#lp-pom-form-9 #country')[0].value = geoip\_city();   
});   

Hi

This is a solution for a automatic dropdown country list pre-selection.

I’d like to share the solution with you and hope it will help one or the other person who doesn’t exactly know how to use Javascript. My version works, but sure is not very elegant and I hope that someone will pick it up and make it beautiful.

  1. (as above) Purchase a subscription through www.maxmind.com
  2. (as above) Include a Javascript code into the header (name it something like geo_loc_js) and use the snippet from: http://dev.maxmind.com/geoip/legacy/j…

It will look something like:

<script src="//js.maxmind.com/js/country.js" type="text/javascript"></script>

  1. (new) Add following Javascript before the body end tag. Name it something like “Geo Locate Country”. Within the Javascript you will have to exchange the “lp-pom-form-128” to the actual ID of your form. Also the list name and all options need to be customized.
<script type="text/javascript"> <br /><br />
$(document).ready(function(){ <br /><br />
  var cc = geoip_country_code(); <br /><br />
  if (cc=="AT"){$('#lp-pom-form-128 #land')[0].value = "
sterreich";} <br />
  else if (cc=="CH"){$('#lp-pom-form-128 #land')[0].value = "Schweiz";} <br />
  else if (cc=="DE"){$('#lp-pom-form-128 #land')[0].value = "Deutschland";} <br />
  else if (cc=="BE"){$('#lp-pom-form-128 #land')[0].value = "Belgien";} <br />
  else if (cc=="FR"){$('#lp-pom-form-128 #land')[0].value = "Frankreich";} <br />
  else if (cc=="LI"){$('#lp-pom-form-128 #land')[0].value = "Liechtenstein";} <br />
  else if (cc=="LU"){$('#lp-pom-form-128 #land')[0].value = "Luxembourg";} <br />
  else if (cc=="NL"){$('#lp-pom-form-128 #land')[0].value = "Niederlande";} <br />
  else if (cc=="NO"){$('#lp-pom-form-128 #land')[0].value = "Norwegen";} <br />
  else if (cc=="PL"){$('#lp-pom-form-128 #land')[0].value = "Polen";} <br />
  else if (cc=="PT"){$('#lp-pom-form-128 #land')[0].value = "Portugal";} <br />
  else if (cc=="SE"){$('#lp-pom-form-128 #land')[0].value = "Schweden";} <br />
  else if (cc=="ES"){$('#lp-pom-form-128 #land')[0].value = "Spanien";} <br /><br /> }); <br /> </script>   

Cheers
Aleks

Userlevel 3

Awesome! Thanks for sharing that Aleks!

What if I need to redirect to another URL?

Hi Kenji - Can you give me a bit more clarity on what exactly are you looking to do here? Generally, if you’re looking to drive traffic from multiple different countries/locations, and wish to have different versions for each location, then you’re going to want to target your ads and create individual landing pages for each. Give me a bit more info on what you’re trying to achieve and I’ll see what I can do to help. 

I’ve updated the script to be loaded and executed only after the entire page has loaded. This is a performance improvement.

<br />// Delay geoip location script until the entire site has loaded<br />$().ready(function() {<br />// Load MaxMind's geoip library. Once that's done, continue.<br />$.getScript("//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js", function() {<br />var onSuccess = function(geoipResponse) {<br />// Get the country name in germen (can be any language offered by MaxMind. See the documentation)<br />dataLayer.push({<br />'eventCategory': 'GeoIP',<br />'eventAction': 'MaxMind lookup success',<br />'eventLabel': geoipResponse.country.names.de<br />});<br />// Update the country field in the form. The selector needs to be adjusted for each landingpage.<br />$('#lp-pom-form-128 #land')[0].value = geoipResponse.country.names.de;<br />};<br />// Error handling<br />var onError = function(error) {<br />console.log("MaxMind error");<br />dataLayer.push({<br />'eventCategory': 'Error',<br />'eventAction': 'Maxmind lookup error',<br />'eventLabel': JSON.stringify(error, undefined, 4)<br />});<br />};<br />geoip2.country(onSuccess, onError);<br />});<br />});<br />

Aleks, this is (still) awesome. Thanks so much! I’m going to send over a quick email to connect on making this into an official workaround article. Hang tight!

Be careful how specific you get with IP to Geo. As I mentioned in one of the other threads, it doesn’t work well at the city level.

For auto selecting countries, it should work perfectly. Or if you want to segment on something like east coast vs west coast (Hardee’s v Carl’s Jr anyone?). But if you are thinking of using it to segment cities that are only a few hundred miles away, it didn’t work for us.

Thanks for chiming in on this again, Philip. It’s important to remember that the accuracy isn’t even close to being perfect yet. Country selection, or West vs. East (Carl’s Jr for the win!) should be perfectly fine though. 

wooowww thanks for information

Great

I just implemented this script to capture [city], [state], [zip], and [country] on our Unbounce pages and pass them in hidden form fields into custom fields in our SFDC instance. Drop it in the head of your form page and make sure the “#lp-pom-form-XX” matches and you should be good to go.

$().ready(function() {

  $( window ).load(function() {



 $.getScript("//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js", function() {







 var onSuccess = function(geoipResponse) {













$('#lp-pom-form-61 #city')[0].value = geoipResponse.city.names.en;













$('#lp-pom-form-61 #zip')[0].value = geoipResponse.postal.code;













$('#lp-pom-form-61 #state')[0].value = geoipResponse.subdivisions[0].iso_code;













$('#lp-pom-form-61 #country')[0].value = geoipResponse.registered_country.iso_code;







 };







 var onError = function(error) {







 };







 geoip2.city(onSuccess, onError);





});

  }); });

Just wanted to chime in and say that @Rob has published a new #unbounce-tips-and-scripts post about how to do just this by using GeoIP.

Badge

I have the free Geo IP working just fine on my non WordPress domains. However, this is not working on my Wordpress domains. I think it is due to a conflict as I have a Geo IP lookup plugin running. I could very easily just do this in Zapier using the visitor Ip sent from Unbounce, through the IP2Location integration. But I already license Maxmind. So I just wrote this script to do it and send it to a hidden form value “country1” (not country as there could be a conflict there on Wordpress, just in case). Had to customize the form value, this was posted on Github for the city lookup. That function works great too if it is needed. My code is posted as a comment on that same page. https://gist.github.com/alewolf/275278f1da379b56512d

Maxmind made some changes in the service
https://dev.maxmind.com/geoip/geoip2/javascript/

anybody can help how to update the script to work with this changes of maxmind?

thank you

Reply