Capturing User IP Address in Web Analytics

Grabbing the IP address is something that many web analytics systems do not expose to the UI and require either back end processing rules (Adobe Analytics) or adding in via logic (Google Analytics). This blog posting will provide you the necessary jQuery to do this.

To start, you will need to make sure you are including the jQuery library so that you can be certain that the code will work.

Adobe Analytics (within appMeasurement library):

//capture IP address via custom code
s.eVarXX = GetUserIP();
if (s.eVarXX) { s.propXX = “D=vXX”; //replacing the XX with the eVar number

function GetUserIP(){
var ret_ip;
$.ajaxSetup({async: false});
$.get(‘https://api.ipify.org/?format=json’, function(r){
ret_ip = r.ip;
});
return ret_ip;
}

You can also create a data element within Adobe DTM and then reference the data element within a global page code rule to grab the value.

Google Analytics:

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());

//custom code
function GetUserIP(){
var ret_ip;
$.ajaxSetup({async: false});
$.get(‘https://api.ipify.org/?format=json’, function(r){
ret_ip = r.ip;
});
return ret_ip;
}

gtag(‘config’, ‘GA_TRACKING_ID’, {
‘custom_map’: {‘dimension1’: getUserIP()}
});

gtag(‘config’, ‘UA-56232019-2’);

That’s it! With this in place you will be able to capture end user’s IP addresses and have that as a variable within your web analytics tool for reporting and analysis purposes.