Created
October 23, 2018 08:04
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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