Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<script>
function matchIP(ipaddress) {
// Comment following three lines if you want to use the IP range method
if (ipaddress == "111.222.333.444") {
dataLayer.push({"internal": "true"});
}
// To use the following IP range check, comment the previous three lines
// of code and uncomment the following lines
//
// var ipRange = ipaddress.split(".");
// var lastOctet = parseInt(ipRange.pop());
// if(lastOctet >= 1 && lastOctet <= 100) {
// dataLayer.push({"internal": "true"});
// }
}
var ipaddress = "";
try {
jQuery.ajax({
type : "GET",
dataType : "json",
url : "http://api.hostip.info/get_json.php",
async : true,
success : function(data) {
ipaddress = data.ip;
},error : function(errorData) {
ipaddress = "none";
},complete : function() {
matchIP(ipaddress);
}
});
} catch(e) {
console.log("Oops, something went wrong: " + e.message);
}
dataLayer.push({"event": "ipComplete"});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment