Email tracking: add external ID or custom field



There is a use case that seems possible from the API docs, but I wanted to ask just in case.

If I install the tracking code on a page, and enable identity tracking, am I then able to use the identify method of the JS API to add an additional external ID to the contact that is already identified because he visited via an email?

The specific docs I am referring to are here:

I am assuming I would do the following:

_hsq.push(["identify", { { id: "my_external_id" } }]);

Is my assumption here correct? I would just like to ensure that calling "identify" without an email here will set the external ID, and it will be accessible via the REST API.

Thanks in advance!


Hi @elsurudo, the ID that you'd push with the identify function is completely external and cannot be accessible via API. You can use the identify method to associate your external ID to the contact to effectively identify your contact in both your external system and HubSpot. From the docs:

this 'id' is treated as a completely external identity, so while analytics data can be associated with a specific contact record by the id (if, for example, you've previously identified a record by id and by email, or the record was previously identified by id and the visitor also has a form submission), the contact record cannot be looked up by this id.

The use case would be if you have an external system that has an identifier for a contact and you may or may not have their email address in hubspot. you can use the known value of their ID to identify the contact and attribute their page views etc to that id. If they convert on a form later, you can then know who the visitor is in both HubSpot and your external system.


Hi @Connor_Barley, thanks for the answer.

If I am understanding correctly, when a user converts on a page via a Hubspot email link with identity tracking enabled, the Hubspot JS tracking script can match that user's identity to the Hubspot Contact with the email address from which the visit came, and the Contact can be effectively identified.

More concretely, what I am asking is: at the point of the page visit, how can I then add an external ID (or any other data, for that matter) to that particular Hubspot contact, via the Javascript API?


Hi @elsurudo, you'd have to write to a specific HubSpot contact property in order to add data to a contact. For example:

var _hsq = window._hsq = window._hsq || [];
    email: '',
    firstname: 'Identify Test Email 1',
    lastname: 'From Page 2'


Hi @Connor_Barley, I'm not sure I understand.

Let's say the user converts on the page via email (with identity tracking enabled), but the page itself doesn't know the identity of the user (in your example above, the email address).

Can I still add additional data to the Hubspot contact without knowing the email address? It was my understanding that this was the purpose of identity tracking – that we can track the conversion of the user, and then track additional context via the Javascript API.


Really, what I am trying to do is:

GIVEN the page has the tracking code installed, and identity tracking is enabled

WHEN a user visits the page via an email campaign

THEN I want to save some piece of data to the Contact that converted to his Contact record in Hubspot
AND I want to be able to pull that data back out via API

One option was using event tracking (_hsq.push(["trackEvent" ...), but according to this post, it is not possible to get these events via API...

Could you confirm whether what I am trying to do is even possible?


Hi @elsurudo, if your user is converting from a marketing email sent to them, you could include a query parameter in your links like this when sending your emails to contacts.

<a href="{{}}">Click!</a>

This way, they'll have their email in the URL as a query parameter. On the page you could pull off that value with some JS, then use that inside the identify call.

For your next question, you can definitely send data to HubSpot using the track event function, but yes you're right that this data cannot be pulled programmatically from HubSpot. If you're tracking something specific, or are looking to simply bucket contacts that complete some sort of event, you could use the Lists tool to segment. Otherwise, I'd recommend perhaps creating a custom contact property, and including that within your identify call like so:

var _hsq = window._hsq = window._hsq || [];
    email: getParameterByName("email"),
    custom_property: "custom prop value goes here"

You could then pull this data via the Contacts API. Be aware that most of our APIs do not Support CORS/AJAX calls, so the call to the contacts api would have to happen on your servers.


Hi @Connor_Barley, that query param workaround seems like it could work and I will test it out. Thanks for the tip.

However, it is a bit error-prone, as the query param would need to be added to each link in the email manually, right? We would ideally be adding this to every single link in every single campaign – is there any way to do something like this automatically?

My other concern is: what if someone visits the page and adds that query param to the end of the URL themselves? Or visits from an email with their own email as the param, but then sends off another request with a different email address. I know there is a hubspotutk cookie set from emails when identity tracking is enabled which identities the user in some form – would this prevent this kind of "visit forgery", and subsequent setting of the value – unless the cookie matches the correct email?


Hey @elsurudo, yes that'd have to be done manually on each link so you're justified in your concerns. There isn't a way to do this automatically. If you're sending customers a marketing email > a page with the tracking code on it and you've enabled identity tracking, they will be attributed the proper website activity, you just wouldn't be able to push new properties to the contact via the tracking code api, which is why that api isn't really designed to do this. The identify function is really supposed to be used when you have their contact info in your system, but may or may not know their email address. A prime example would be if you have login functionality and want to associate their record in HubSpot to their email.

Every marketing email in an account that has enabled identity tracking will append query parameters to links that are in the email itself. One of those parameters is the _hsenc parameter which stands for HubSpot Email Encoded. It's a long string of numbers and letters that, when decoded on our end (you cannot decode this yourself due to privacy reasons), equates to the contact's email address. The tracking code then recognizes this string and is able to either set the hubspotutk for a contact if they've never visited your site, or associate the correct hubspotutk with that user and identify them.

Overall, I don't think your end goal is possible without using that workaround I mentioned since you'd have to use that email query parameter on each link, then identify the contact and push a custom property to that contact as well. Is the data you're looking to track something that would even be appropriate to be set on a contact record as a custom property? What data are you trying to push to a contact?