Vue Hotjar Next Plugin
Vue Hotjar Next is a plugin designed for integrating Hotjar analytics seamlessly into Vue 3 applications. Currently stable at version 2.1.0, the package follows an as-needed release cadence, with significant updates like the v2.0.0 major refactor improving API access and type safety. Key differentiators include its exclusive compatibility with Vue 3 (requiring `vue-hotjar` for Vue 2 projects), providing direct access to the Hotjar Identify API and configuration settings via Vue's global properties (`$hj` and `$hjSettings`). This approach allows developers to interact with Hotjar functions like `hj('identify', ...)` through `$hj` without directly manipulating the `window.hj` object, offering a more Vue-idiomatic integration. It also ships with TypeScript types, ensuring robust development.
Common errors
-
TypeError: app.use is not a function
cause Attempting to use `vue-hotjar-next` with a Vue 2 application, where the `app` instance and `use` method signature differ.fixEnsure your project is running Vue 3. If you need Hotjar for Vue 2, use the `vue-hotjar` package instead. -
Property '$hj' does not exist on type 'ComponentPublicInstance'
cause When using TypeScript, the global properties `$hj` and `$hjSettings` are added dynamically and might not be automatically inferred on `this` or the component instance without proper type augmentation.fixAdd a declaration file (e.g., `vue-hotjar-next.d.ts`) to augment the `ComponentCustomProperties` interface with `$hj: HotjarPlugin;` and `$hjSettings: HotjarSettings;`. -
Hotjar tracking data is not appearing in my Hotjar account
cause Multiple possible causes: `isProduction` is set to `false`, Hotjar ID is incorrect, or the snippet isn't loading due to network issues/ad blockers.fixVerify `isProduction` is `true` in your configuration, double-check your Hotjar Site ID, and inspect your browser's network tab to confirm the Hotjar script (`static.hotjar.com/c-v.js`) is loaded without errors.
Warnings
- breaking This plugin is exclusively for Vue 3. It will not work with Vue 2 applications. Attempting to use it with Vue 2 will result in errors related to `app.use` or global property access.
- breaking Version 2.0.0 introduced a major refactor, changing how the Hotjar API and settings are accessed. Previously, users might have interacted directly with `window.hj` or `window._hjSettings`. Now, these are bound to Vue global properties.
- gotcha The `isProduction` parameter, which controls whether Hotjar tracking is enabled, defaults to `true` if not explicitly defined. This means tracking might be active in development environments unless explicitly set to `false`.
- gotcha The `id` parameter, representing your Hotjar Site ID, supports both `number` and `string` types. Before v2.1.0, while it might have worked, the type definition primarily suggested a number. Version 2.1.0 explicitly added string ID support and refined validation.
Install
-
npm install vue-hotjar-next -
yarn add vue-hotjar-next -
pnpm add vue-hotjar-next
Imports
- VueHotjar
import { VueHotjar } from 'vue-hotjar-next';import VueHotjar from 'vue-hotjar-next';
- $hj
window.hj
app.config.globalProperties.$hj
- $hjSettings
window._hjSettings
app.config.globalProperties.$hjSettings
Quickstart
import { createApp } from 'vue';
import App from './App.vue';
import VueHotjar from 'vue-hotjar-next';
const app = createApp(App);
app.use(VueHotjar, {
id: process.env.VUE_APP_HOTJAR_ID ?? 12345678,
isProduction: process.env.NODE_ENV === 'production',
snippetVersion: 6
});
app.mount("#app");
// Example of accessing Hotjar Identify API globally or in a component (after setup)
// In a component (Options API):
// export default {
// mounted() {
// this.$hj('identify', 'USER_ID', { userProperty: 'value' });
// }
// };
// In a component (Composition API):
// import { getCurrentInstance } from 'vue';
// const instance = getCurrentInstance();
// instance.appContext.config.globalProperties.$hj('identify', 'USER_ID', { userProperty: 'value' });