tessl install tessl/npm-posthog-js@1.335.0PostHog Browser JS Library is a comprehensive browser analytics and feature management SDK that enables developers to capture user events, track product analytics, manage feature flags, record session replays, and implement feedback mechanisms like surveys and conversations in web applications.
Heatmaps capture click and mouse movement data to visualize where users interact with your website. This data powers click heatmaps, scroll depth analysis, and rage click detection in PostHog.
Checks whether heatmaps are currently enabled based on configuration or server-side settings.
/**
* Whether heatmaps are enabled
* Checks local config and server-side remote config
*/
readonly isEnabled: boolean;Usage Example:
if (posthog.heatmaps.isEnabled) {
console.log('Heatmaps are active');
}Starts heatmap data collection if enabled.
/**
* Starts heatmap collection if enabled
* Safe to call multiple times - will not restart if already running
*/
function startIfEnabled(): void;Usage Example:
// Typically called automatically on initialization
// Can be called manually if needed
posthog.heatmaps.startIfEnabled();Stops heatmap data collection.
/**
* Stops heatmap collection and clears listeners
*/
function stop(): void;Usage Example:
// Stop collecting heatmap data
posthog.heatmaps.stop();The interval at which heatmap data is sent to the server.
/**
* Flush interval in milliseconds
* Controls how often collected heatmap data is sent to PostHog
* @default 5000 (5 seconds)
*/
readonly flushIntervalMilliseconds: number;Usage Example:
const interval = posthog.heatmaps.flushIntervalMilliseconds;
console.log(`Heatmaps flush every ${interval}ms`);Heatmaps can be controlled through initialization config.
posthog.init('token', {
// Enable/disable heatmaps
capture_heatmaps: true, // default: undefined (server-controlled)
// Or use detailed configuration
capture_heatmaps: {
// Custom flush interval in milliseconds
flush_interval_milliseconds: 5000 // default
}
});interface HeatmapsConfig {
/**
* Enable or disable heatmaps
* - true: Enable heatmaps
* - false: Disable heatmaps
* - undefined: Use server-side configuration (default)
*/
capture_heatmaps?: boolean | {
/**
* How often to send heatmap data to server (milliseconds)
* @default 5000
*/
flush_interval_milliseconds?: number;
};
}Heatmaps automatically track:
By default, heatmaps are controlled by server-side configuration from PostHog:
// Let PostHog control heatmaps
posthog.init('token'); // Uses server config
// Override to always enable
posthog.init('token', {
capture_heatmaps: true
});
// Override to always disable
posthog.init('token', {
capture_heatmaps: false
});Heatmap data is:
// Initialize with heatmaps enabled
posthog.init('token', {
capture_heatmaps: true
});
// Check if heatmaps are active
if (posthog.heatmaps.isEnabled) {
console.log('Collecting heatmap data');
}// Send heatmap data more frequently
posthog.init('token', {
capture_heatmaps: {
flush_interval_milliseconds: 2000 // Send every 2 seconds
}
});// Only enable heatmaps for specific pages
posthog.init('token', {
capture_heatmaps: false // Disabled by default
});
// Enable on specific pages
if (window.location.pathname.startsWith('/product/')) {
posthog.heatmaps.startIfEnabled();
}
// Disable on other pages
posthog.heatmaps.stop();// Start collecting heatmaps based on user action
function enableDetailedTracking() {
posthog.set_config({
capture_heatmaps: true
});
posthog.heatmaps.startIfEnabled();
}
// Stop collecting heatmaps
function disableDetailedTracking() {
posthog.heatmaps.stop();
posthog.set_config({
capture_heatmaps: false
});
}Heatmaps automatically detect and track rage clicks (rapid repeated clicks):
posthog.init('token', {
capture_heatmaps: true,
autocapture: {
rageclicks: true // Enable rage click detection
}
});
// Rage clicks are automatically included in heatmap dataHeatmaps can also track dead clicks (clicks with no effect):
posthog.init('token', {
capture_heatmaps: true,
autocapture: {
dead_clicks: true // Enable dead click detection
}
});Heatmaps complement session recording for complete user interaction analysis:
posthog.init('token', {
capture_heatmaps: true, // Visual click/movement analysis
session_recording: {
enabled: true // Video replay of sessions
}
});Focus heatmap collection on important pages to optimize performance and data costs:
const highValuePages = ['/pricing', '/checkout', '/product'];
posthog.init('token', {
capture_heatmaps: highValuePages.some(page =>
window.location.pathname.startsWith(page)
)
});For high-traffic pages, consider a longer flush interval:
const isHighTrafficPage = window.location.pathname === '/';
posthog.init('token', {
capture_heatmaps: {
// Less frequent flushing on high-traffic pages
flush_interval_milliseconds: isHighTrafficPage ? 10000 : 5000
}
});Use feature flags to control heatmap collection:
posthog.init('token');
// Check feature flag to enable heatmaps
posthog.onFeatureFlags(() => {
if (posthog.isFeatureEnabled('enable-heatmaps')) {
posthog.set_config({
capture_heatmaps: true
});
posthog.heatmaps.startIfEnabled();
}
});Heatmaps respect your privacy settings:
posthog.init('token', {
capture_heatmaps: true,
// Heatmaps will respect these settings
mask_all_text: true,
mask_all_element_attributes: true
});// Gradually enable heatmaps for users
posthog.init('token');
posthog.onFeatureFlags(() => {
const heatmapVariant = posthog.getFeatureFlag('heatmap-rollout');
if (heatmapVariant === 'enabled') {
posthog.set_config({
capture_heatmaps: true
});
posthog.heatmaps.startIfEnabled();
}
});// Different settings for different page types
function configureHeatmapsForPage() {
const path = window.location.pathname;
if (path.startsWith('/checkout')) {
// Detailed tracking for checkout
posthog.set_config({
capture_heatmaps: {
flush_interval_milliseconds: 2000 // Fast flushing
}
});
} else if (path.startsWith('/blog')) {
// Basic tracking for content pages
posthog.set_config({
capture_heatmaps: {
flush_interval_milliseconds: 10000 // Slower flushing
}
});
}
posthog.heatmaps.startIfEnabled();
}
// Call on route changes in SPAs
configureHeatmapsForPage();// Use heatmaps to analyze different variants
posthog.init('token', {
capture_heatmaps: true
});
// Tag heatmap data with experiment variant
posthog.register({
ab_test_variant: posthog.getFeatureFlag('pricing-page-test')
});
// Analyze heatmaps for each variant in PostHog// Monitor heatmap collection performance
let heatmapStats = {
flushes: 0,
lastFlushTime: Date.now()
};
// Check flush interval
setInterval(() => {
if (posthog.heatmaps.isEnabled) {
heatmapStats.flushes++;
const interval = Date.now() - heatmapStats.lastFlushTime;
console.log(`Heatmap flush #${heatmapStats.flushes}, interval: ${interval}ms`);
heatmapStats.lastFlushTime = Date.now();
}
}, posthog.heatmaps.flushIntervalMilliseconds);Once enabled, heatmap data appears in PostHog's Toolbar:
Heatmaps have minimal performance impact:
For optimal performance:
// Check if enabled
console.log('Heatmaps enabled:', posthog.heatmaps.isEnabled);
// Force enable
posthog.set_config({
capture_heatmaps: true
});
posthog.heatmaps.startIfEnabled();
// Check configuration
console.log('Flush interval:', posthog.heatmaps.flushIntervalMilliseconds);If heatmaps aren't enabled and capture_heatmaps is not set:
posthog.init('token', {
capture_heatmaps: true // Force enable
});If experiencing high memory usage:
// Reduce flush interval to send data more frequently
posthog.init('token', {
capture_heatmaps: {
flush_interval_milliseconds: 2000 // Flush more often
}
});