How to track Hotjar in Google Analytics (and Why)

Hotjar Recordings Dashboard

If you are working with digital optimization, you have probably already heard about Hotjar, an “All-in-one Analytics and Feedback solution“. Hotjar works well on it’s own, but the value of the tool increases if you integrate it with Google Analytics.

In this blog post I demonstrate how to integrate Hotjar in Google Analytics using Google Tag Manager, and I also give you some examples of why it’s useful.

!!! UPDATE !!!

This blog post was updated 14th of March 2016. Earlier this blog post described how to get the Hotjar User ID from the Hotjar cookie. Hotjar doesn’t use this cookie anymore, so this method stopped working. This updated blog post now describes a working solution again.

!!! UPDATE 2!!!

I have written about a more accurate solution for tracking the Hotjar User ID (but it’s also more complex). See my blog post Hotjar userId tracking in Google Analytics with Google Tag Manager v2 for an improved solution.

!!! END UPDATE !!!

Tracking Hotjar in Google Analytics using Google Tag Manager

The first thing you should do if haven’t done it yet is to head over to Hotjar, create an account, and add the Hotjar-script to a Custom HTML Tag in Google Tag Manager. Then we are going to track the Hotjar User ID in Google Analytics.  It’s especially Recordings that makes it useful to track the Hotjar User ID.

The advantage of tracking the Hotjar User ID in Google Analytics, is that you now can use Google Analytics to identify behavior that Hotjar can help you answer. Later in this blog post I demonstrate how this can be used in Customer Service, but I have also had really great help from this tracking in webshop checkouts combined with Error Message tracking. Combine Error Messages tracked in Google Analytics with the Hotjar User ID, and you have Gold!

Hotjar Recordings Dashboard

Getting the Hotjar User ID

To get the complete Hotjar User ID you can access it using the code below.

The Hotjar userId contains a value similar to this: 41d7c48b-8689-42dd-9a79-83690993f2ee.

However, this value isn’t available on Page View or DOM, you will have to until Window Loaded in Google Tag Tag Manager to get the User ID according to my experience. This means that it can be tricky to send this value to Google Analytics. To overcome this problem I use a Custom HTML Tag to store the Hotjar User ID in Local Storage, and then I read the Local Storage value into Google Analytics.

It’s one drawback with this method, if the user bounces, you will not be able to track the Hotjar User ID (but that user is probably not interesting to anyway).

Reading the Hotjar User ID from Local Storage

To make this setup it a little bit easier, we will first create a Custom Javascript Variable that reads “our” Hotjar User ID from Local Storage. Name the Variable Hotjar User ID.

Storing the Hotjar User ID to Local Storage

Create a Custom HTML Tag, and add this code into the tag. Call this tag for Hotjar Store User ID.

We are only interested in the first part of the value, namely 41d7c48b. This is the Hotjar User ID you find in the Hotjar interface. The code above stores only that value to Local Storage.

This tag requires 2 different triggers. 1 trigger to fire the tag, and 1 trigger to block the tag.

Trigger to fire the Hotjar Store User ID tag

Google Tag Manager - Window Loaded

Trigger to block the Hotjar Store User ID tag

The purpose of the blocking trigger is that it is no point in loading this tag if the Hotjar User ID already is stored in Local Storage. We therefore simply check if the Hotjar User ID Variable is undefined.

GTM Trigger - Hotjar User ID does not equal undefined

Google Analytics setup and tracking

Then head over to Google Analytics and create a Custom Dimension with Scope set to Session. The S: in the Dimension name simply means that this is a Dimension with the Scope set to Session.

Google Analytics Hotjar Dimension

If you are using many Dimensions and have many different Google Analytics Tags in Google Tag Manager, it can be useful to add the Dimension Index to a Variable as well. Below is my {{Hotjar User ID – Index}}. The Index number is derived from the Google Analytics Dimension settings.

Hotjar User ID Index in Google Tag Manager

