Amplitude JavaScript SDK (Legacy)
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
-
Events not showing in Amplitude dashboard
cause Common causes include incorrect API key during `init()`, network connectivity issues, ad blockers preventing event transmission, or events being queued and not yet flushed to the server.fixVerify your API key is correct during `amplitude.getInstance().init()`. Ensure events are being sent (e.g., `uploadIntervalMillis` configuration or `flushQueueSize`). Check browser console for errors or network requests to Amplitude. Use Amplitude's Chrome extension for real-time debugging. -
Uncaught TypeError: amplitude.getInstance is not a function
cause This typically occurs if the Amplitude SDK script has not been loaded or has loaded incorrectly before `amplitude.getInstance()` is called. It can also happen in module environments if `amplitude` is not correctly imported.fixEnsure the Amplitude SDK script tag is placed correctly in your HTML (ideally at the end of `<body>` or using `defer`). If using modules, verify `import amplitude from 'amplitude-js';` or `const amplitude = require('amplitude-js');` is at the top of your file and the module resolution is correct. -
TypeError: undefined is not an object (evaluating 'e._q.push')
cause This error, often seen in environments with multiple SDKs or complex bundling, suggests an issue with Amplitude's internal queue mechanism, possibly due to conflicting snippets or incorrect initialization of the global `amplitude` object.fixCheck for multiple Amplitude snippets loaded on the page. Ensure the Amplitude snippet is loaded only once and initialized correctly. If using the snippet, ensure it's not being overridden. Consider consolidating to a single, consistent loading mechanism (e.g., npm package) if possible. -
Amplitude cookie format changed, resulting in new device IDs for returning users.
cause This is a known breaking change introduced in `amplitude-js` v6.0. If applications using the same Amplitude project are not updated simultaneously, older SDK versions will write cookies in the old format, leading to new device IDs being generated for returning users once they encounter an application with the updated SDK.fixUpgrade all client-side applications that share the same Amplitude API key to `amplitude-js` v6.0.0 or newer at the same time to ensure consistent cookie handling. Refer to the migration guide for additional options like `cookieForceUpgrade`.
Warnings
- breaking The cookie format changed in v6.0 to be more compact. If you use the same Amplitude project across multiple applications and track anonymous users, all `amplitude-js` instances across those applications must be updated simultaneously. Failure to do so will result in anonymous users having different device IDs across your applications, leading to broken user journeys and inaccurate analytics.
- deprecated Support for React Native was removed from `amplitude-js` in version 8.0.0. The SDK is no longer maintained for React Native projects, and using it in such environments will lead to unexpected behavior and missing features.
- gotcha The `amplitude-js` library is in maintenance mode, and new feature development is focused on the `@amplitude/analytics-browser` (TypeScript) SDK for web environments. Using `amplitude-js` for new projects means you will miss out on new features, improved developer experience, and better TypeScript support.
- gotcha Setting `flushIntervalMillis` to `0` was previously being overwritten by a default value of 10 seconds due to a bug. This could lead to unexpected delays in event transmission even when immediate flushing was desired.
- gotcha When initiating `amplitude.init()` in `_app.js` within a Next.js application, it can cause 500 errors on pages that use `getServerSideProps` due to potential incompatibilities with Server-Side Rendering.
Install
-
npm install amplitude-js -
yarn add amplitude-js -
pnpm add amplitude-js
Imports
- amplitude (global)
<!-- loaded via script tag --> <script type="text/javascript"> amplitude.getInstance().init('YOUR_API_KEY'); </script> - amplitude (CommonJS)
import amplitude from 'amplitude-js'; // Incorrect for older Node.js/CommonJS setups
const amplitude = require('amplitude-js'); - amplitude (ESM)
const amplitude = require('amplitude-js'); // While technically works in some ESM contexts, prefer 'import'import amplitude from 'amplitude-js';
Quickstart
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);