Insights

Adding Clarity Heatmaps & Recordings in GrowthBook experiments

Illustration of two people looking at a website heatmap
Picture of Rudger de Groot
Rudger de Groot
Founder & CRO

Share

Just the other day I found this question on the Growthbook Slack channel #ask-questions:

I just want to ask if anyone has any experience with the Growthbook & Clarity. I would like to see the heatmaps of each variant. Does anyone have any experience? Has anyone tried to do this maybe through Clarity custom events? Or in other qualitative tools? Thank you for your time, peeps.
Slack

Clarity & GrowthBook User

#ask-questions

This is a great question because by adding heatmaps and user session recordings to your experiment you gain more insights into which elements drove a change with the visitors. This added qualitative metadata can definitely help you better understand the insights that you get from an experiment, which can lead to better follow-up experiments.

The solution is adding custom tags to your variation, these tags are then added to a Clarity session when the visitor visits your experiment.

To use custom tags, pass the set argument along with a key-value pair to define a tag in your JavaScript. When Clarity collects data for that tag, it appears in the Filters options.

In Growthbook, there are multiple ways to add this JS code to your variation, but if you are using, for example, the visual editor, then you just have to add the code in the JS field.

window.clarity(“set”, “AB-Test-1”, “Variation-1”)

GrowthBook Visual Editor, Custom JS field of Variation 1, where the window.clarity('set', …) call goes

One thing you do have to check is the order in which Clarity and Growthbook load. For this to work, Clarity must be loaded when the experiment starts; otherwise, the tags won’t be set.

Now, this is where it can get a bit tricky. GrowthBook is usually faster than Clarity (which is a good thing if you want to prevent page flashing), so you also need to add some logic so your JS code checks first if Clarity is loaded. When that validates, the custom tag is set.

Here is a screenshot of how we integrate Clarity with Growthbook. Credits to Pascal Louwes for this work of art.

Code of triggerClarity(): sets the Clarity custom tags Experiment and Variation for the current GrowthBook experiment and queues the calls until Clarity has loaded

The same code as text:

import { tracer } from "@tracer/tracer.js";

/**
 * Triggers clarity tracking for a given experiment and its variation.
 *
 * This function sends the experiment and variation data to Clarity for tracking purposes.
 * If Clarity is not yet loaded, it queues up the tracking calls for when Clarity becomes available.
 *
 * @param {Object} experiment - The experiment object containing details about the current experiment.
 * @param {string} experiment.id - The unique identifier for the experiment.
 * @param {string} experiment.name - The name of the experiment.
 * @param {Object} experiment.variation - The variation object for the experiment.
 * @param {string} experiment.variation.id - The unique identifier for the variation.
 * @param {string} experiment.variation.name - The name of the variation.
 */
const triggerClarity = ({ experiment }) => {
  const {
    id: experimentId,
    name: experimentName,
    variation: { id: variationId, name: variationName }
  } = experiment;

  try {
    tracer.info("trigger-clarity: sending data: ", {
      experiment: `${experimentId} - ${experimentName}`,
      variation: `${variationId}: ${variationName}`
    });
    // Start queuing up calls if Clarity hasn't loaded
    window.clarity =
      window.clarity ||
      function () {
        (window.clarity.q = window.clarity.q || []).push(arguments);
      };
    window.clarity("set", "Experiment", `${experimentId} - ${experimentName}`);
    window.clarity("set", "Variation", `${variationId}: ${variationName}`);
  } catch (e) {
    tracer.error("trigger-clarity.js error:", e);
  }
};

export { triggerClarity };

We assist companies
scaling experimentation

Our GrowthBook expert

Rudger de Groot

Rudger de Groot

Founder & CEO

Contact form

Discover how we’ve helped leading brands transform their digital experiences through experimentation and innovative GrowthBook solutions.

Direct contact

Nijverheidstraat 11-1E
7511 JM Enschede
the Netherlands

+31 53 234 0444