Vue Hotjar Plugin
Vue Hotjar is a plugin designed for easy integration of the Hotjar analytics and feedback platform into Vue.js projects. It simplifies the process of adding the Hotjar tracking code and provides a global Vue property, `$hj`, to interact with Hotjar's API (e.g., Identify API, stateChange, virtual page views). The package is currently at version 1.4.0, with its last update in June 2021. Its primary differentiators include environment-based tracking activation/deactivation and a streamlined interface to the Hotjar API within Vue instances. However, it offers only partial support for Vue 3, with known issues regarding TypeScript typings, and the `$hj` global property is exclusively available for Vue 2.x.x versions. A separate `vue-hotjar-next` package exists for full Vue 3 support, indicating this package is primarily for Vue 2 applications.
Common errors
-
Cannot read property '$hj' of undefined
cause Attempting to access `this.$hj` in a Vue 3 component or when `isProduction` is `false` without a check. In Vue 3, `$hj` is not injected by this package. If `isProduction` is `false`, `$hj` is explicitly `false`.fixFor Vue 2, ensure `isProduction` is `true` or wrap `$hj` calls in `if (this.$hj) { ... }`. For Vue 3, this package does not provide `$hj`. Use `vue-hotjar-next` for Vue 3 support, or directly interact with `window.hj` after Hotjar has loaded. -
TypeError: Cannot read properties of undefined (reading 'use')
cause This error typically indicates that `Vue` is not correctly imported or available when `Vue.use(Hotjar, ...)` is called, or an incorrect `Vue` instance is being used for initialization.fixEnsure `import Vue from 'vue'` is at the top of your main application file and that you are using a Vue 2 application context for this plugin. Verify `vue` is correctly installed as a dependency.
Warnings
- breaking This `vue-hotjar` package is designed for Vue 2.x.x. For Vue 3.x.x projects, it offers only partial support, and users may encounter issues, particularly with TypeScript typings due to interface updates in Vue 3. A separate package, `vue-hotjar-next`, is available for full Vue 3 compatibility.
- gotcha The `$hj` global property, which provides access to the Hotjar API, is only available if your Vue version is 2.x.x. It is explicitly disabled for Vue 3.0.0 and above for compatibility reasons.
- gotcha The `$hj` global property will return `false` if the `isProduction` parameter is set to `false` during plugin initialization. This means any calls to `$hj` methods must be wrapped in a conditional check.
- gotcha The package has not been updated since June 2021. This might lead to compatibility issues with newer versions of Vue 2 (though less likely) or newer Hotjar SDK features/changes not explicitly covered by this plugin.
Install
-
npm install vue-hotjar -
yarn add vue-hotjar -
pnpm add vue-hotjar
Imports
- Hotjar
import { Hotjar } from 'vue-hotjar'import Hotjar from 'vue-hotjar'
- Vue.use
import Hotjar from 'vue-hotjar'; new Vue({ el: '#app', render: h => h(App) }) // Hotjar not initializedimport Vue from 'vue'; import Hotjar from 'vue-hotjar'; Vue.use(Hotjar, { id: 'XXXXXXX' }) - $hj
this.$hj('event', 'user_action');if (this.$hj) { this.$hj('event', 'user_action'); }
Quickstart
import Vue from 'vue';
import Hotjar from 'vue-hotjar';
// Hotjar Site ID can be found at insights.hotjar.com under tracking.
// It's recommended to bind `isProduction` to your Node environment variable.
// snippetVersion defaults to 6 if not specified.
Vue.use(Hotjar, {
id: process.env.VUE_APP_HOTJAR_ID ?? '1234567',
isProduction: process.env.NODE_ENV === 'production',
snippetVersion: 6
});
// Example component where $hj can be used (Vue 2.x.x only)
export default {
methods: {
trackPurchase(userId, amount) {
if (this.$hj) {
this.$hj('identify', userId, {
email: 'user@example.com',
purchase_amount: amount,
ab_test: 'variant-B',
});
this.$hj('stateChange', '/purchase-complete');
}
}
}
};