How do I start or stop Hotjar recording dynamically based on the visitor's consent choice?
Short answer
Hotjar's snippet has no documented public API to pause and resume an already-initialized recording session, so the reliable pattern is to never load the snippet at all until consent is granted - treating it as a load-gate problem, not a live-toggle problem - and to clear Hotjar's cookies and stop reinitializing it if consent is later revoked.
Why this happens
Hotjar's script begins recording essentially as soon as it executes and reads its site settings; there's no officially documented "stop recording but keep the session" hook for an already-running instance. Teams that try to build a live on/off switch around an already-loaded snippet are working against that gap rather than with it.
The clean equivalent of a live toggle is upstream of the snippet entirely: control whether the snippet ever executes in the first place, using the same consent-gate pattern you'd use for any other consent-controlled tag.
Fix it
- 1
Never ship the Hotjar snippet as an executable script by default
Mark it inert on page load (
type="text/plain"with adata-categoryattribute matching your CMP's category naming, the same pattern used for every other consent-gated tag on this site) so the browser never executes it before consent is checked.<script type="text/plain" data-category="analytics"> (function(h,o,t,j,a,r){ h.hj = h.hj || function(){ (h.hj.q = h.hj.q || []).push(arguments); }; h._hjSettings = { hjid: YOUR_HJID, hjsv: YOUR_HJSV }; a = o.getElementsByTagName("head")[0]; r = o.createElement("script"); r.async = 1; r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv; a.appendChild(r); })(window, document, "https://static.hotjar.com/c/hotjar-", ".js?sv="); </script> - 2
Have your CMP activate the script only on the matching category accept
Configure the consent-management library to find
type="text/plain"scripts tagged with the accepted category and swap them to executabletext/javascript(or clone-and-replace the node) - this is the standard activation mechanism, not something Hotjar-specific. - 3
Handle revocation as a teardown, not a pause
If a visitor later withdraws analytics consent, clear Hotjar's cookies (
_hjSession*,_hjid, and related keys) and simply don't reinitialize the snippet on the next load - there's no supported way to "pause" a session already in progress, so don't build one. - 4
Guard SPA route-change calls the same way
If you call
hj('stateChange', path)on client-side navigation, guard every call site with the same consent check used for the initial load - a route change after consent is denied shouldn't fire this call just because the snippet happened to load earlier in the session. - 5
Confirm the gate is structural, not a JavaScript conditional
A
type="text/plain"swap prevents execution at the browser level; anif (consentGranted) { loadHotjar(); }conditional around an otherwise-normal script tag is more fragile and can race with other page scripts during initial load.
How to verify it worked
In a fresh browser session (cookies cleared), load the page, decline the analytics category, and confirm no request to static.hotjar.com appears in the Network tab and no _hj* cookies are set.
Accept the analytics category, reload, and confirm the request and cookies now appear.
Accept consent, then revoke it through your consent-preferences UI, and confirm existing _hj* cookies are cleared and no further Hotjar requests fire on subsequent page loads in that session.
Related
Still stuck, or want this checked against your specific setup?
We scope every engagement in discovery, before implementation - no assumptions about your stack.