Skip to content

Instantly share code, notes, and snippets.

@panoply
Created August 2, 2017 19:27
Show Gist options
  • Save panoply/1ab34580673209fab0ddc92a33043afc to your computer and use it in GitHub Desktop.
Save panoply/1ab34580673209fab0ddc92a33043afc to your computer and use it in GitHub Desktop.
geoIP
```
var GeoIP = (function () {
var fn = {};
var data = {};
var geo = [];
var rate;
fn.init = function(options) {
data = options;
fn.loadGeoIP(data.maxmind);
};
fn.loadGeoIP = function(script) {
if (!loadjs.isDefined('GEOIP')) {
loadjs(script, 'GEOIP', {
success: function(){
return geoip2.country(fn.dispatch, fn.error);
}
});
}
};
fn.loadJson = function (path, success, error) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
if (success)
success(JSON.parse(xhr.responseText));
} else {
if (error)
error(xhr);
}
}
};
xhr.open("GET", path, true);
xhr.send();
};
fn.dispatch = function (geoip) {
if(window.location.hostname === 'www.brixtol.com') {
if (geoip.country.iso_code === "SE"
|| geoip.country.iso_code === "NO"
|| geoip.country.iso_code === "DK") {
fn.redirect("SE");
} else {
fn.localize(geoip.country.iso_code, geoip.country.names.en);
}
}
if (window.location.hostname === 'se.brixtol.com'){
if (geoip.country.iso_code === "SE"
|| geoip.country.iso_code === "NO"
|| geoip.country.iso_code === "NL"
|| geoip.country.iso_code === "DK") {
fn.localize(geoip.country.iso_code, geoip.country.names.en);
} else {
fn.redirect("EU");
}
}
};
fn.localize = function (iso, country) {
fn.loadJson(data.settings, function(geoJson){
return fn.setup(geoJson.local, iso, country);
}, function(xhr){
return fn.error(xhr);
});
};
fn.setup = function (config, iso, country) {
var obj;
for (var i = 0; i < config.length; i++) {
geo = config[i];
if (geo.iso === iso) {
if (geo.iso === 'SE') {
rate = geo.sek_shipping_rate;
} else {
rate = geo.eur_shipping_rate;
}
obj = {
country_code: geo.iso,
currency: geo.cur,
shipping_rate: rate,
country: country,
flag: 'https://cdn.shopify.com/s/files/1/0638/4637/t/9/assets/flag_'+geo.iso.toLowerCase()
};
store.set('Localize', obj);
}
}
return fn.flag(obj.flag);
};
fn.flag = function (flag) {
var element = document.querySelector(data.flagSelector);
element.style.backgroundImage = 'url('+flag+'.svg)';
};
fn.redirect = function (site) {
if (site === "SE") {
if (window.location.pathname < 0) {
window.location = 'https://www.se.brixtol.com';
} else {
window.location = 'https://www.se.brixtol.com'+window.location.pathname;
}
} else if (site === "EU") {
if (window.location.pathname < 0) {
window.location = 'https://www.brixtol.com';
} else {
window.location = 'https://www.brixtol.com'+window.location.pathname;
}
} else {
return false
}
};
fn.error = function (error) {
console.warn(error);
};
return fn;
})();
var local = GeoIP.init({
flagSelector: '#flag',
maxmind: '//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js',
settings: '//cdn.shopify.com/s/files/1/1477/6508/t/7/assets/localize.json',
redirect: 'https://www.brixtol.com'
});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment