Hotjar Tracking Code
Adds Hotjar heatmaps and session recordings to your public pages, without recording logged-in staff.
Opens WPIDE on
Code
add_action( 'wp_head', function () {
$site_id = 'YOUR_SITE_ID'; // Hotjar Site ID (numbers only).
$require_consent = false; // true: wait for consent from the WPIDE cookie banner.
if ( ! ctype_digit( (string) $site_id ) || current_user_can( 'edit_posts' ) ) {
return;
}
?>
<script>
(function (run) {
if (!<?php echo $require_consent ? 'true' : 'false'; ?> || /(?:^|; )wpide_consent=granted/.test(document.cookie)) { run(); return; }
document.addEventListener('wpide:consent', function (e) { if (e.detail === 'granted') { run(); } });
})(function () {
window.hj = window.hj || function () { (window.hj.q = window.hj.q || []).push(arguments); };
window._hjSettings = { hjid: <?php echo (int) $site_id; ?>, hjsv: 6 };
var s = document.createElement('script');
s.async = true;
s.src = 'https://static.hotjar.com/c/hotjar-' + window._hjSettings.hjid + '.js?sv=' + window._hjSettings.hjsv;
document.head.appendChild(s);
});
</script>
<?php
} );
Notes
Replace YOUR_SITE_ID with your Hotjar Site ID; nothing is output while the placeholder is in place. Mention Hotjar in your privacy policy.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.