_hsq array:
_hsq array is processed when the tracking code is loaded, so you can push an identify call to the array before the code loads to have the identity information pushed with the initial trackPageView call.
Available Methods
The following methods are available in the Tracking Code API:identify- Identify a visitor by emailtrackEvent- Track eventstrackPageView- Track page viewssetPath- Set path for single-page applicationsaddPrivacyConsentListener- Check consent statusrevokeCookieConsent- Remove consent cookiesdoNotTrack- Prevent all tracking
Visitor identification
The HubSpot analytics tool identifies unique contact records using two pieces of data:usertoken (stored in the visitors browser in the hubspotutk cookie) and email. When the HubSpot tracking code tracks an action (such as a page view) for a visitor, it automatically associates that action with the usertoken. When you use the tracking code API to identify a visitor by email address, the analytics system ties that email to the usertoken, allowing HubSpot to update an existing contact record or create a new one. HubSpot validates the email address used for any process that would create or update a contact record. Analytics data (page views, original source, etc.) associated with the usertoken are displayed on the contact record.
When identifying visitors with the tracking code API, note the following:
- Only use the
identifymethod when you know the email address of the person viewing the page. Using a placeholder email for unknown visitors creates a contact record with that placeholder email and links the visitor’susertokencookie with that record. This means that all unknown visitors would be linked with that single placeholder contact moving forward. - The
identifyfunction only sets the identities in the tracker. The identities do not get passed to HubSpot until you make a separatetrackPageViewortrackEventcall. - The HTTP API cannot be used to link event analytics data with a contact record, but you can use the
email=parameter in the URL to link the event with an existing contact (if the email is associated with a record in HubSpot) or create a new contact (if the email address does not belong to an existing record).
Event tracking
ThetrackEvent function tracks legacy and new custom events in HubSpot reports. Events track that an action occurred, and can be linked to a contact record so that you can see when a contact triggered that event.
To track more complex processes on your website or track events that occur offline, use the events API.
Event IDs and names
Events can be triggered using a numerical ID or a string name. If you’re creating events in HubSpot, they’re automatically assigned a numerical ID, which is used to trigger that specific event. You can get the ID out of the URL when viewing the event in HubSpot. You can also trigger an event using a string name for the event. If you use a name, and there is not an existing event that already has that name, a new event with that name will be created dynamically. If an event with that name was already created dynamically, the new event will count towards that existing event. This allows you to keep dynamically creating new events without doing manual work in HubSpot.Tracking in single-page applications
The HubSpot tracking code automatically records a page view when the code is first loaded. You can manually track page views in a single-page application without reloading the tracking code. Use thesetPath and trackPageView functions to update and track the current page: