Geo-Location Script?


#1

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 ?


#2

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


#3

Yes try the javascript version with no luck


#4

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?


#5

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


#6

Hey Joerg, glad you found that useful!


#7

Thank you Carl, you are a gentleman and scholar !


#8

Johan, you’re too kind…


#9

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?


#10

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.


#11

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


#12

Can you post the script you used?


#13

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

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

#14

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


#15

Awesome! Thanks for sharing that Aleks!


#16

What if I need to redirect to another URL?


#17

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. 


#18

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 />

#19

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!


#20

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.