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 trackCustomBehavioralEvent 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).

Custom event tracking

Please note: trackEvent has been deprecated and no longer works. Use trackCustomBehavioralEvent instead.
The trackCustomBehavioralEvent function tracks custom events for reporting, analytics, and more. 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. You can also send event data via the events API.

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 June 10, 2026