Geo-Location Script?

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 ?

Hey Johan, MaxMind should work, did you try their JavaScript product?

Yes try the javascript version with no luck

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

Here’s what I did on my Unbounce page:

  1. included the MaxMind City JavaScript, located in the document head (see

  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,


Can you post the script you used?

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

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


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
  2. (as above) Include a Javascript code into the header (name it something like geo_loc_js) and use the snippet from:…

It will look something like:

<script src="//" 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>   


1 Like

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("//", 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':<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 =;<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 />, 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.