The next thing to do is to add the {{Hotjar User ID}} to your Google Analytics Tag in Google Tag Manager (3). Since the scope of the Dimension is set to Session, it’s normally enough to add the dimension to your generic Page View Tag. But remember, since we are storing the Hotjar User ID on Window Loaded, it can be wisely to add this dimension to other Google Analytics Tags in GTM also (if you have other Google Analytics Tags in your GTM setup).

This is what it takes to track the Hotjar User ID in Google Analytics.

In the Tag setting below I have highlighted two other Google Analytics Dimensions, {{userId}} (1) and {{orderID}} (2). Keep on reading if you want to know why I have highlighted those dimensions.

Google Analytics Tag setup with Hotjar Dimension

Using Hotjar in Ecommerce Customer Service

As I wrote, it’s normally enough to add tracking of the Hotjar User ID to the generic Page View Tag. The screen shot above is however from a special setup that tracks a AJAX based checkout in a webshop. What I’m tracking here is the Google Analytics UserId (1) and the Order ID (2).

The Order ID in a webshop is usually generated in the checkout, and by tracking this number in Google Analytics it can help you improve your customer service with the help from Hotjar.

I wasn’t able to complete my purchase. Is your webshop broken?

Sometimes a webshop receives emails like this.

If you have a good Google Analytics Enhanced Ecommerce setup and tracking of Error Messages, maybe you can find the answer there. With tracking of Order ID and Hotjar User ID, you have more options to find out what the problem was, fix it, and give the person a helping answer.

If this is a registered customer you can use the UserId, but if the person checked out as “Guest” you will have to check the Order ID. Log into backend of the webshop, and you will hopefully¹ find the person in the “Abandoned Cart Report”. In this case you find the person, and you see that the Order ID is 105641. A quick Custom Report in Google Analytics shows that the Hotjar User ID is 11f1e312.

Now log into Hotjar and see if you have a recording² for that ID. If you have a recording for that person, it is a big chance it will help you identify the problem and help you answer the “is your webshop broken” question.

Google Analytics Custom Report with Order ID and Hotjar ID

¹You will not always find the person in the “Abandoned Cart Report”. It depends on where in the checkout the person abandoned.
²Hotjar may sample your traffic. When this sampling occurs depends on your volume and your Hotjar plan. With a smart setup, sampling shouldn’t be an issue if you have the right Hotjar plan.

Since a checkout may contain sensitive user data, remember to mask fields in Hotjar that contains that.

When customers just abandon the checkout, and never send an email about the problem

To increase conversions from abandoned carts, and to increase emails from persons that weren’t able to complete a purchase, a good tactic is to use shopping cart abandonment email marketing. Include some text in the email that encourages the person to reply to the email if they had some trouble with completing the purchase.

To sum up, Hotjar is superb on its own, but integrated with Google Analytics you can get even more value from the tool.

7 Comments on "How to track Hotjar in Google Analytics (and Why)"

  1. Great article — thanks for the detailed instructions! Curious, do you know if Hotjar has made any changes to the cookie -> user id relationship? I’ve implemented the steps above and am getting the 8 digits Hotjar id in GA now, but those IDs are not matching any of the user IDs in my Hotjar recrordings.

    • Hotjar did change their tracking so my method did break.

      I have (today) updated this blog post with a new working solution.

  2. Great article, Eivind. Very cool.

    My question however is .. I want to add a metric instead of a dimension. How can I achieve this?

    • Eivind Savio | March 27, 2016 at 9:13 pm | Reply

      Hi Tadd

      If I understand your question correctly, you want to track the Hotjar ID as a Metric instead of a Dimension? You can’t do that. Or did you mean something else?


  3. Hi Eivind,

    I’m really interested in testing this, but I’m having trouble at this part:

    “This tag requires 2 different triggers. 1 trigger to fire the tag, and 1 trigger to block the tag.”

    On the tag “Hotjar Store User ID”, what should the “Fire On” rules look like?

Leave a comment

Your email address will not be published.