Hotjar is a popular behavior analytics tool that helps you understand how users interact with your website through heatmaps, session recordings, surveys, and feedback tools. Here's how to integrate Hotjar with your Next.js application:
I'll create a reusable Hotjar component that you can add to your Next.js application:
Set up your Hotjar ID as an environment variable:
Create a .env.local
file in your project root and add:
NEXT_PUBLIC_HOTJAR_ID=your_hotjar_id
Verify the installation:
You can track specific events using the Hotjar API:
// Track an event
window.hj('event', 'button_clicked');
// Track a value
window.hj('event', 'form_submitted', {total_value: 100});
You can set user attributes for better segmentation:
// Identify a user (if you have user authentication)
window.hj('identify', 'USER_ID', {
email: 'user@example.com',
name: 'John Doe',
plan: 'premium'
});
For more advanced usage, you can create a custom hook:
Please make sure to add the following environment variable to your project: