Skip to content

Instantly share code, notes, and snippets.

@tsertkov
Created October 23, 2018 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsertkov/6f4a508dd04dc9f26455d91c88db0229 to your computer and use it in GitHub Desktop.
Save tsertkov/6f4a508dd04dc9f26455d91c88db0229 to your computer and use it in GitHub Desktop.
Conditionally triggering custom tag (loading remote js script) based on user geolocation and other conditions.
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script id="myScript" type="text/javascript">
(function (){
var excludeCountries = [
'Russia'
]
var timeout = 10000
var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)no_ads\s*\=\s*([^;]*).*$)|^.*$/, '$1')
if (cookieValue !== 'true') {
$.getJSON('https://ipapi.co/json/', function(data) {
var countryName = data.country_name
if (excludeCountries.indexOf('country_name') !== -1) return
setTimeout(function () {
document.cookie = 'no_ads=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'
triggerTag()
}, timeout)
})
}
function triggerTag() {
var scriptTag=document.createElement("script")
scriptTag.type="text/javascript"
scriptTag.charset="utf-8"
scriptTag.src=("//scripts.example.com/xxx.js")
var s=document.getElementById("myScript")
s.parentNode.insertBefore(scriptTag, s)
}
})()
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment