Skip to Content
IntegrationHeatmaps

Heatmaps Integration

Heatmaps show where visitors click on each page, split by device. Turn them on with one attribute on the tracking script, then open Heatmaps in your dashboard. Reading heatmaps and capturing page layouts is covered in the Heatmaps dashboard guide.

Quick start

Heatmaps are off by default. Add data-heatmaps="true" to your analytics script:

<script async src="https://betterlytics.io/analytics.js" data-site-id="your-site-id" data-heatmaps="true" ></script>

Clicks are sent within 30 seconds, or sooner when a visitor leaves the page or switches tabs, and then show up on the Heatmaps page.

Self-hosting? Load analytics.js from your instance and send events to it, with the script tag and the npm package alike. Tracking Scripts in the Self-Hosting guide shows the two changes.

Configuration reference

AttributeTypeDefaultDescription
data-heatmapsbooleanfalseRecords clicks for heatmaps.
data-heatmap-samplenumber (0 to 100)100Percent of page views that record clicks.
data-dynamic-urlsstring list-Groups pages such as /product/* into one heatmap, the same way as for pageviews. See Dynamic URLs.
data-scripts-base-urlstring (URL)Origin of analytics.jsBase to load heatmap.js and capture.js from.

Using the npm package

Heatmaps are disabled by default. Enable them with enableHeatmaps, available from @betterlytics/tracker 0.2.4:

import betterlytics from '@betterlytics/tracker'; betterlytics.init('your-site-id', { enableHeatmaps: true, // true | false (default) heatmapSample: 50, // 0 to 100, defaults to 100 });

See the package docs for the full API and options: @betterlytics/tracker on npm .

How clicks are recorded

  • Sampling is decided per page view. Only a sampled page view downloads the small heatmap.js script.
  • Clicks are sent in batches within 30 seconds of the first click, or sooner when the visitor leaves the page or switches tabs, to the server your events go to (the origin of data-server-url).
  • A page view records at most 300 clicks. Automated browsers are skipped.
  • Each click is stored with the page path (without query string or hash) and the device, but not with the visitor or session. The device comes from the window width: Mobile under 576 px, Tablet from 576 to 991 px, Desktop from 992 px.
  • Clicks are kept as long as your other analytics data.

What is collected

For each click, the tracker sends:

  • where on the page the click landed, with the window width and the page’s full height
  • a CSS selector for the clicked element, and where inside the element the click landed
  • whether the element is fixed or sticky, such as a header that stays on screen
  • the page address and screen size, as with a pageview, and when each click happened

Heatmaps never collect text from the page, input values, keystrokes or screenshots. They have no consent switch of their own: clicks aren’t linked to a visitor or session and carry no page content.

Stable element ids

Clicks are tied to the element that was clicked, so they stay on the right button when content above it moves or the layout is shown at another width. The tracker describes each element with a selector that matches only that element, plus one more level of the page around it, which helps tell it apart from similar elements that appear later, and prefers an id attribute when there is one.

Give the elements you care about, such as calls to action, navigation links and form buttons, a plain id that stays the same on every page load:

<button id="signup-cta">Start free trial</button>

Ids generated at runtime, such as React’s useId() output (:r1: or «r1»), change between page loads. When there is fixed text after the generated part, as in radix-«r1»-trigger-pages, the text around it is used; otherwise the id is skipped. Without a usable id, the selector falls back to tag names and up to two class names, which can stop matching when classes or the page structure change. A click on an element that no selector of up to 500 characters picks out on its own isn’t recorded.

Content-Security-Policy

heatmap.js loads from the same origin as analytics.js (or from data-scripts-base-url), and clicks are sent to your event server, the origin of data-server-url. Capturing a page from the dashboard loads capture.js and replay.js the same way and uploads the capture to the event server. If your site sets a Content-Security-Policy, allow those origins in script-src and connect-src: https://betterlytics.io for the hosted service, or your instance’s origin when self-hosting.

Troubleshooting

  • No clicks appear: clicks are sent within 30 seconds, or when you leave the page or switch tabs, so wait 30 seconds or switch away from your site before checking. Then check that data-heatmaps="true" is on the script tag that loads analytics.js, that data-heatmap-sample isn’t 0, and that heatmap.js loads in your browser’s Network tab. A blocked script shows up as a CSP violation in the Console.
  • Clicks are missing on a captured page: clicks whose element isn’t on the capture, or can’t be told apart from a similar one, aren’t drawn. Give those elements a stable id, see Stable element ids.
  • One page shows up as many: pages with ids or slugs in the path, such as /product/123, get a heatmap each. Group them with data-dynamic-urls.

Next steps: Read heatmaps and capture page layouts in the Heatmaps dashboard guide.