0

I'm trying to block internal IP and have to add a code which can give me this :

<script type="text/javascript">
 dataLayer = [{
 'visitorIP': '155.55.155.155'
 }];
 </script>

Normally i should create it using my server and php because i can't get visitorIP with only js in GTM, but i have to add this js into a gtm variable :

function() {
 var newIP = '{{visitorIP}}';
 //INTERNAL TRAFFIC
 var patt = new RegExp("^28.28.128.1[0-9]?$");
 if(patt.test(newIP) == true) {return('internal');}
 //MONITORING SYSTEM
 patt = new RegExp("^65.65.65.12[0-9]$");
 if(patt.test(newIP) == true) {return('monitoring');}
 return('external');
 }

And i was thinking of getting easily this IP directly in GTM using a code like this and not datalayer, or directly add datalayer code with the function in the GTM variable. To do this i was thinking using something like this :

$.getJSON('https://api.ipify.org?format=json', function(data){
    console.log(data.ip);
});

But it's jquery and i don't want GTM to override my website jquery version using this, so i would like in js ; and i don't know how to implement this to have a variable retrieving only the ip in js and using it in my script to have something like this :

function() {
getJSON('https://api.ipify.org?format=json', function(data){
    var visitorIP = data.ip;
});
 var newIP = visitorIP;
 //INTERNAL TRAFFIC
 var patt = new RegExp("^28.28.128.1[0-9]?$");
 if(patt.test(newIP) == true) {return('internal');}
 //MONITORING SYSTEM
 patt = new RegExp("^65.65.65.12[0-9]$");
 if(patt.test(newIP) == true) {return('monitoring');}
 return('external');
 }

Do you have an idea ?

Playzare
  • 93
  • 3
  • 13
  • Possible duplicate of [Download JSON object as a file from browser](https://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser) – Tibrogargan Jun 09 '18 at 22:12
  • or [how to get JSON with pure JS](/questions/20743186/get-json-array-with-pure-js) – Tibrogargan Jun 09 '18 at 22:20
  • or [Send/Retrieve JSON](/questions/24468459/sending-a-json-to-server-and-retrieving-a-json-in-return-without-jquery) – Tibrogargan Jun 09 '18 at 22:25
  • But i don't understand how to use it, and jquery to js is just a small piece of my question :/ – Playzare Jun 10 '18 at 11:00

0 Answers0