Track Page View

_hsq.push(['trackPageView']);

Track a page view. This function is called when the tracking code is loaded on a page, but you can manually call this function to track subsequent views in a single page application.

This function does not support any arguments.
The title tracked will be the current value of document.title

The URL tracked is based on one of either:

  • the path set using the setPath function.  If you're site is built as a single-page application, this function is the preferred method of setting the tracked path.
  • If setPath has never been called, the tracked URL will be the Referer HTTP header in the request being made by the visitor's browser to HubSpot's tracking servers (modifying the browser history state would update the value used for that header).
    IMPORTANT NOTES:
    • Relying on the Referer header will not support URL fragments (anything after the # in the URL), as fragments are not included in the Referer header.
    • Any path set using the setPath function will override the data in the Referer header, so if you call setPath once, you will need to use setPath to update the path for each view you want to track.

Note - There are a few things to keep in mind when using this function:
  • As mentioned above, this function is automatically called when the tracking code loads, and there is not a way to prevent that initial call. Calling this function manually before or during the initial page load could lead to duplicate views being tracked.
    • While that call cannot be prevented, you can control the URL recorded for the page by pushing a setPath call into _hsq before the tracking code is loaded. See the Tracking Code overview page for an example of this.
  • We recommend against including a <link rel="canonical"> tag in your page if your site is a single-page application. If your page uses <link rel="canonical"> tags, you'll need to use the setPath function to update the path of your new pages, as the canonical URL set from the link tag will override any detected URL if you're only updating the browser history state.