How to detect a visitor’s country of origin on your Shopify store
There are a bunch of reasons why you’d want to know what country a visitor is coming from on your website. If you want to detect a visitor’s country of origin on your Shopify store. Please try with the bellow Javascript code.
1 2 3 4 5 6 7 | $(document).ready(function(){ $.get('//www.cloudflare.com/cdn-cgi/trace', function(data) { var country_code = data.replace(/(\r\n|\n|\r)/gm,"").split('loc='); country_code = country_code[1].split('tls='); country_code = country_code[0]; }); }); |
This Javascript code will return the 2 letter country code. Eg: VN (Vietnamese), US (United States),… You can refer to the article here to know country codes of all countries in the world.
In fact, you must combine with another script to have the feature you want as: block an entire country from hitting your site or country specific redirects.