Mixpanel Browser SDK

2.78.0 · active · verified Sun Apr 19

The `mixpanel-browser` library is the official JavaScript browser client for Mixpanel, enabling web applications to send analytics data, capture user events, perform session replay, manage feature flags, and track network telemetry. Currently at version 2.78.0, the library receives frequent updates, typically on a weekly or bi-weekly cadence, reflecting active development and the introduction of new features such as enhanced session recording capabilities (cross-origin iframe support, network recording, event-triggered recording), a refined masking API for sensitive data, and advanced autocapture options. Key differentiators include its comprehensive feature set for client-side analytics, robust session replay with masking and console recording, and a flexible module loading system allowing for optimized bundle sizes by excluding session recording components when not needed. It provides first-party TypeScript types and supports modern ESM import patterns.

Common errors

Warnings

Install

Imports

Quickstart

Initializes Mixpanel with common configuration options, identifies a user, tracks a custom event, sets user profile properties, and demonstrates basic feature flag usage.

import mixpanel from 'mixpanel-browser';

const MIXPANEL_TOKEN = process.env.MIXPANEL_TOKEN ?? 'YOUR_MIXPANEL_PROJECT_TOKEN';

// Initialize Mixpanel with autocapture, session recording, and debug mode
mixpanel.init(MIXPANEL_TOKEN, {
  autocapture: true, // Automatically track common user interactions
  track_pageview: true, // Automatically track page views
  record_sessions_percent: 100, // Record 100% of user sessions for replay
  record_heatmap_data: true, // Enable heatmap, rage click, and dead click collection
  persistence: 'localStorage', // Use localStorage for more reliable persistence than cookies
  debug: true, // Enable debug logs in the console
  // Example of cross-origin iframe recording allowlist for security (since v2.77.0)
  // record_allowed_iframe_origins: ['https://embedded-widget.example.com'],
});

// Identify a user after they log in or sign up
mixpanel.identify('USER_ID_123', {
  name: 'John Doe',
  email: 'john.doe@example.com',
  plan: 'Enterprise'
});

// Track a custom event with properties
mixpanel.track('Signed Up', {
  'Signup Type': 'Referral',
  'Source Page': '/pricing'
});

// Track a page view explicitly (if not using track_pageview: true in init)
// mixpanel.track('Page View', {
//   'Page Title': document.title,
//   'Page URL': window.location.href
// });

// Set or update user profile properties
mixpanel.people.set({
  '$last_login': new Date().toISOString(),
  'Subscription Status': 'Active'
});

// Example of using feature flags (requires feature flags configured in Mixpanel)
mixpanel.flags.whenReady().then(() => {
  if (mixpanel.flags.enabled('new-feature-flag')) {
    console.log('New feature is enabled for this user!');
  } else {
    console.log('New feature is NOT enabled for this user.');
  }
});

view raw JSON →