Skip to content

Browser SDK 2.0 Migration Guide

Amplitude Browser SDK 2.0 (@amplitude/analytics-browser) features default event tracking, improved marketing attribution tracking, simplified interface and a lighter weight package.

Browser SDK 2.0 is compatible with Amplitude Session Replay.

Using Browser SDK with Ampli V2

Ampli V2 is compatible with both Browser SDK 2.0 and Browser SDK 1.0. Follow this migration guide to upgrade.

Terminology

  • @amplitude/analytics-browser@1: Browser SDK 1.0
  • @amplitude/analytics-browser@2: Browser SDK 2.0

Dependency

{
  "dependencies": {
-     "@amplitude/analytics-browser": "^1"
+     "@amplitude/analytics-browser": "^2"
  }
}

Default events tracking

Starting Browser SDK 2.0, default tracking is enabled by default. Default tracking is implicit tracking performed by Amplitude on your behalf, and includes page views, sessions, file downloads, form interactions, and marketing attribution.

To opt out of default tracking, set options.defaultTracking to false.

amplitude.init(API_KEY, undefined, {
  defaultTracking: false,
});

Additionally, you can pick and choose which events you want tracked by Amplitude. For example, if you only want default tracking for marketing attribution and page views, you can use the code below.

amplitude.init(API_KEY, undefined, {
  defaultTracking: {
    attribution: true,
    pageViews: true,
    sessions: false,
    fileDownload: false,
    formInteractions: false,
  },
});

Marketing attribution tracking

Starting Browser SDK 2.0, Amplitude consolidates Browser SDK and Marketing Analytics SDK to provide a single solution for both product and marketing analytics use case.

Marketing attribution tracking excludes all subdomains of the same root domain as referrer. This means traffic from one subdomain to another (ie analytics.amplitude.com to experiment.amplitude.com) are not tracked with no additional configuration.

Browser SDK 1.0, by default, allows other subdomains to be tracked as referrer. If this is behavior is desired, refer to the code below.

Deprecates options.attribution.trackNewCampaigns

This option is no longer supported as it has been adopted a non configurable default behavior. Amplitude tracks any changes to campaign parameters which includes UTM, referrer and click ID parameters.

Deprecates options.attribution.trackPageViews

