Feature |
Browser SDK 2.0 |
Javascript SDK |
|---|---|---|
Package |
||
Logger provider |
Amplitude Logger by Default. Fully customizable. |
Amplitude Logger by default. Not customizable. |
Storage provider for unsent events |
LocalStorage by default. Fully customizable. |
LocalStorage. Not configurable |
Identity storage |
Cookie stoage by default. local storage, session storage and no storage supported |
The same |
Customization |
Plugins |
Not supported |
Bundle size |
Tree shaking for optimization |
No Optimization |
Server endpoint |
HTTP V2 API |
HTTP V1 API |
Batch API |
Supported, with configuration. |
Not supported |
Amplitude Browser SDK 2.0 (@amplitude/analytics-browser@2+) features:
Browser SDK 2.0 isn't backwards compatible with amplitude-js.
To migrate to @amplitude/analytics-browser@2+, update your dependencies and instrumentation.
@amplitude/analytics-browser@2+, your implementation may experience disruption to web attribution. Before you upgrade, choose whether attribution occurs during a session. After upgrading, attribution can happen during the session, and is no longer configurable.
In both versions, attribution can occur during initialization.
AMP_{first 6 digit of api key} from JavaScript SDK 6.0.0 and aboveAMP_{first 10 digit of api key} from Browser SDK 2Migrating from a JavaScript SDK version prior to 6.0.0 requires manual cookie migration.
To manually migrate cookies:
amplitude.init() in the SDK configuration.this.init('API_KEY', {
deviceId: 'device_id',
userId: 'user_id',
});
amplitude-js: Maintenance Browser SDK@amplitude/analytics-browser@2+: Browser SDK 2.0For snippet installation, update your project's snippet loader.
For Node projects, update your dependency list in package.json.
{
"dependencies": {
"amplitude-js": "^8"
}
}
{
"dependencies": {
"@amplitude/analytics-browser": "^2.11.8"
}
}
Browser SDK 2.0 offers an API to instrument events. To migrate to the Browser SDK 2.0, update a few calls. The following sections detail which calls have changed.
getInstance() has been removed. To initialize the SDK, call init(), with the same parameters. However, config comes in a different shape. See Configuration.
import amplitude from "amplitude-js"
amplitude.getInstance().init(API_KEY, OPTIONAL_USER_ID, config)
import * as amplitude from "@amplitude/analytics-browser"
amplitude.init(API_KEY, OPTIONAL_USER_ID, config)
Configuration options
amplitude-js
@amplitude/analytics-browser@2+
config.apiEndpointconfig.serverUrl
config.batchEventsconfig.flushQueueSize
config.cookieExpirationconfig.cookieOptions.expiration
config.secureCookieconfig.cookieOptions.secure
config.sameSiteCookieconfig.cookieOptions.sameSite
config.cookieNameNOT SUPPORTED
config.cookieForceUpgradeNOT SUPPORTED
config.deferInitializationNOT SUPPORTED. See Defer initialization.
config.disableCookiesconfig.identityStorage. Set it to 'localStorage' to disable cookies
config.deviceIdconfig.deviceId
config.deviceIdFromUrlParamDevice Id is pulled from URL parameter by default. See Browser SDK 2.0 Cross-domain tracking.
config.domainNOT SUPPORTED
config.eventUploadPeriodMillisconfig.flushIntervalMillis
config.eventUploadThresholdconfig.flushQueueSize
config.forceHTTPsNOT SUPPORTED
config.includeFbclidNOT SUPPORTED. See Web attribution.
config.includeGclidNOT SUPPORTED. See Web attribution.
config.includeReferrerNOT SUPPORTED. See Web attribution.
config.includeUtmNOT SUPPORTED. See Web attribution.
config.languageNOT SUPPORTED. See Plugins.
config.libraryNOT SUPPORTED. See Plugins.
config.logLevelconfig.logLevel
config.logAttributionCapturedEventNOT SUPPORTED
config.optOutconfig.optOut
config.onErrorNOT SUPPORTED
config.onExitPageNOT SUPPORTED. See Flush.
config.platformNOT SUPPORTED.
platform isn't supported at the configuration level, but it does still exist in the event object. Overwrite this by either assigning a platform while tracking an event, or enriching the event.platform using the enrichment plugin. See Plugins.
config.savedMaxCountNOT SUPPORTED
config.saveEventsNOT SUPPORTED
config.saveParamsReferrerOncePerSessionNOT SUPPORTED.
config.sessionTimeoutconfig.sessionTimeout
config.storageconfig.identityStorage
config.trackingOptionsconfig.trackingOptions
config.transportconfig.transportProvider
config.unsetParamsReferrerOnNewSessionNOT SUPPORTED. Default behavior.
config.unsentKeyNOT SUPPORTED
config.unsentIdentifyKeyNOT SUPPORTED
config.uploadBatchSizeconfig.flushQueueSize
config.headersNOT SUPPORTED
config.serverZoneconfig.serverZone
config.useDynamicConfigNOT SUPPORTED
config.serverZoneBasedApiNOT SUPPORTED
config.sessionIdconfig.sessionId
config.partnerIdconfig.partnerId
The maintenance Browser SDK offered a variety of logEvent APIs like logEventWithTimestamp, logEventWithGroups to override specific properties in the event payload. Instead of these, use the unified track API in @amplitude/analytics-browser@2+.
The logEvent() API maps to track().
const eventType = "Button Clicked"
const eventProperties = {
type: "primary",
}
amplitude.getInstance().logEvent(
eventType,
eventProperties,
)
const eventType = "Button Clicked"
const eventProperties = {
type: "primary",
}
amplitude.track(
eventType,
eventProperties,
)
The logEventWithTimestamp() API maps to track().
const eventType = "Button Clicked"
const eventProperties = {
type: "primary",
}
const timestamp = Date.now()
amplitude.getInstance().logEventWithTimestamp(
eventType,
eventProperties,
timestamp,
)
const eventType = "Button Clicked"
const eventProperties = {
type: "primary",
}
const eventOptions = {
time = Date.now()
}
amplitude.track(
eventType,
eventProperties,
eventOptions
)
The logEventWithGroups() API maps to track().
const eventType = "Button Clicked"
const eventProperties = {
type: "primary",
}
const groups = {
orgId: "12345",
}
amplitude.getInstance().logEventWithGroups(
eventType,
eventProperties,
groups,
)
const event_type = "Button Clicked"
const event_properties = {
type: "primary",
}
const groups = {
orgId: "12345",
}
const event = {
event_type,
event_properties,
groups
}
amplitude.track(event)
sendEvents()The sendEvents() API maps to flush().
amplitude.getInstance().sendEvents()
amplitude.flush()
The APIs for setting user properties are the same, except for the removal of getInstance(). Here are code snippets to migrate APIs for user properties.
api2.amplitude.com) which enforces no length limit for deviceId and userId. The latest SDK uses Amplitude's HTTP V2 API (api2.amplitude.com/2/httpapi) and requires identifiers to be at least five characters by default. When you migrate to the latest SDK, set config.minIdLength to a smaller value if you allowed identifiers with fewer than five characters.Set a userId when you invoke amplitude without calling getInstance()
const userId = "1"
amplitude.getInstance().setUserId(userId)
const userId = "1"
amplitude.setUserId(userId)
api2.amplitude.com) which enforces no length limit for deviceId and userId. The latest SDK uses Amplitude's HTTP V2 API (api2.amplitude.com/2/httpapi) and requires identifiers to be at least five characters by default. When you migrate to the latest SDK, set config.minIdLength to a smaller value if you allowed identifiers with fewer than five characters.Set a deviceId when you invoke amplitude without calling getInstance()
const deviceId = "1"
amplitude.getInstance().setDeviceId(deviceId)
const deviceId = "1"
amplitude.setDeviceId(deviceId)
Set a sessionId when you invoke amplitude without calling getInstance()
const sessionId = Date.now()
amplitude.getInstance().setSessionId(sessionId)
const sessionId = Date.now()
amplitude.setSessionId(sessionId)
The clearUserProperties API has been removed. Use the unified identify API to remove user properties.
amplitude.getInstance().clearUserProperties()
amplitude.identify(
new amplitude.Identify().identify.clearAll()
)
The setUserProperties API has been removed. Use the unified identify API to add user properties.
amplitude.getInstance().setUserProperties({
membership, "paid",
payment, "bank",
})
const identify = new amplitude.Identify()
identify
.set("membership", "paid")
.set("payment", "bank")
amplitude.identify(identify)
Make an identify call on amplitude without calling getInstance().
const identify = new amplitude.Identify()
identify.set("membership", "paid")
amplitude.getInstance().identify(identify)
const identify = new amplitude.Identify()
identify.set("membership", "paid")
amplitude.identify(identify)
Make an identify call on amplitude without calling getInstance().
const identify = new amplitude.Identify()
identify.set("membership", "paid")
amplitude.getInstance().groupIdentify(identify)
const identify = new amplitude.Identify()
identify.set("membership", "paid")
amplitude.groupIdentify(identify)
Track revenue using revenue() API on amplitude without calling getInstance().
const revenue = new amplitude.Revenue()
revenue
.setProductId("productId")
.setPrice(10)
amplitude.getInstance().logRevenueV2(revenue)
const revenue = new amplitude.Revenue()
revenue
.setProductId("productId")
.setPrice(10)
amplitude.revenue(revenue)
The configs config.language, config.library, config.platform were available in amplitude-js to allow modification of event payloads for these specific fields. Although @amplitude/analytics-browser@2+ doesn't support these configurations, you can add plugins to the new Browser SDK to enrich event payloads.
import { BrowserConfig, EnrichmentPlugin, Event, PluginType } from "@amplitude/analytics-types"
export class LibraryModifierPlugin implements EnrichmentPlugin {
name = 'library-modifier'
type = PluginType.ENRICHMENT as const
/**
* setup() is called on plugin installation
* example: client.add(new LibraryModifierPlugin());
*/
setup(config: BrowserConfig): Promise<undefined> {
this.config = config
}
/**
* execute() is called on each event instrumented
* example: client.track('New Event');
*/
execute(event: Event): Promise<Event> {
event.library = 'my-library-name/1.0.0'
return Promise.resolve(event)
}
}
To install your custom plugin, use add() with your custom plugin as a parameter.
import * as amplitude from '@amplitude/analytics-browser'
amplitude.add(new LibraryModifierPlugin())
amplitude.init(API_KEY, OPTIONAL_USER_ID)
To defer initialization in amplitude-js, call init with config.deferInitialization set to true, and eventually call enableTracking() to formalize initialization and send all enqueued events.
amplitude.getInstance().init(API_KEY, OPTIONAL_USER_ID, {
deferInitialization: true,
})
amplitude.getInstance().logEvent("Event 1")
amplitude.getInstance().logEvent("Event 2")
amplitude.getInstance().logEvent("Event 3")
amplitude.getInstance().enableTracking()
init() at a later time than track(). All track() calls are processed after initialization completes.
amplitude.track("Event 1")
amplitude.track("Event 2")
amplitude.track("Event 3")
amplitude.init(API_KEY, OPTIONAL_USER_ID)
In amplitude-js, enable web attribution through the following configurations:
config.includeGclidconfig.includeFbclidconfig.includeReferrerconfig.includeUtmIn @amplitude/analytics-browser@2+, the single configuration config.autocapture.attribution controls the web attribution. This configuration's default setting is true, and it captures all campaign parameters. These are the same campaign parameters supported in amplitude-js.
Sometimes you should send events immediately, like when a user navigates away from a page. This is common when tracking button clicks that direct the user to another page while sending event payload in batches.
In amplitude-js do this by using onExitPage() callback.
amplitude.getInstance().init(API_KEY, OPTIONAL_USER_ID, {
onExitPage: () => {
amplitude.sendEvents()
},
})
pagehide event.
window.addEventListener('pagehide', () => {
// Set https transport to use sendBeacon API
amplitude.setTransport('beacon')
// Send all pending events to server
amplitude.flush()
});
amplitude-js passes one init callback function for executing any function after initialization and two separate callback functions for success and error network requests. With @amplitude/analytics-browser@2+ supporting promises (and async/await), the asynchronous methods like init(), track(), identify(), groupIdentify() return a custom promise interface.
const initResult = await amplitude.init("YOUR_API_KEY").promise
if (initResult.code === 200) {
// success logic
} else {
// error logic
}
const result = await amplitude.track("Button Clicked").promise
if (result.code === 200) {
// success logic
} else {
// error logic
}
May 2nd, 2024
Need help? Contact Support
Visit Amplitude.com
Have a look at the Amplitude Blog
Learn more at Amplitude Academy
© 2025 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.