Skip to main content
Use the HubSpot tracking code to track page views, identify visitors, and track events. Function calls are pushed into the _hsq array:
var _hsq = (window._hsq = window._hsq || []);
_hsq.push(["method", "arg1"]);
Anything already in the _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:

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 identify method 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’s usertoken cookie with that record. This means that all unknown visitors would be linked with that single placeholder contact moving forward.
  • The identify function only sets the identities in the tracker. The identities do not get passed to HubSpot until you make a separate trackPageView or trackEvent call.
  • 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

The trackEvent 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.
Please note:
  • Events created in the HubSpot app can only be triggered by ID.
  • Events created dynamically by name can only be created once. If you delete an event that was created this way, a new event will not be created if you try to dynamically trigger an event with the same name.

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 the setPath and trackPageView functions to update and track the current page:
<!-- Set up the path for the initial page view -->
<script>
  var _hsq = window._hsq = window._hsq || [];
  _hsq.push(['setPath', '/home']);
</script>

<!-- Load the HubSpot tracking code -->
<!-- Start of HubSpot Embed Code -->
  <script type="text/javascript" id="hs-script-loader" async defer src="//js.hs-scripts.com/{hubId}.js"></script>
<!-- End of HubSpot Embed Code -->

<!-- Tracking subsequent page views -->
<script>
  var _hsq = window._hsq = window._hsq || [];
  _hsq.push(['setPath', '/about-us']);
  _hsq.push(['trackPageView']);
</script>
Last modified on April 17, 2026