# Hotjar Tracking Code

> Adds Hotjar heatmaps and session recordings to your public pages, without recording logged-in staff.

- Type: PHP
- Runs: Frontend only
- Category: [Analytics & Tracking](https://wpide.com/library/category/analytics/)
- Tags: hotjar, heatmaps, recordings, ux
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/analytics-hotjar/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Hotjar Tracking Code" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
