Skip to content

Instantly share code, notes, and snippets.

@alewolf
Last active May 5, 2020 09:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alewolf/275278f1da379b56512d to your computer and use it in GitHub Desktop.
Save alewolf/275278f1da379b56512d to your computer and use it in GitHub Desktop.
MaxMind GeoIP city locator for unbounce forms
<script type="text/javascript">
$( window ).load(function(){
$.getScript("//geoip-js.com/js/apis/geoip2/v2.1/geoip2.js", function() {
var onSuccess = function(geoipResponse) {
// change '#lp-pom-form-64 #city' to match your form ID and field name
$('#lp-pom-form-64 #city')[0].value = geoipResponse.city.names.en;
};
var onError = function(error) {
// do nothing
};
geoip2.city(onSuccess, onError);
});
});
</script>
@jimhill10
Copy link

jimhill10 commented Mar 18, 2020

For the country lookup this code works great, customized for my form ID of course. I used "country1" for the field name in Unbounce because I had a conflict with another application running on the site. Most will want to use just a new hidden form value of country.

<script type="text/javascript">

$( window ).load(function(){

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

		var onSuccess = function(geoipResponse) {
			// change '#lp-pom-form-398 #country1' to match your form ID and field name
            		$('#lp-pom-form-398 #country1')[0].value = geoipResponse.country.names.en;
		};

		var onError = function(error) {
            		// do nothing
		};

		geoip2.country(onSuccess, onError);
	});
});

</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment