# IP Address not capturing in GTM

**URL:** <https://community.hubspot.com/t/ip-address-not-capturing-in-gtm/119680>\
**Category:** APIs & Integrations\
**Tags:** developer-tools, events, forms\
**Created:** [November 1, 2024, 6:10pm UTC](https://community.hubspot.com/t/ip-address-not-capturing-in-gtm/119680 "2024-11-01T18:10:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnalyticsGirl](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@AnalyticsGirl](https://community.hubspot.com/u/AnalyticsGirl)\
**Post date:** [November 1, 2024, 6:10pm UTC](https://community.hubspot.com/t/ip-address-not-capturing-in-gtm/119680/1 "2024-11-01T18:10:55Z")

</div>

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](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,

---

<div class="post-metadata">

**Author:** ![SteveHTM](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stevehtm/32/22074_2.png) [@SteveHTM](https://community.hubspot.com/u/SteveHTM)\
**Post date:** [November 2, 2024, 12:07am UTC](https://community.hubspot.com/t/ip-address-not-capturing-in-gtm/119680/2 "2024-11-02T00:07:53Z")

</div>

@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:

```lia-code-sample
 field.value = new_value;
 field.dispatchEvent(new Event('input', { bubbles: true }));

```

Hope this is helpful!

Steve

---

<div class="post-metadata">

**Author:** ![AnalyticsGirl](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@AnalyticsGirl](https://community.hubspot.com/u/AnalyticsGirl)\
**Post date:** [November 4, 2024, 5:43pm UTC](https://community.hubspot.com/t/ip-address-not-capturing-in-gtm/119680/3 "2024-11-04T17:43:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SteveHTM](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stevehtm/32/22074_2.png) [@SteveHTM](https://community.hubspot.com/u/SteveHTM)\
**Post date:** [November 4, 2024, 7:08pm UTC](https://community.hubspot.com/t/ip-address-not-capturing-in-gtm/119680/4 "2024-11-04T19:08:49Z")

</div>

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
