On this page

Hotjar

Hotjar is a product experience insights tool that provides behavioral analytics and feedback data with users.

Set up the integration

Hotjar plugin setup

To forward events to Hotjar, use a variant of an Amplitude Destination Plugin. The template below is a Destination Plugin tailored for Hotjar. The plugin creates an instance of the Hotjar browser tracking code and forwards tracked events from Amplitude's SDK. Customize this template for any need.

ts
import { BrowserConfig, DestinationPlugin, Event, PluginType, Result } from '@amplitude/analytics-types';
import { default as hj } from '@hotjar/browser';
export class HotjarPlugin implements DestinationPlugin {
  name = 'hotjar';
  type = PluginType.DESTINATION as const;
  siteId: number;
  hotjarVersion: number;

  constructor(siteId: number, hotjarVersion: number) {
    this.siteId = siteId;
    this.hotjarVersion = hotjarVersion;
  }

  async setup(): Promise<void> {
    hj.init(this.siteId, this.hotjarVersion);
  }

  async execute(event: Event): Promise<Result> {
    if (event.event_type === '$identify') {
      const { user_id, device_id, user_properties } = event;
      const hotjarId = user_id || device_id || '';
      hj.identify(hotjarId, user_properties || {});
    } else {
      hj.event(event.event_type);
    }
    return {
      code: 0,
      event: event,
      message: 'Event forwarded to Hotjar API',
    };
  }
}

Amplitude plugin usage

In the app's code, import the plugin and add it to the Amplitude SDK instance.

ts
import * as amplitude from '@amplitude/analytics-browser';
import { HotjarPlugin } from './HotjarPlugin';

amplitude.init(AMPLITUDE_API_KEY);
amplitude.add(new HotjarPlugin(HOTJAR_SIDE_ID, HOTJAR_VERSION));

amplitude.logEvent('open app');

Was this helpful?