How to get a visitor’s location and redirect them to right store for Shopify
There are many businesses create multiple online stores with Shopify. Each site will serve a specific customer area. And the requirement is “How can I automatically send customers to the right store, based on their country or IP Address?”
Example we have 3 sites:
- www.domain.us – Customers in the United States
- www.domain.ca – Customers in the Canada
- www.domain.com – Customers in rest the world
Don’t worry. This article guide you to do that.
We use service of GeoPlugin.Net (GeoPlugin) to detected visitor’s location. GeoPlugin provides a public HTTP API for software developers to search the geolocation of IP addresses. This is the code.
1 | <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script> |
You should embed this code into <head> tag
The result returned includes: IP, country code, country name, region code, region name, city, latitude, longitude, currency code…
We need country code information. You can compare with country codes table at here to know more.
Now we write code to redirect.
In site’s United States
1 2 3 4 5 6 7 8 9 | <script> if(geoplugin_countryCode() != 'US'){ if(geoplugin_countryCode() == 'CA'){ location.href = "//www.domain.ca"; }else{ location.href = "//www.domain.com"; } } </script> |
In site’s Canada
1 2 3 4 5 6 7 8 9 | <script> if(geoplugin_countryCode() != 'CA'){ if (geoplugin_countryCode() == 'US'){ location.href = "//www.domain.us"; }else{ location.href = "//www.domain.com"; } } </script> |
and site’s rest the world
1 2 3 4 5 6 7 | <script> if(geoplugin_countryCode() == 'US'){ location.href = "//www.domain.us"; } else if(geoplugin_countryCode() == 'CA'){ location.href = "//www.domain.ca"; } </script> |
If you don’t know write code you can choose one of the apps:
Where exactly should I introduce this code?
You have to add this code in you theme.liquid file
Hi,
I tried this code. But its working only for one time. Can please help me on this.
Its should works for every time.
Did not work for me at all. I put the code below on the theme.liquid file. The only effect was the website would load really slow but ultimately still be in the same site, did not redirect also