Create a contact via API and associating with cookie based activity



We have a try page that launches directly into a Office 365 sign in page (similar to Google/Facebook OAuth page) that then redirects directly into our product.
We use the HubSpot API to create a contact based on the sign in user name (which is an email).

We also have the Hubspot tracker code in our product UI.

What we wanted to do is map the anonymous user that is tracked by the HubSpot tracker in the browser to the newly created contact.
I know we could force them to fill out a HubSpot form on the try page before sending them to the sign in page. but that is asking them for the email twice and isn’t a great experience and adds friction to signing up.

Is there a way in the API when creating a contact to use the tracker ID to let Hubspot map the web browsing activity?

Also, we can get the tracker ID in our product. Is there a way to see whether the contact already exists in HubSpot so we are just updating the record rather than creating a new one?


Hi @jthake,

It sounds like you could use the Identify a visitor method from the tracking code API:


this would allow me to see whether a user exists already as a contact.

But the scenario is more for when they ARE NOT a contact in HubSpot. But our tracker has been monitoring them in a browser for their activity around our site. I want to ensure that we capture that information as we create that contact in HubSpot via the API. Is this possible?


Hi @jthake,

The identify method actually works in both scenarios. If there is an existing contact record with a matching email address, the existing contact will be updated. Otherwise, a new contact record will be created. In both cases, the analytics data collected for the visitor will be associated with the contact record.


I’m not sure I’m being clear enough. If our product calls your API with just a email address. How does it know to match the analytics data collected to the visitor. IF the visitor has never filled in a form on our web site to tell hubspot their email address?


Hi @jthake,

The identify function is a method included with the HubSpot tracking code, and it automatically associates the current hubspotutk cookie with the contact corresponding to the information you provide. This is similar to the way that a form submission associates an anonymous visitor’s activity with a particular contact.


Hi, @Derek_Gervais

Follow up question,
Where will I put the _hsq.push(["identify", { {identity details} }]); ?
After we create the contact via API we will execute the code?



Hi @Reynan_Agapito,

Your questions are a bit broad and may be better suited to their own thread. That said, I'm happy to help!

What is your goal in using the identify method? Have you already installed the HubSpot tracking code on your site?

Are you creating contacts with the Contacts API? If so, what triggers a creation (e.g. a form submission)?


Hi @Isaac_Takushi,

Sorry for giving you unspecific question.
I'll do my best to explain it here.

I want to use "identify" method to send all the activities of a contact on browser to Hubspot.
Our Contact is being created via Contacts API,
Yes we already installed HubSpot tracking code on our sites.

here's our work flow.

  1. Visitor will visit HubSpot pages.
  2. Visitor will fill up a Custom HTML form (Form we have created manually not HubSpot form).
  3. Upon submission of the custom html form we will trigger the Contacts API to create a contact record for that visitor.
  4. After creating a visitor, I want to save all the activities of that contact before it was created as a contact. That's why I want to use the "identify" method.

Can we achieve that?

If you have more questions, please feel free to ask.

Thank you!


Hi, @Reynan_Agapito.

Thank you for clarifying!

I think that flow would work (similar to what is discussed here), however I see an even easier path.

Since your visitors are filling out a custom form, I recommend connecting it to the Forms API. If you pass the HubSpot utk cookie token value (hutk) in the context object, then HubSpot will associate all browsing activity to the contact as it is created. Instead of using both the Contacts API and the identify method, you would only have to use the Forms API.

Would this approach work in your situation? Let me know if I can clarify further.


Hi @Isaac_Takushi,

Thank you for you suggestion! I really appreciate it!

GOD Bless you


Happy to help, @Reynan_Agapito!


Hi @Isaac_Takushi, I felt it was more useful to post in this recent, relevant thread than to make an entirely new one. I'm trying to do a similar thing with my companies website. Our main website has hubspot tracking setup, and hubspotutk cookies are properly created when visiting that site. When a potential customer clicks on a link to to create a new account through our software (different site), I pass the hubspotutk cookie through the link to the other site. In the backend we use this cookie in the POST request to hubspot for our form. The form information appears to fill in just fine, but the hubspotutk doesn't seem to be properly associated since we always get X was created from Direct Traffic from No URL.

I have read varying ways to supply this depending on the situation (hs_context/context). I believe the latter is correct, but I've tried them both with no success.

The data is sent in the following format:

'email': ;,
'context': {
'hutk': self.hubspotutk,
'pageUri': '',
'ipAddress': '',
'pageName': 'Sample',
'firstname': self.firstname,
'lastname': self.lastname,
'lifecyclestage': self.lifecyclestage,
'key_contact': self.key_contact


Welcome, @_patrick!

Would you actually mind splitting this off into its own topic? We actually find it easier to keep track of and easier for other users to search. I'll take a look as soon as I can.