Capture IP Country on Non-Hubspot forms

Hello, we have a free account signup on our website that is our main source of leads. The signup flow is not a hubspot form, so we use the apis to send the data from it to hubspot.

However, we haven’t been able to figure out how get the IP country, IP City, and IP state/region values to populate with this method. Does anyone know how to accomplish that?

Hi @KHill15,

Thank you for reaching out to the Community!

I’ll move this post to the API board to make sure it’s seen by the right experts.

Hi @himanshurauthan, @tjoyce, @dannio, do you maybe have any input on this matter? Thank you!

Cheers
Mia, Community Team

Hello @MiaSrebrnjak and @KHill15,

As you have mentioned that you are using a non-HubSpot form and using API to send data to the HubSpot.

What you can do is simply add the ‘ipAddress’ index in your data and you can pass user system IP to the ‘IP address’ field.

$ip = isset( $_SERVER[‘REMOTE_ADDR’] ) ? sanitize_text_field( wp_unslash( $_SERVER[‘REMOTE_ADDR’] ) ) : ‘’;

$form_data[‘ipAddress’] = $ip;

You can also check the doc for form data submission.

I would suggest is to use the form submission API if you weren’t using it already. The normal contact creation API doesn’t support IPs or cookies, and you’ll miss a lot of the tracking information. However although this records the IP of the contact, I’m not sure if that carries over to the other IP fields.

The docs can be found here:

https://legacydocs.hubspot.com/docs/methods/forms/submit_form

So what you would do is create a form in hubspot, and map the form fields in your current form to the hubspot form on submission.

Here’s an example (ajax call via jquery):

data = 
 {
 "fields": [
 {
 "name": "email",
 "value": "test@test.com"
 },

 ],
 "context": {
 "hutk": "xxxxxxxx", // Hubspot Cookie value - hubspotutk
 "pageUri": "*url of submission page*",
 "pageName": "Booking Form",
 "ipAddress": "*browser ip address"
 },
 "skipValidation": true
		}

 $.ajax({
 type: "POST",
 url: 'https://api.hsforms.com/submissions/v3/integration/submit/*account ID*/*form ID*',
 contentType:"application/json;",
 dataType:"json",
 headers: {
 "accept": "application/json",
 "Access-Control-Allow-Origin":"*"
 },
 data: JSON.stringify(data),
 success: function(result){
 console.log(result)
 },
 error: function(xhr,status,error) {
 console.log(error);
 }
 });

Thanks for the tip LPM. We’re actually using the Tracking Code API. Do you know if that supports the ipAddress field? It doesn’t seem to from our testing.