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,