Advanced Cookie Based Web Tracking in HubSpot
Most websites use cookies to track visitor behavior and page performance, and HubSpot can be utilized for the most common use cases with almost no configuration. I will use this article to demonstrate some of the more advanced features of HubSpot's cookie tracking, and how you can gain even more control of this data. Better for you and better for your users.
In this article:
- Installing the tracking code, and basic tracking
- The layers of data quality
- Individual page views
- Anonymous users
- Known users
- Cross domain tracking
- Advanced tracking features
- the _hsq object
- Identifying users
- Recognizing anonymous visitor
- Registering known user
- Tracking pageviews
- Custom events
Installing the tracking code, and basic tracking
As this article will focus primarily on the advanced features, I will keep the basic introduction short. HubSpot used an account specific tracking code to initiate web tracking, and this must be installed on your website before you can continue and start tracking your web performance.
See HubSpot's article Install the HubSpot tracking code to learn more about how to implement this on your website.
Once this is installed on your website, you are already automatically tracking the basic web performance metrics, and will be able to create reports with this data in HubSpot. For most companies, this will be sufficient to start reporting on your key web metrics.
Make sure you have also enabled a cookie banner on your website, in order to comply with privacy regulations like GDPR and CCPA.
More info: Set up your cookie tracking settings and consent banner
The layers of data quality
You will mainly have three layers of data quality, depending on the consent and the information you have about the active user. You will not necessarily be able to differentiate between these in your tracking, which is also why web performance tracking should only be considered a reliable indication of you website's activity that will never be 100% accurate.
Individual page views
For users that don't consent to using cookies to track their data, you will not be able to register any context. Each pageview of click this user makes will still be tracked and reported, but you will not have any information about which of these actions are made by whom, or even by the same user.
Anonymous users
Anonymous users are recognized by the tracking scripts and have an anonymous ID registered to them. This allows us to know how activities are tied together, and if several pageviews are made by the same person as opposed to many different people.
Known users
Known users are recognized by the tracking script and have given a consent to tracking their activity on the website. They have the same anonymous ID as the other users, but have made actions that tie their anonymous ID to a specific known person in your CRM. You will be able to see individual activities these users make in their contact record in HubSpot.
Cross domain tracking
If you are tracking data from multiple domains, i.e. from a website and a platform hosted separately, or similar, you should set up your domains in HubSpot to allow cross domain tracking.

Go to your HubSpot account -> Settings -> Account management -> Tracking & analytics -> Tracking code -> Advanced tracking, and add all the relevant domains. Keep in mind that the tracking code also needs to be installed on all of them for this to work.
Advanced tracking features
Now that all the basic out-of-the-box tracking is up and running, you can also use scripts on your website to push additional context or information to the tracking code, in order to register extra activities, or add necessary context the the visitor.
the _hsq object
When the tracking code is loading correctly on your website, it will define a _hsq object that contains a single push() function. You can use this function to push data to HubSpot's tracking, similar to what you could potentially do with apps like Google Tag Manager.

You can easily check if this object is defined by opening it in the console and checking. If this object is not defined, your tracking code is not implemented correctly.
Identifying users
HubSpot will automatically identify users when they submit a form or similar, but sometimes you may have access to this information without HubSpot knowing about it. One example is if you are tracking activity on a restricted page or an application, where the user must be logged in.
Recognizing anonymous visitor
You can use an ID to regognize a user, and add context to other activities made by the same user without registering their identity or adding them to your CRM. Push an identity to your _hsq where you include any ID, and new activity where the same ID is used later will be registered as the same visitor.

This ID can be anything, and does not have to be a number.

Registering known user
If you know the identity of your visitor and wish to register this to your CRM, you can use the same function to push their email address.

This will create a new user to your CRM if it does not exist already. When you identify a user like this, all known activity back in time will also be registered to the same user, so you can experience activities like pageviews suddenly appearing in your contacts that happened weeks ago or longer.
Tracking pageviews
You can track additional pageviews manually with the same push() function. Be aware that HubSpot will automatically track a page view every time a new page loads, so tracking this manually could lead to double tracking.
However there are some cases where you may want to track extra pageviews. If your website is an SPA it will typically only load once, and not re-run the tracking script when you switch between pages, this is a typical use case for tracking extra page views with a separate script. There could also be other cases like wanting to track separate tabs like separate pages, or even to add more context to your pageview log.

As you can see in this example, I am not only tracking a pageview, but I will first specify the path I want to track. Here I can add whatever contact I want, like anchors, or UTM parameters that were not originally there for the visitor. Be aware that HubSpot will not control if the page actually exists so you can track views on pages you don't have.
Custom events
If you have an Enterprise license, you can also push custom events tot he tracking script that will be associated to the active visitor.

There are a lot of possibilities in how to configure custom events, so please see HubSpot's guide on how to Create custom events.