Create Contact Using JavaScript Tracking Code API


I am trying to create a contact using javascript tracking code api.
I have used
var _hsq = window._hsq || [];

in my footer. Does this code has to be added before loading tracking code js or can be added after the tracking code ? if the contact already exists, then tracking is working as expected as I can see the page visited by the user’s. Is there is any limitation in using the JavaSript API?


Hi @manojmohan,

This function call stores the data in the tracker, but the data is not actually passed to HubSpot with this call. The data will only be passed when tracking a pageview or an event (with either the trackPageView or trackEvent functions).


Hi @Derek_Gervais,
I have tried pushing the trackPageView after the above code, but it doesn’t create the contact. I also observed that once the contact has been created and then if you delete the contact that has been create, then try to create through API. It doesn’t create at all. Is there any limitation in creation process of contact?


Hi @manojmohan,

Just to make sure that we’re on the same page, you’re asking about creating a contact, deleting that contact, then trying to create it again through the tracking code API? Can you send me an example of a contact that you’re having trouble creating via the tracking code API since it was deleted from your portal?


Hi @Derek_Gervais,
Sorry for the delayed response. My exact problem is if I delete contact from my hubspot contacts, and if I visits the page with email id and pushing it to the hubspot using _hsq.push([‘identify’,{‘email’:emailid}]); i.e. js code to track pages, the contact won’t not be created. If during the same process, if new email id has been pushed using js code, it is creating the contact. Any idea why such behaviour occur? Is there any trash where the contact would have been after been deleted in the contacts in hubspot?


Hi @manojmohan,

We track deleted records internally and prevent the tracking code from creating new analytics events for deleted records. This means that if a contact is deleted from your portal, you won’t be able to re-create them using the tracking code API (i.e. the 'identify' method). Other methods can still re-create contact records, like form submissions or the contacts API. There isn’t a ‘trash’ or anything similar in HubSpot, so you won’t necessarily be able to track deleted records. If there are specific records that you believe have been deleted, you can contact Support and they can help you investigate.


Hi @Derek_Gervais,
Thank you for the quick response and support. I believe if there is a feature to add deleted contacts through contact API, why not through tracking code API? The behaviour should be same for every API I suppose.


Hi @manojmohan,

It’s due to the different use cases of each API. The contacts API is generally used for creating offline contacts, similar to creating a contact manually via the UI. If a HubSpot user deleted a contact, and then manually added the contact again via the UI, HubSpot would allow the creation (since it’s clear given the user’s actions they want to recreate the contact). This is mirrored in the way the contacts API functions.

The tracking code API is different, though; with the tracking code API, contacts can be created as a result of normal visitor tracking. If a contact is deleted by a user, we’ll stop collecting their analytics info. Since the tracking code API contact creation occurs as a result of the normal visitor tracking, we’ll also stop collecting that. This mirrors the way the existing visit/page view tracking functions. Note that if the contact submits a form, or is created by a user, the analytics info will still exist and continue being collected.

I hope that helps clarify the difference in API behavior. If you have other questions or would like advice on dealing with edge cases like these, please let me know.


Hi @Derek_Gervais,
Thanks for the clarification. I think your statement should have been mentioned in the documentation.


Hi @manojmohan,

Completely agree, I'll work on updating the docs to make them more clear.