Amplitude JavaScript SDK (Legacy)

8.21.10 · maintenance · verified Sun Apr 19

The `amplitude-js` library is the original JavaScript SDK for integrating web applications with Amplitude Analytics, enabling developers to track user events, user properties, and revenue data. Currently at version 8.21.10, this SDK primarily receives maintenance updates, with new feature development and a TypeScript-first approach now concentrated in the newer `@amplitude/analytics-browser` package. While `amplitude-js` continues to be functional for existing projects, it is no longer recommended for new browser-based instrumentations and has deprecated support for React Native since version 8.0.0, advising migration to `@amplitude/react-native` for cross-platform applications and `@amplitude/node` for server-side environments. Its release cadence has slowed significantly, focusing on bug fixes and dependency upgrades. It differentiates itself as the foundational Amplitude SDK before the introduction of more specialized and modern alternatives.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Amplitude SDK, set a user ID and properties, and log two different events with associated properties. It showcases basic event tracking functionality in a browser context.

import amplitude from 'amplitude-js'; // Or const amplitude = require('amplitude-js');

// Initialize Amplitude with your API key
// Replace 'YOUR_API_KEY' with your actual Amplitude Project API Key
amplitude.getInstance().init('YOUR_API_KEY', null, {
  includeReferrer: true,
  includeUtm: true,
  saveEvents: true,
  logLevel: 'WARN',
  // Optional: Configure event upload interval (default is 10 seconds)
  uploadIntervalMillis: 5000,
  // Optional: Set the server zone if your project is in the EU
  // serverZone: 'EU'
});

// Set a user ID after initialization
amplitude.getInstance().setUserId('user_12345');

// Set user properties
amplitude.getInstance().setUserProperties({
  plan: 'premium',
  signup_date: new Date().toISOString(),
  country: 'USA',
});

// Log an event
amplitude.getInstance().logEvent('Button Clicked', {
  button_name: 'Submit Form',
  form_id: 'contact_form_1',
  page: window.location.pathname,
});

// Log another event after some user interaction
setTimeout(() => {
  amplitude.getInstance().logEvent('Page Viewed', {
    page_name: 'Product Details',
    product_id: 'SKU789',
    category: 'Electronics',
  });
  console.log('Amplitude initialized and events logged. Check your Amplitude dashboard.');
}, 2000);

view raw JSON →