Hide the shipping banner based on visitor location
If you're running a split test, for example, only in the Germany & Austria then you likely will not want visitors from other countries to see a "Free shipping over €50" banner.
Below we have some javascript you can include that will hide the banner for all visitors from outside of Germany or Austria.
You will need to modify line #4 so that it's an array of the countries included in the current test.
We recommend placing this script just before the closing </head> tag but it can safely be placed anywhere including an external javascript file.
1
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/3.0.1/js.cookie.min.js"></script>
2
3
<script type="text/javascript">
4
5
/* Set these variables */
6
var shipScoutIncludedCountryCodes = ["DE", "AT"] //include alpha-2 country codes https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
7
8
/* Do not modify code below */
9
var shipScoutProcessCountry = function(countryCode) {
10
window._shipScout = window._shipScout || [];
11
_shipScout.push(function(response) {
12
// Check to see if the visitor's country is NOT in the included countries list
13
if (shipScoutIncludedCountryCodes.indexOf(countryCode) === -1) {
14
//hide the ShipScout banner
15
console.log("ShipScout: Banner hidden for visitors in " + countryCode)
16
document.querySelectorAll('.shipscout-banner').forEach(function(banner) {
17
banner.style.display = 'none'
18
})
19
}
20
})
21
}
22
23
//Check for "sc_country" cookie
24
if (Cookies.get('sc_country')) {
25
shipScoutProcessCountry(Cookies.get('sc_country'))
26
} else {
27
// Make a request to ipinfo
28
var shipScoutXhr = new XMLHttpRequest();
29
shipScoutXhr.open("GET", '/browsing_context_suggestions.json');
30
shipScoutXhr.onreadystatechange = function() {
31
if (this.readyState === XMLHttpRequest.DONE) {
32
if (this.status === 200) {
33
var data = JSON.parse(shipScoutXhr.responseText)
34
if (data && data.detected_values && data.detected_values.country && data.detected_values.country.handle) {
35
shipScoutProcessCountry(data.detected_values.country.handle)
36
Cookies.set('sc_country', data.detected_values.country.handle)
37
} else {
38
console.log('ShipScout: Error with country lookup')
39
}
40
} else {
41
console.log('ShipScout: Error with country lookup request')
42
}
43
}
44
}
45
shipScoutXhr.send();
46
}
47
48
</script>
Copied!
Copy link