Skip to content

Instantly share code, notes, and snippets.

@andreibabor
Created November 23, 2022 12:49
Show Gist options
  • Save andreibabor/3246baeb8785905238589935b4f82bba to your computer and use it in GitHub Desktop.
Save andreibabor/3246baeb8785905238589935b4f82bba to your computer and use it in GitHub Desktop.
Shopify: IP detect and redirects to other domains
<script>
//begin my custom ip redirect
//Let's check if we have the value in localstorage
if (localStorage.getItem('country')) {
//Already have the value in localStorage, no need to make a call to IPinfo
// window.location.href = '/welcome?country=' + localStorage.getItem('country')
console.log(data.country);
} else {
// No cached data, let's get it from IPinfo
fetch('https://ipinfo.io/json?token=16af836c0d5158')
.then(res => res.json())
.then(data => {
//We have the data, let's cache it in localStorage before redirecting
localStorage.setItem('ipinfo', data.country)
//window.location.href = '/welcome?country=' + data.country
console.log(data.country)
if ( data.country === "HK" ){
window.location.href = 'https://www.judithleiber.hk/zh-hk/';
}
else if (data.country === "JP"){
window.location.href = 'https://www.judithleiber.jp/ja-jp/';
}
else if (data.country === "KR"){
window.location.href = 'https://www.judithleiber.kr/ko-kr/';
}
})
}
</script>
<!--end my custom ip redirect -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment