IP Address not capturing in GTM

Hi,

I am wondering if anyone can help me. We are trying to pass the ip address through a hidden form field. The ip address field name: ipaddress.

This is the code we are using in GTM - however the IP address isn’t being captured in Hubspot but the FB click id is.

<script>
// Function to read a specific cookie value by name
function readCookie(name) {
var nameEQ = name + “=”;
var ca = document.cookie.split(‘;’);
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}

// Function to set the IP address value in the hidden field
function setIPAddress(ipAddress) {
console.log(‘IP Address:’, ipAddress);
var ipField = document.querySelector(“input[name=‘ipaddress’]”);
if (ipField) {
console.log(‘Found hidden IP address field:’, ipField);
ipField.value = ipAddress;
console.log(‘IP address set in hidden field:’, ipField.value);
} else {
console.error(‘Hidden IP address field not found.’);
}
}

// Function to set the fbclid value from the URL parameter
function setFbclid() {
var urlParams = new URLSearchParams(window.location.search);
var fbclid = urlParams.get(‘fbclid’);
console.log(‘FBCLID Value:’, fbclid);
var fbclidField = document.querySelector(“input[name=‘fbclid’]”);
if (fbclidField) {
console.log(‘Found hidden FBCLID field:’, fbclidField);
fbclidField.value = fbclid;
console.log(‘FBCLID value set in hidden field:’, fbclidField.value);
} else {
console.error(‘Hidden FBCLID field not found.’);
}
}

// Function to set the _fbp value from the cookie
function setFBPCookie() {
var fbpValue = readCookie(‘_fbp’);
console.log(‘FBP Cookie Value:’, fbpValue);
var fbpField = document.querySelector(“input[name=‘fbp’]”);
if (fbpField) {
console.log(‘Found hidden FBP field:’, fbpField);
fbpField.value = fbpValue;
console.log(‘FBP value set in hidden field:’, fbpField.value);
} else {
console.error(‘Hidden FBP field not found.’);
}
}

// Function to fetch IP address from ipify API
function fetchIPAddress() {
console.log(‘Fetching IP address…’);
var script = document.createElement(‘script’);
script.src=“https://api.ipify.org?format=jsonp&callback=getIP”;
script.onerror = function() {
console.error(‘Failed to load the ipify script.’);
};
document.body.appendChild(script);
}

function getIP(json) {
console.log(‘IP address fetched:’, json.ip);
dataLayer.push({“event”:“ipEvent”,“ipAddress” : json.ip});
setIPAddress(json.ip);
}

// Function to initialize form field injections
function initializeFormFieldInjections() {
fetchIPAddress();
setFBPCookie();
setFbclid();
}

// Wait for the DOM content to be fully loaded
document.addEventListener(‘DOMContentLoaded’, function() {
console.log(‘DOM fully loaded and parsed’);
// Wait for the form to be present in the DOM
var formCheckInterval = setInterval(function() {
var form = document.querySelector(‘form’);
if (form) {
console.log(‘Form is present’);
clearInterval(formCheckInterval);
initializeFormFieldInjections();
} else {
console.log(‘Waiting for the form to be present…’);
}
}, 100); // Check every 100ms
});
</script>

Any thoughts as to why it wouldn’t capture the ip address?

Thank you,

@AnalyticsGirl - you are not very explicit here about where your error occurs. Which console logs succeed and which fail? HubSpot decided a while back that it would not permit IP addrress capture through its own mechanisms, but you may well be working around that issue here if most of the logging operates as expected.

But I do think that you may be missing a specific step after update of the input value - making sure the form logic sees that new value. my code uses:

 field.value = new_value;
 field.dispatchEvent(new Event('input', { bubbles: true }));

Hope this is helpful!

Steve

Thanks for your feedback Steve.

The error in the console log is “hidden field not found”

But other then that no other error and the Fbclid does work.

I suggest taking a close look at the HTML structure of the form to see whether you are using the right identifier to find the input field DOM object. I found that hidden fields get structured somewhat differently to those which are visible on forms and I needed to apply some hueristics to get to the fight identifier.

Steve