This option no longer exists but Amplitude can be configured similarly using page view options.

  amplitude.init(API_KEY, undefined, {
-   attribution: {
-     trackPageViews: true
+   defaultTracking: {
+     pageViews: {
+       trackOn: 'attribution',
+     },
    },
  });

Starting Browser SDK 2.0, Amplitude has simplified the options to manage the use of cookies. By default, user identity is stored on browser cookies.

Using an alternative storage API

  amplitude.init(API_KEY, undefined, {
-   disableCookies: true,
+   identityStorage: 'localStorage',
  });

Disabling user identity persistence

- import { MemoryStorage } from '@amplitude/analytics-core';
-
  amplitude.init(API_KEY, undefined, {
-   cookieStorageProvider: new MemoryStorage(),
+   identityStorage: 'none',
  });

The options to manage cookie usage are now nested under options.cookieOptions for a more discoverable interface.

  amplitude.init(API_KEY, undefined, {
-   cookieExpiration: 365,
-   cookieSameSite: 'Lax',
-   cookieSecure: false,
-   cookieUpgrade: true,
-   domain: '',
+   cookieOptions: {
+     expiration: 365,
+     sameSite: 'Lax',
+     secure: false,
+     upgrade: true,
+     domain: '',
+   },
  });

Deprecates user agent client-side parsing

Starting Browser SDK 2.0, Amplitude replaced enrichment of user properties relating to user agent from client-side to server-side. The enriched user properties include os_name, os_version, device_model, device_manufacturer. While the new enrichment strategy yields more accurate results, it also yields slightly different results than Browser SDK 1.0 and may impact your existing analytics charts that query these properties. To prevent this breaking change from impacting you, install @amplitude/plugin-user-agent-enrichment-browser to configure Amplitude to enrich these user properties on the client-side and yield enrichment results similar to Browser SDK 1.0. Please visit our documentation for more details.

No to enums

Amplitude no longer requires the use of enums specifically TransportType, ServerZone and PluginType, and accepts its literal values.

Setting transport provider on initialization

  import * as amplitude from '@amplitude/analytics-browser';

  amplitude.init(API_KEY, USER_ID, {
-   transport: amplitude.Types.TransportType.Fetch,
+   transport: 'fetch',
  });

Setting transport provider using setTransport()

  import * as amplitude from '@amplitude/analytics-browser';

- amplitude.setTransport(amplitude.Types.TransportProvider.Fetch);
+ amplitude.setTransport('fetch');

Setting server zone on initialization

  import * as amplitude from '@amplitude/analytics-browser';

  amplitude.init(API_KEY, USER_ID, {
-   serverZone: amplitude.Types.ServerZone.US,
+   serverZone: 'US',
  });

Simplified plugin interface

Amplitude has made it easier to create your own plugins, requiring less properties for faster authoring.

plugin.name [optional]

The name field is an optional property that allows you to reference the plugin for deletion purposes. If not provided, Amplitude will assign a random name when the plugin is added. If you do not plan to delete your plugin, you can skip assigning a name.

plugin.type [optional]

The type field is an optional property that defines the type of plugin you are creating. Refer to execute() function below to distinguish the two types. If not defined, the plugin defaults to an enrichment type.

plugin.setup() [optional]

The setup function is an optional method and is called when the plugin is added or on first init whichever happens later. This function accepts two parameters: 1) Amplitude configuration; and 2) Amplitude instance. This is useful for setup operations and tasks that depend on either the Amplitude configuration or instance. Examples include assigning baseline values to variables, setting up event listeners, and many more.

plugin.execute() [optional for type: enrichment]

For enrichment plugins, execute function is an optional method and is called on each event. This function must return a new event, otherwise, the passed event is dropped from the queue. This is useful for cases where you need to add/remove properties from events, filter events, or perform any operation for each event tracked.

For destination plugins, execute function is a required method and is called on each event. This function must return a response object with keys: event (BaseEvent), code (number), and message (string). This is useful for sending events for third-party endpoints.

plugin.teardown() [optional]

The teardown function is an optional method and is called when Amplitude re-initializes. This is useful for resetting unneeded persistent state created/set by setup or execute methods. Examples include removing event listeners, mutation observers, etc.

Comparison

This section provides a comparison between different version of Browser SDK. This overview provides key insights for developers, identifying areas that have significant changes and need careful consideration.

Feature
Package @amplitude/analytics-browser@2 @amplitude/analytics-browser@1
Web Attribution Enabled by default with Web Attribution V2. Enabled by default with Web Attribution V1.
Default Event Tracking Enabled by default Disabled by default

Web Attribution V2 vs Web Attribution V1

Web Attribution V2
Web Attribution V1
  • Enabled by default.
  • Tracks attribution on init with a new campaign regardless of session context (new or existing). Not configurable.
  • Default value for all initial touch attribution properties is "EMPTY". Configurable with config.initialEmptyValue. Value is configurable.
  • Does not start a new session on new campaign. Configurable with config.resetSessionOnNewCampaign = true.
  • Tracks ad click IDs.
  • Enabled by default.
  • Tracks attribution on init with a new session. Not configurable.
  • Does not track attribution on init with a new campaign. Configurable with config.trackNewCampaigns.
  • Default value for all initial touch attribution properties is "EMPTY". Configurable with config.initialEmptyValue.
  • Does not start a new session on new campaign. Configurable with config.resetOnNewCampaign.
  • Tracks ad click IDs.
Web Attribution V2 Web Attribution V1
Web Attribution V2 Web Attribution V1

Was this page helpful?