Vue Hotjar Plugin

1.4.0 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `vue-hotjar`, initialize it with a Hotjar Site ID, and conditionally use the global `$hj` property for tracking custom events or identifying users.

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');
      }
    }
  }
};

view raw JSON →