Skip to content

Instantly share code, notes, and snippets.

@brandonsheppard
Last active March 21, 2019 23:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonsheppard/27a2d1813d83822c0819 to your computer and use it in GitHub Desktop.
Save brandonsheppard/27a2d1813d83822c0819 to your computer and use it in GitHub Desktop.
Setup basic geoIP

This is the correct way to do this

<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script type="text/javascript">
	var queryCountry = (function () {
		var onSuccess = function (geoipResponse) {
			var popupstatus = $.cookie('popupstatus')
			var country = geoipResponse.country.iso_code
			if(popupstatus != 'popped' && country != 'AU'){
				// open fancybox
				$.fancybox.open([
					{
						href : '/internationalpopup' // This is the URL to the page which contains the contents for the popup
					}
				], {
					padding : 20
				});
				// Set the cookie popupstatus to be popped so it does not bug the customer again
				$.cookie('popupstatus', 'popped');
			}
		};
		var onError = function (error) {
			var country = none
		};
		return function () {
			geoip2.country(onSuccess, onError);
		};
	}());
	queryCountry();
</script>

Dependencies

Maxmind GeoIP

Please note that the service will use MaxMind GeoIP2 and you will need to follow the below step to set up your GeoIP service.

  1. You will need to purchase the credit from the following url https://www.maxmind.com/en/javascript
  2. Once you login to your MaxMind Account, you will need to go to "JavaScript Service Domains" (Under Account Information Menu)
  3. Then, add your Neto Website Domain. (e.g. www.yourhost.com.au)

They have a trial so you can make a trial for the client and send them the details to take it over. Takes a few hours for them to set up your trial so do this first.

cookie.js

Just load this in the footer directly below where jquery is being loaded:

<script type="text/javascript" src="//cdn.neto.com.au/assets/neto-cdn/jquery_cookie/jquery.cookie.js"></script>

Do not do this—it is no longer supported.

To change the country to be determined by the customers location:

(footer)

[%JS_GEOIP_CHECK processor_type:'maxmind'/%]

To do the popup if they are in another country:

(footer)

<script type="text/javascript">
var queryCountry = function () {
	var popupstatus = $.cookie('popupstatus')
	var customercountry = '[@session:ship_country@]'
	if(popupstatus != 'popped' && customercountry != 'AU'){
		// open fancybox
		$.fancybox.open([
			{
				href : '/internationalpopup' // This is the URL to the page which contains the contents for the popup
			}
		], {
			padding : 20
		});
		// Set the cookie popupstatus to be popped so it does not bug the customer again
		$.cookie('popupstatus', 'popped');
	}
}
</script>

To manually set their country back to AU:

(inside modal)

<a href="/?ship_country=AU">Go back to AU</a>

User will probably also want a notification on the homepage AFTER they have set their ship country back to AU, to do this, just add another query to the url and use [@form@] logic:

(inside modal)

<a href="/?ship_country=AU&countrychanged=true">Go back to AU</a>

(on the homepage somewhere:)

[%if [@form:countrychanged@] eq 'true'%]
<div class="alert alert-success">Success! We've changed your location back to AU</div>
[%/if%]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment