Remove the Chat based on country codeUpdated 5 months ago
If you only work with certain countries you might want to only handle Chat messages coming from those countries.
The code below hides the Chat for ALL countries except United States and Mexico:
1<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>2<script>3// set your ipstack API key4var ipstackAPIKey = 'YOUR_API_KEY'5// set your country code whitelist6var countryCodeWhiteList = ['US', 'MX']7// set this to false if you only have a free tier account for ipstack8var useHttps = true910var initGorgiasChatPromise = (window.GorgiasChat) ? window.GorgiasChat.init() : new Promise(function (resolve) { window.addEventListener('gorgias-widget-loaded', function () { resolve();})});1112function removeGorgiasChat(countryCode) {13 initGorgiasChatPromise.then(function() {14 var container = document.querySelector('#gorgias-chat-container');15 if (container && !countryCodeWhiteList.includes(countryCode)) {16 container.remove()17 }18 })19}2021var countryCode = window.localStorage.getItem('countryCode')22if (!countryCode) {23 protocol = useHttps ? 'https' : 'http'24 $.ajax({25 url: protocol + '://api.ipstack.com/check?access_key=' + ipstackAPIKey + '&fields=country_code',26 dataType: 'jsonp',27 success: function (json) {28 if (json && json.country_code) {29 countryCode = json.country_code30 } else {31 countryCode = 'unknown'32 }33 window.localStorage.setItem('countryCode', countryCode)34 removeGorgiasChat(countryCode)35 },36 error: function (request, status, error) {37 console.error(request.responseText);38 window.localStorage.setItem('countryCode', 'unknown')39 removeGorgiasChat('unknown')40 }41 })42} else {43 removeGorgiasChat(countryCode)44}45</script>
Instructions for Shopify
1. Go to your Shopify home screen.
2. Click on Online Store on the left.
3. Select Themes → Actions → Edit code.
4. Under Layout, choose theme.liquid.
5. Copy the code above and paste it on the code text box.
6. Enter your Ipstack API key where the code says YOUR_API_KEY.
7. Enter the country code/s where you only want to display the Chat widget where it says ['US', 'MX'].
8. In the part where it says 'var useHttps = true', change it from true to false if you're using a free account on Ipstack.
9. Click on Save.
The Chat won't be available to anyone except for shoppers from the countries you entered.