Browser SDK 1

The Browser SDK lets you send events to Amplitude.

Note

An improved version of Amplitude Browser SDK is now available. Amplitude Browser SDK 2.0 features default event tracking, improved marketing attribution tracking, simplified interface and a lighter weight package. Amplitude recommends the Browser SDK 2.0 for both product analytics and marketing analytics use cases. Upgrade to the latest Browser SDK 2.0. See the Migration Guide for more information.

Initialize the SDK

Sending events

This SDK uses the HTTP V2 API and follows the same constraints for events. Make sure that all events logged in the SDK have the event_type field and at least one of deviceId  (included by default) or userId, and follow the HTTP API's constraints on each of those fields.

To prevent instrumentation issues, device IDs and user IDs must be strings with a length of 5 characters or more. If an event contains a device ID or user ID that's too short, the ID value is removed from the event. If the event doesn't have a userId or deviceId value, Amplitude may reject the upload with a 400 status. Override the default minimum length of 5 characters by setting the minIdLength config option.

You must initialize the SDK before you can instrument any events. Your Amplitude project's API key is required. You can pass an optional user ID and config object in this call. You can use the SDK anywhere after it's initialized anywhere in an application.

1// Option 1, initialize with API_KEY only
2amplitude.init(API_KEY);
3 
4// Option 2, initialize with user ID if it's already known
5amplitude.init(API_KEY, 'user@amplitude.com');
6 
7// Option 3, initialize with configuration
8amplitude.init(API_KEY, 'user@amplitude.com', options);

Configure the SDK

Configuration options

Name Description Default Value
instanceName string. The instance name. $default_instance
flushIntervalMillis number. Sets the interval of uploading events to Amplitude in milliseconds. 1,000 (1 second)
flushQueueSize number. Sets the maximum number of events batched in a single upload attempt. 30 events
flushMaxRetries number. Sets the maximum number of retries for failed upload attempts. This is only applicable to errors that can be retried. 5 times.
logLevel LogLevel.None or LogLevel.Error or LogLevel.Warn or LogLevel.Verbose or LogLevel.Debug. Sets the log level. LogLevel.Warn
loggerProvider Logger. Sets a custom loggerProvider class from the Logger to emit log messages to desired destination. Amplitude Logger
minIdLength number. Sets the minimum length for the value of userId and deviceId properties. 5
optOut boolean. Sets permission to track events. Setting a value of true prevents Amplitude from tracking and uploading events. false
serverUrl string. Sets the URL where events are upload to. https://api2.amplitude.com/2/httpapi
serverZone EU or US. Sets the Amplitude server zone. Set this to EU for Amplitude projects created in EU data center. US
useBatch boolean. Sets whether to upload events to Batch API instead of the default HTTP V2 API or not. false
appVersion string. Sets an app version for events tracked. This can be the version of your application. For example: "1.0.0" undefined
deviceId string. Sets an identifier for the device running your application. UUID()
cookieExpiration number. Sets expiration of cookies created in days. 365 days
cookieSameSite string. Sets SameSite property of cookies created. Lax
cookieSecure boolean. Sets Secure property of cookies created. false
cookieStorage Storage<UserSession>. Sets a custom implementation of Storage<UserSession> to persist user identity. MemoryStorage<UserSession>
cookieUpgrade boolean. Sets upgrading from cookies created by maintenance Browser SDK. If true, new Browser SDK deletes cookies created by maintenance Browser SDK. If false, Browser SDK keeps cookies created by maintenance Browser SDK. true
disableCookies boolean. Sets permission to use cookies. If value is true, localStorage API is used to persist user identity. The cookies is enable by default.
domain string. Sets the domain property of cookies created. undefined
partnerId string. Sets partner ID. Amplitude requires the customer who built an event ingestion integration to add the partner identifier to partner_id. undefined
sessionTimeout number. Sets the period of inactivity from the last tracked event before a session expires in milliseconds. 1,800,000 milliseconds (30 minutes)
userId number. Sets an identifier for the tracked user. Must have a minimum length of 5 characters unless overridden with the minIdLength option. undefined
trackingOptions TrackingOptions. Configures tracking of more properties. See the Optional tracking section for more information. Enable all tracking options by default.

Along with the basic configuration options, you can configure attribution.

Attribution options

Name Description Default Value
config.attribution.disabled boolean. Whether disable the attribution tracking. false
config.attribution.excludeReferrers string[]. Exclude the attribution tracking for the provided referrers string Including all referrers by default.
config.attribution.initialEmptyValue string. Customize the initial empty value for attribution related user properties to any string value. EMPTY
config.attribution.resetSessionOnNewCampaign boolean. Whether to reset user sessions when a new campaign is detected. Note a new false
config.attribution.trackNewCampaigns boolean. Whether tracking new campaigns on the current session. false
config.attribution.trackPageViews boolean. Whether track page view on attribution. Note that config.defaultTracking.pageViews has higher priority over this configuration. Learn more about it here. false

Configure batching behavior

To support high-performance environments, the SDK sends events in batches. The track method queues in memory every event it logs. The SDK flushes events in batches in the background. Customize batch behavior with flushQueueSize and flushIntervalMillis. By default, the serverUrl is https://api2.amplitude.com/2/httpapi. For customers who want to send large batches of data at a time, set useBatch to true to set setServerUrl to batch event upload API https://api2.amplitude.com/batch. Both the regular mode and the batch mode use the same events upload threshold and flush time intervals.

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 // Events queued in memory will flush when number of events exceed upload threshold
3 // Default value is 30
4 flushQueueSize: 50,
5 // Events queue will flush every certain milliseconds based on setting
6 // Default value is 10000 milliseconds
7 flushIntervalMillis: 20000,
8 // Using batch mode with batch API endpoint, `https://api2.amplitude.com/batch`
9 useBatch: true
10});

EU data residency

Configure the server zone when you initialize the client data to Amplitude's EU servers. The SDK sends data based on the server zone if it's set.

Note

For EU data residency, create your project in and use an API key from Amplitude EU.

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 serverZone: 'EU',
3});

Debugging

Control the level of logs the SDK prints to the console with the following logLevel settings:

Log level Description
none Suppresses all log messages
error Shows error messages only
warn Default. Shows error and warning messages.
verbose Shows informative messages.
debug Shows all messages, including function context information for each public method the SDK invokes. Amplitude recommends this log level for development only.

Set the logLevel parameter.

1amplitude.init(AMPLITUDE_API_KEY, OPTIONAL_USER_ID, {
2 logLevel: amplitude.Types.LogLevel.Warn,
3});

The default logger outputs log to the developer console. You can provide your own logger implementation based on the Logger interface for any customization purpose. For example, collecting any error messages from the SDK in a production environment.

Set the logger by configuring the loggerProvider with your own implementation.

1amplitude.init(AMPLITUDE_API_KEY, OPTIONAL_USER_ID, {
2 loggerProvider: new MyLogger(),
3});

Debug mode

Enable the debug mode by setting the logLevel to "Debug", for example:

1amplitude.init(AMPLITUDE_API_KEY, OPTIONAL_USER_ID, {
2 logLevel: amplitude.Types.LogLevel.Debug,
3});

With the default logger, extra function context information is output to the developer console when invoking any SDK public method, including:

  • type: Category of this context, for example "invoke public method".
  • name: Name of invoked function, for example "track".
  • args: Arguments of the invoked function.
  • stacktrace: Stacktrace of the invoked function.
  • time: Start and end timestamp of the function invocation.
  • states: Useful internal states snapshot before and after the function invocation.

Track an event

Events represent how users interact with your application. For example, "Button Clicked" might be an action you want to track.

1// Track a basic event
2amplitude.track('Button Clicked');
3 
4// Track events with optional properties
5const eventProperties = {
6 buttonColor: 'primary',
7};
8amplitude.track('Button Clicked', eventProperties);

You can also pass a BaseEvent object to track. For more information, see the BaseEvent interface for all available fields.

1const event_properties = {
2 buttonColor: 'primary',
3};
4 
5const event = {
6 event_type: "Button Clicked",
7 event_properties,
8 groups: { 'role': 'engineering' },
9 group_properties: { 'groupPropertyKey': 'groupPropertyValue' }
10};
11 
12amplitude.track(event);

Track events to multiple projects

By default, Amplitude SDKs send data to one Amplitude project. To send data to more than one project, add an instance of the Amplitude SDK for each project you want to receive data. Then, pass instance variables to wherever you want to call Amplitude. Each instance allows for independent apiKey, userId, deviceId, and settings values.

1const defaultInstance = amplitude.createInstance();
2defaultInstance.init(API_KEY_DEFAULT);
3 
4const envInstance = amplitude.createInstance();
5envInstance.init(API_KEY_ENV, {
6 instanceName: 'env',
7});

Track default events

Starting in SDK version 1.9.1, the Browser SDK tracks default events, and adds a configuration to control the collection of default events. Browser SDK tracks the following default events:

  • Page views
  • Sessions
  • Form interactions
  • File downloads
Name Value Description
config.defaultTracking.pageViews Optional. boolean Enables default page view tracking. If value is true, Amplitude tracks page view events on initialization. Default value is false.

Event properties tracked includes: [Amplitude] Page Domain, [Amplitude] Page Location, [Amplitude] Page Path, [Amplitude] Page Title, [Amplitude] Page URL

See Tracking page views for more information.
config.defaultTracking.sessions Optional. boolean Enables session tracking. If value is true, Amplitude tracks session start and session end events. Default value is false.

See Tracking sessions for more information.
config.defaultTracking.formInteractions Optional. boolean Enables form interaction tracking. If value is true, Amplitude tracks form start and form submit events. Default value is false.

Event properties tracked includes: [Amplitude] Form ID, [Amplitude] Form Name, [Amplitude] Form Destination

See Tracking form interactions for more information.
config.defaultTracking.fileDownloads Optional. boolean Enables file download tracking. If value is true, Amplitude tracks file download events. Default value is false.

Event properties tracked includes: [Amplitude] File Extension, [Amplitude] File Name, [Amplitude] Link ID, [Amplitude] Link Text, [Amplitude] Link URL

See Tracking file downloads for more information.

Use the following code sample to start tracking all default events. Or, omit the configuration to keep them disabled.

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 defaultTracking: {
3 pageViews: true,
4 sessions: true,
5 formInteractions: true,
6 fileDownloads: true,
7 },
8});

To track all default events, you can also set config.defaultTracking to true. This setting enables the SDK to track any new default events that Amplitude may add.

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 defaultTracking: true,
3});

Track page views

When you set config.defaultTracking.pageViews to true, Amplitude uses default page view tracking behavior. This sends a page view event on initialization and appears in Amplitude as [Amplitude] Page Viewed.

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 defaultTracking: {
3 pageViews: true,
4 },
5});

Note

You may notice that both config.defaultTracking.pageViews and config.attribution.trackPageViews have configurations for whether to enable page view tracking especially when you are using the web attribution plugin. Notice that config.defaultTracking.pageViews has higher priority over config.attribution.trackPageViews which means that config.defaultTracking.pageViews overwrites the setting of the attribution page view event. When config.attribution.trackPageViews is enabled, the SDK tracks page view events only when attribution changed. When config.defaultTracking.pageViews is enabled, the SDK tracks page view events when page changed.

Advanced configuration for tracking page views

Use advanced configuration for better control of when the SDK sends page view events.

Name Value Description
config.defaultTracking.pageViews.trackOn Optional. "attribution" or () => boolean Provides advanced control on when page view events are tracked.

You can omit or set the value to undefined, and configure page view events to be tracked on initialization.

You can set the value to "attribution" and configure page view events to be tracked only when web attribution are tracked.

You can set the value to a function that returns a boolean (true or false) and configure page view events to be tracked based on your criteria.
config.defaultTracking.pageViews.trackHistoryChanges Optional. "pathOnly" or "all" Provides advanced control for single page application on when page views are tracked.

You can omit or set the value to "all", and configure page view events to be tracked on any navigation change to the URL within your single page application. For example: navigating from https://amplitude.com/#company to https://amplitude.com/#blog.

Set the value to "pathOnly", and configure page view events to be tracked on navigation change to the URL path only within your single page application. For example: navigating from https://amplitude.com/company to https://amplitude.com/blog.
config.defaultTracking.pageViews.eventType Optional. string Customize the event_type for page view event.

For example, you can configure Amplitude to track page views only when the URL path contains a certain substring, for example home.

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 defaultTracking: {
3 pageViews: {
4 trackOn: () => {
5 return window.location.pathname.includes('home');
6 },
7 },
8 },
9});

Amplitude tracks the following information with page view events.

Name Description Default Value
event_type string. The event type for page view event. Configurable through defaultTracking.pageViews.eventType or enrichment plugin. [Amplitude] Page Viewed from version 1.9.1.
event_properties.[Amplitude] Page Domain string. The page domain. location.hostname or ''.
event_properties.[Amplitude] Page Location string. The page location. location.href or ''.
event_properties.[Amplitude] Page Path string. The page path. location.path or ''.
event_properties.[Amplitude] Page Title string. The page title. document.title or ''.
event_properties.[Amplitude] Page URL string. The value of page URL. location.href.split('?')[0] or ''.
event_properties.${CampaignParam} string. The value of UTMParameters ReferrerParameters ClickIdParameters if has any. Check here for the possible keys. Any undefined campaignParam or undefined.

See this example to understand how to enrich default page view events, such as adding more properties along with page view tracking.

Track sessions

Set config.defaultTracking.sessions to true to enable Amplitude to track sessions.

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 defaultTracking: {
3 sessions: true,
4 },
5});

A session is the period of time a user has your website open. See How Amplitude defines sessions for more information. When a new session starts, Amplitude tracks a session start event is and is the first event of the session. The event type for session start is [Amplitude] Start Session. When an existing session ends, Amplitude tracks [Amplitude] End Sessions, which is the last event of the session.

Track form interactions

Set config.defaultTracking.formInteractions to true to enable Amplitude to track form interactions.

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 defaultTracking: {
3 formInteractions: true,
4 },
5});

Amplitude tracks [Amplitude] Form Started when the user initially interacts with the form. An initial interaction can be the first change to a text input, radio button, or dropdown.

Amplitude tracks [Amplitude] Form Submitted when the user submits the form. If the user submits a form with no initial change to any form fields, Amplitude sends both [Amplitude] Form Started and [Amplitude] Form Submitted events.

Amplitude can track forms that built with <form> tags and <input> tags nested. For example:

1<form id="subscriber-form" name="subscriber-form" action="/subscribe">
2 <input type="text" />
3 <input type="submit" />
4</form>

Track file downloads

Set config.defaultTracking.fileDownloads to true to enable Amplitude to track file downloads.

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 defaultTracking: {
3 fileDownloads: true,
4 },
5});

Amplitude tracks a file download event when an anchor or <a> tag linked to a file is clicked. The event type for file download is [Amplitude] File Downloaded. Amplitude determines that the anchor or <a> tag linked to a file if the file extension matches the following regex:

pdf|xlsx?|docx?|txt|rtf|csv|exe|key|pp(s|t|tx)|7z|pkg|rar|gz|zip|avi|mov|mp4|mpe?g|wmv|midi?|mp3|wav|wma

User properties

User properties are details like device details, user preferences, or language to help you understand your users at the time they performed an action in your app.

Identify is for setting the user properties of a particular user without sending any event. The SDK supports the operations set, setOnce, unset, add, append, prepend, preInsert, postInsert, and remove on individual user properties. Declare the operations via a provided Identify interface. You can chain together multiple operations in a single Identify object. The Identify object is then passed to the Amplitude client to send to the server.

Note

If you send the Identify call is sent after the event, the results of operations are visible immediately in the dashboard user’s profile area. However, they don't appear in chart results until another event is sent after the Identify call. The identify call only affects events going forward. More details here.

Set a user property

The Identify object provides controls over setting user properties. It works like this: first, instantiate an Identify object, then call Identify methods on it, and finally, the client can make a call with the Identify object.

1const identifyEvent = new amplitude.Identify();
2amplitude.identify(identifyEvent);

Identify.set

This method sets the value of a user property. For example, you can set a role property of a user.

1const identifyEvent = new amplitude.Identify();
2identifyEvent.set('location', 'LAX');
3 
4amplitude.identify(identifyEvent);

Identify.setOnce

This method sets the value of a user property only one time. Subsequent calls using setOnce() are ignored. For example, you can set an initial login method for a user and because only the initial value is tracked, setOnce() ignores later calls.

1const identifyEvent = new amplitude.Identify();
2identifyEvent.setOnce('initial-location', 'SFO');
3 
4identify(identifyEvent);

Identify.add

This method increments a user property by some numerical value. If the user property doesn't have a value set yet, it's initialized to 0 before it's incremented. For example, you can track a user's travel count.

1const identifyEvent = new amplitude.Identify();
2identifyEvent.add('travel-count', 1);
3 
4amplitude.identify(identifyEvent);

Arrays in user properties

You can use arrays as user properties. Directly set arrays or use prepend, append, preInsert and postInsert to generate an array.

Identify.prepend

This method prepends a value or values to a user property array. If the user property doesn't have a value set yet, it's initialized to an empty list before the new values are prepended.

1const identifyEvent = new Identify();
2identifyEvent.prepend('visited-locations', 'LAX');
3 
4identify(identifyEvent);

Identify.append

This method appends a value or values to a user property array. If the user property doesn't have a value set yet, it's initialized to an empty list before the new values are prepended.

1const identifyEvent = new amplitude.Identify();
2identifyEvent.append('visited-locations', 'SFO');
3 
4amplitude.identify(identifyEvent);

Identify.preInsert

This method pre-inserts a value or values to a user property if it doesn't exist in the user property yet. Pre-insert means inserting the values at the beginning of a given list. If the user property doesn't have a value set yet, it's initialized to an empty list before the new values are pre-inserted. If the user property has an existing value, this method is a no-op.

1const identifyEvent = new amplitude.Identify();
2identifyEvent.preInsert('unique-locations', 'LAX');
3 
4identify(identifyEvent);

Identify.postInsert

This method post-inserts a value or values to a user property if it doesn't exist in the user property yet. Post-insert means inserting the values at the end of a given list. If the user property doesn't have a value set yet, it's initialized to an empty list before the new values are post-inserted. If the user property has an existing value, this method is a no-op..

1const identifyEvent = new amplitude.Identify();
2identifyEvent.postInsert('unique-locations', 'SFO');
3 
4amplitude.identify(identifyEvent);

Identify.remove

This method removes a value or values to a user property if it exists in the user property. Remove means remove the existing values from the given list. If the user property has an existing value, this method is a no-op.

1const identifyEvent = new amplitude.Identify();
2identifyEvent.remove('unique-locations', 'JFK')
3 
4amplitude.identify(identifyEvent);

User groups

Amplitude supports assigning users to groups and performing queries, such as Count by Distinct, on those groups. If at least one member of the group has performed the specific event, then the count includes the group.

For example, you want to group your users based on what organization they're in by using an 'orgId'. Joe is in 'orgId' '10', and Sue is in 'orgId' '15'. Sue and Joe both perform a certain event. You can query their organizations in the Event Segmentation Chart.

When setting groups, define a groupType and groupName. In the previous example, 'orgId' is the groupType and '10' and '15' are the values for groupName. Another example of a groupType could be 'sport' with groupName values like 'tennis' and 'baseball'.

Setting a group also sets the groupType:groupName as a user property, and overwrites any existing groupName value set for that user's groupType, and the corresponding user property value. groupType is a string, and groupName can be either a string or an array of strings to tell that a user is in multiple groups.

Example

If Joe is in 'orgId' '15', then the groupName would be '15'.

1// set group with a single group name
2amplitude.setGroup('orgId', '15');

If Joe is in 'sport' 'soccer' and 'tennis', then the groupName would be '["tennis", "soccer"]'.

1// set group with multiple group names
2amplitude.setGroup('sport', ['soccer', 'tennis']);

You can also set event-level groups by passing an Event Object with groups to track. With event-level groups, the group designation applies only to the specific event being logged, and doesn't persist on the user unless you explicitly set it with setGroup.

1amplitude.track({
2 event_type: 'event type',
3 event_properties: { eventPropertyKey: 'event property value' },
4 groups: { 'orgId': '15' }
5})

Track revenue

The preferred method of tracking revenue for a user is to use revenue() in conjunction with the provided Revenue interface. Revenue instances store each revenue transaction and allow you to define several special revenue properties (such as 'revenueType' and 'productIdentifier') that are used in Amplitude's Event Segmentation and Revenue LTV charts. These Revenue instance objects are then passed into revenue() to send as revenue events to Amplitude. This lets automatically display data relevant to revenue in the platform. You can use this to track both in-app and non-in-app purchases.

To track revenue from a user, call revenue each time a user generates revenue. In this example, the user purchased three units of a product at $3.99.

1const event = new amplitude.Revenue()
2 .setProductId('com.company.productId')
3 .setPrice(3.99)
4 .setQuantity(3);
5 
6amplitude.revenue(event);

Revenue interface

Name Description Default Value
product_id Optional. string. An identifier for the product. Amplitude recommend something like the Google Play Store product ID. Empty string.
quantity Required. number. The quantity of products purchased. Note: revenue = quantity * price. 1
price Required. number. The price of the products purchased, and this can be negative. Note: revenue = quantity * price. null
revenue_type Optional, but required for revenue verification. string. The revenue type (for example, tax, refund, income). null
receipt Optional. string. The receipt identifier of the revenue. null
receipt_sig Optional, but required for revenue verification. string. The receipt signature of the revenue. null
properties Optional. { [key: string]: any }. An object of event properties to include in the revenue event. null

Flush the event buffer

The flush method triggers the client to send buffered events immediately.

1amplitude.flush();

By default, flush is called automatically in an interval, if you want to flush the events altogether, you can control the async flow with the optional Promise interface, for example:

1amplitude.init(API_KEY).promise.then(function() {
2 amplitude.track('Button Clicked');
3 amplitude.flush();
4});

Custom user ID

If your app has its login system that you want to track users with, you can call setUserId at any time.

1amplitude.setUserId('user@amplitude.com');

You can also assign the User ID as an argument to the init call.

1amplitude.init(API_KEY, 'user@amplitude.com');

Custom session ID

You can assign a new Session ID using setSessionId. When setting a custom session ID, make sure the value is in milliseconds since epoch (Unix Timestamp).

1amplitude.setSessionId(Date.now());

Custom device ID

If your app has its login system that you want to track users with, you can call setUserId at any time.

You can assign a new device ID using deviceId. When setting a custom device ID, make sure the value is sufficiently unique. Amplitude recommends using a UUID.

1amplitude.setDeviceId(uuid());

Reset when a user logs out

reset is a shortcut to anonymize users after they log out, by:

  • setting userId to undefined
  • setting deviceId to a new UUID value

With an undefined userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.

1amplitude.reset();

Opt users out of tracking

You can turn off logging for a given user by setting setOptOut to true.

1amplitude.setOptOut(true);

Events aren't saved or sent to the server while setOptOut is enabled, and the setting persists across page loads.

Re-enable logging by setting setOptOut to false.

1amplitude.setOptOut(false);

Optional tracking

By default, the SDK tracks these properties automatically. You can override this behavior by passing a configuration called trackingOptions when initializing the SDK, setting the appropriate options to false.

Tracking Options Default
deviceManufacturer true
deviceModel true
ipAddress true
language true
osName true
osVersion true
platform true
1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 trackingOptions: {
3 deviceManufacturer: false,
4 deviceModel: false,
5 ipAddress: false,
6 language: false,
7 osName: false,
8 osVersion: false,
9 platform: false,
10 },
11});

Callback

All asynchronous APIs are optionally awaitable through a Promise interface. This also serves as a callback interface.

1amplitude.init("apikey", "12321.com").promise.then(function() {
2 // init callback
3})
4 
5amplitude.track('Button Clicked').promise.then(function(result) {
6 result.event; // {...} (The final event object sent to Amplitude)
7 result.code; // 200 (The HTTP response status code of the request.
8 result.message; // "Event tracked successfully" (The response message)
9});

1// Using async/await
2const initResult = await amplitude.init("apikey", "12321.com").promise;
3 
4const results = await amplitude.track('Button Clicked').promise;
5result.event; // {...} (The final event object sent to Amplitude)
6result.code; // 200 (The HTTP response status code of the request.
7result.message; // "Event tracked successfully" (The response message)

Plugins

Plugins allow you to extend Amplitude SDK's behavior by, for example, modifying event properties (enrichment type) or sending to third-party APIs (destination type). A plugin is an object with methods setup() and execute().

add

The add method adds a plugin to Amplitude. Plugins can help processing and sending events.

1amplitude.add(new Plugin());

remove

The remove method removes the given plugin name from the client instance if it exists.

1amplitude.remove(plugin.name);

Create your custom plugin

Plugin.setup

This method contains logic for preparing the plugin for use and has config as a parameter. The expected return value is undefined. A typical use for this method, is to copy configuration from config or instantiate plugin dependencies. This method is called when the plugin is registered to the client via amplitude.add().

Plugin.execute

This method contains the logic for processing events and has event as parameter. If used as enrichment type plugin, the expected return value is the modified/enriched event. If used as a destination type plugin, the expected return value is a map with keys: event (BaseEvent), code (number), and message (string). This method is called for each event that's instrumented using the client interface, including Identify, GroupIdentify and Revenue events.

Plugin examples

Destination type plugin

Here's an example of a plugin that sends each event that's instrumented to a target server URL using your preferred HTTP client.

1function myDestinationPlugin (serverUrl) {
2 const name = 'my-destination-plugin';
3 const type = amplitude.Types.PluginType.DESTINATION;
4 let amplitudeConfig;
5 
6 /**
7 * setup() is called on plugin installation
8 * example: amplitude.add(new myDestinationPlugin());
9 */
10 const setup = function (config) {
11 amplitudeConfig = config;
12 }
13 
14 /**
15 * execute() is called on each event instrumented
16 * example: amplitude.track('New Event');
17 */
18 const execute = function (event) {
19 const payload = {
20 key: 'secret',
21 data: event,
22 };
23 return fetch(this.serverUrl, {
24 method: 'POST',
25 headers: {
26 'Content-Type': 'application/json',
27 Accept: '*/*',
28 },
29 body: JSON.stringify(payload),
30 }).then(function(response) {
31 return {
32 code: response.status,
33 event: event,
34 message: response.statusText,
35 };
36 });
37 }
38 
39 return {
40 name,
41 type,
42 setup,
43 execute,
44 },
45}
46 
47amplitude.init(API_KEY);
48amplitude.add(myDestinationPlugin('https://custom.domain.com'));

Enrichment type plugin

Here's an example of a plugin that modifies each event that's instrumented by adding an increment integer to event_id property of an event starting from 100.

1const addEventIdPlugin = () => {
2 const name = 'add-event-id';
3 const type = amplitude.Types.PluginType.ENRICHMENT;
4 let currentId = 100;
5 let amplitudeConfig;
6 
7 /**
8 * setup() is called on plugin installation
9 * example: amplitude.add(new AddEventIdPlugin());
10 */
11 const setup = function (config) {
12 amplitudeConfig = config;
13 }
14 
15 /**
16 * execute() is called on each event instrumented
17 * example: client.track('New Event');
18 */
19 const execute = function (event: Event) {
20 event.event_id = currentId++;
21 return event;
22 }
23 
24 return {
25 name,
26 type,
27 setup,
28 execute,
29 }
30}
31 
32amplitude.init(API_KEY);
33amplitude.add(addEventIdPlugin());

Web attribution enrichment plugin

Download the plugin-web-attribution-browser package and add the webAttributionPlugin before you call the init method.

1npm install @amplitude/plugin-web-attribution-browser

1yarn add @amplitude/plugin-web-attribution-browser

Add the plugin to the Amplitude instance.

1amplitude.add(webAttributionPlugin());
2amplitude.init(API_KEY);

See the configuration options.

Learn more about what the Web Attribution Plugin supports.

Differences from the base SDK

Enabling the Attribution plugin overwrites the default attribution tracking behavior of the SDK.

The SDK’s built in attribution tracking only tracks attribution at the start of sessions. This mean if a user re-enters the site through a new campaign channel (such as direct or an ad) in the middle of a session, this new channel isn't recorded.

If the trackNewCampaigns option is set to true, the campaigns are tracked, and the user’s session is reset when a new campaign is detected.

The Attribution plugin tracks all campaigns, regardless of whether the user is at the start of a session.

Set the resetSessionOnNewCampaign option to true to cause the user’s session to be reset when a new campaign is detected. The session isn't reset in the case where the referrer is just a different subdomain of your site.

Page view enrichment plugin

Download the plugin-page-view-tracking-browser and add pageViewTrackingPlugin before calling the init method.

1npm install @amplitude/plugin-page-view-tracking-browser

1yarn add @amplitude/plugin-page-view-tracking-browser

Add plugin to the Amplitude instance.

1amplitude.add(pageViewTrackingPlugin());
2amplitude.init(API_KEY);

See the configuration options.
Learn more about what the Page View Plugin supports.

Differences from base SDK

The base SDK sends Page View events when a user’s campaign is tracked if the attribution.trackPageViews option is set to true.

The page view plugin sends a Page View event on each page a user visits by default. It also offers options to customize this behavior.

Troubleshooting and debugging

Debugging in a browser can help you identify problems related to your code's implementation, as well as potential issues within the SDKs you're using. Here's a basic guide on how to use the browser's built-in Developer Tools (DevTools) for debugging.

Console

You can find JavaScript errors under Inspect > Console, which might have the details about the line of code and file that caused the problem. The console also allows you to execute JavaScript code in real time.

  • Enable debug mode by following these instructions. Then with the default logger, extra function context information outputs to the developer console when any SDK public method is invoked, which can be helpful for debugging.

  • Amplitude supports SDK deferred initialization. Events tracked before initialization are dispatched after the initialization call. If you can't send events but can send the event successfully after entering amplitude.init(API_KEY, 'USER_ID') in the browser console, it indicates that your amplitude.init call might not have been triggered in your codebase or you aren't using the correct Amplitude instance during initialization."

Network request

Use the Inspect > Network tab to view all network requests made by your page. Search for the Amplitude request.

Check the response code and ensure that the response payload is as expected.

Instrumentation Explorer/Chrome extension

The Amplitude Instrumentation Explorer is an extension available in the Google Chrome Web Store. The extension captures each Amplitude event you trigger and displays it in the extension popup. It's important to ensure that the event has been sent out successfully and to check the context in the event payload.

Check here for more details.

Common Issues

The following are common issues specific to Browser SDK. For more general common issues, see SDK Troubleshooting and Debugging.

Ad Blocker

Ad Blocker might lead to event dropping. These errors show the tracking has been affected by Ad Blocker. When loading via a script tag, an error may appear in the console/network tab while loading the SDK script. When loaded with npm package, there could be errors in the network tab when trying to send events to the server. The errors might vary depending on the browser.

  • Chrome (Ubuntu, MacOS)
    Console: error net::ERR_BLOCKED_BY_CLIENT
    Network: status (blocked:other)
  • Firefox (Ubuntu)
    Console: error text doesn’t contain any blocking-specific info
    Network: Transferred column contains the name of plugin Blocked by uBlock Origin
  • Safari (MacOS)
    Console: error contains text Content Blocker prevented frame ... from loading a resource from ...
    Network: it looks like blocked requests aren't listed. Not sure if it’s possible to show them.

Amplitude recommends using a proxy server to avoid this situation.

Here is the information SDK stored in the cookies. This means that client behavior, like disabling cookies or using a private browser/window/tab, affects the persistence of these saved values in the cookies. If these values aren't persistent or aren't increasing by one, that could be the reason.

CORS

Cross-Origin Resource Sharing (CORS) is a security measure implemented by browsers to restrict how resources on a web page can be requested from a different domain. It might cause this issue if you used setServerURL.

Access to fetch at 'xxx' from origin 'xxx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Cross-origin resource sharing (CORS) prevents a malicious site from reading another site's data without permission. The error message suggests that the server you're trying to access isn't allowing your origin to access the requested resource. This is due to the lack of the Access-Control-Allow-Origin header in the server's response.

  • If you have control over the server, you can "Update the server's CORS policy". Add the Access-Control-Allow-Origin header to the server's responses. This would allow your origin to make requests. The value of Access-Control-Allow-Origin can be * to allow all origins, or it can be the specific URL of your web page.

  • If you don't have control over the server, you can set up a proxy server that adds the necessary CORS headers. The web page makes requests to the proxy, which then makes requests to the actual server. The proxy adds the Access-Control-Allow-Origin header to the response before sending it back to the web page.

If you have set up an API proxy and run into configuration issues related to that on a platform you’ve selected, that’s no longer an SDK issue but an integration issue between your application and the service provider.

Events fired but no network requests

If you set the logger to "Debug" level, and see track calls in the developer console, the track() method has been called. If you don't see the corresponding event in Amplitude, the Amplitude Instrumentation Explorer Chrome extension, or the network request tab of the browser, the event wasn't sent to Amplitude. Events are fired and placed in the SDK's internal queue upon a successful track() call, but sometimes these queued events may not send successfully. This can happen when an in-progress HTTP request is cancelled. For example, if you close the browser or leave the page.

There are two ways to address this issue:

  1. If you use standard network requests, set the transport to beacon during initialization or set the transport to beacon upon page exit. sendBeacon doesn't work in this case because it sends events in the background, and doesn't return server responses like 4xx or 5xx. As a result, it doesn't retry on failure. sendBeacon sends only scheduled requests in the background. For more information, see the sendBeacon section.

  2. To make track() synchronous, add the await keyword before the call.

Advanced topics

Cross-domain tracking

You can track anonymous behavior across two different domains. Amplitude identifies anonymous users by their device IDs which must be passed between the domains. For example:

  • Site 1: www.example.com
  • Site 2: www.example.org

Users who start on Site 1 and then navigate to Site 2 must have the device ID generated from Site 1 passed as a parameter to Site 2. Site 2 then needs to initialize the SDK with the device ID.

The SDK can parse the URL parameter automatically if deviceId is in the URL query parameters.

  1. From Site 1, grab the device ID from getDeviceId().
  2. Pass the device ID to Site 2 via a URL parameter when the user navigates. (for example: www.example.com?deviceId=device_id_from_site_1)
  3. Initialize the Amplitude SDK on Site 2 with init('API_KEY', null).

If the deviceId isn't provided with the init like init('API_KEY', null, { deviceId: 'custom-device-id' }), then it automatically fallbacks to using the URL parameter.

Custom HTTP client

You can provide an implementation of Transport interface to the transportProvider configuration option for customization purpose, for example, sending requests to your proxy server with customized HTTP request headers.

1class MyTransport {
2 send(serverUrl, payload) {
3 // check example: https://github.com/amplitude/Amplitude-TypeScript/blob/main/packages/analytics-client-common/src/transports/fetch.ts
4 }
5}
6 
7amplitude.init(API_KEY, OPTIONAL_USER_ID, {
8 transportProvider: new MyTransport(),
9});

Use sendBeacon

Unlike standard network requests, sendBeacon sends events in the background, even if the user closes the browser or leaves the page.

Warning

sendBeacon sends events in the background, which means events dispatched from sendBeacon don't return a server response and can't be retried when they encounter failures like 4xx or 5xx errors. You can address these retry issues by sending one event/request, but this could increase the network load and the likelihood of throttling.

Set the transport to use sendBeacon for all events

To send an event using sendBeacon, set the transport SDK option to 'beacon' in one of two ways

1amplitude.init(API_KEY, 'user@amplitude.com',
2 {
3 transport: TransportType.SendBeacon,
4 // To make sure the event will be scheduled right away.
5 flushIntervalMillis: 0,
6 flushQueueSize: 1,
7 }
8);

Set the transport to use beacon only when exiting page

Amplitude recommends adding your own event listener for pagehide event.

1window.addEventListener('pagehide',
2 () => {
3 amplitude.setTransport('beacon')
4 // Sets https transport to use `sendBeacon` API
5 amplitude.flush()
6 },
7);

Content security policy (CSP)

If your web app configures the strict Content Security Policy (CSP) for security concerns, adjust the policy to whitelist the Amplitude domains:

  • When using "Script Loader", add https://*.amplitude.com to script-src.
  • Add https://*.amplitude.com to connect-src.

The Browser SDK uses cookie storage to persist information that multiple subdomains of the same domain may likely want to share. This includes information like user sessions and marketing campaigns, which are stored in separate cookie entries.

  • AMP: The SDK creates user session cookies with AMP prefix and the first ten digits of the API key: AMP_{first_ten_digits_API_KEY}.
  • AMP_MKTG: The SDK creates marketing campaign cookies with AMP_MKTG and the first ten digits of the API key: AMP_MKTG_{first_ten_digits_API_KEY}.
  • AMP_TEST: On initialization, the SDK creates a cookie with AMP_TEST prefix to check whether the cookie storage is working properly. Then the SDK sets the value as the current time, retrieves the cookie by a key and checks if the retrieved value matches the original set time. You can safely delete the AMP_TEST prefix cookies if, for some reason, they're not successfully deleted.
  • AMP_TDLTEST: On initialization, the SDK creates a cookie with AMP_TDLTEST prefix to find a subdomain that supports cookie storage. For example, when checking for cookie support on https://analytics.amplitude.com/amplitude/home the SDK first tries to find a subdomain that matches the root domain (amplitude.com) and then falls back to the full domain (analytics.amplitude.com). You can safely delete the AMP_TDLTEST prefix cookies if, for some reason, they're not successfully deleted.

By default, the SDK assigns these cookies to the top-level domain which supports cookie storage. Cookies can be shared on multiple subdomains which allows for a seamless user experience across all subdomains.

For example, if a user logs into the website on one subdomain (data.amplitude.com) where the SDK is initialized. On initialization, the SDK assigns cookies to .amplitude.com. If the user then navigates to another subdomain (analytics.amplitude.com), the login information can be seamlessly shared by shared cookies.

The SDK creates two types of cookies: user session cookies and marketing campaign cookies.

User session cookies

Name Description
optOut Required. A flag to opt this device out of Amplitude tracking. If this flag is set, no extra information will be stored for the user
userId Upon user log-in, if you send this value, it's stored in the cookie. Set this to uniquely identify their users (non-anonymous navigation). It's stored encoded using Base64
deviceId A randomly generated string. It persists unless a user clears their browser cookies and/ or is browsing in private mode. Even if a user consistently uses the same the device and browser, the device ID can still vary
sessionId A randomly generated string for each session
lastEventTime Time of the last event. Used to decide when to expire and create a new session Id
lastEventId Id of the last event

Marketing campaign cookies

Name Description
utm_campaign This identifies a specific campaign used (for example, "summer_sale")
utm_content This identifies what brought the user to the site and is commonly used for A/B testing (for example, "banner-link", "text-link")
utm_id An optional parameter for tracking unique IDs or transaction IDs associated with the link.
utm_medium This identifies a specific campaign used (for example, "summer_sale")
utm_source This identifies which website sent the traffic (for example, Google, Facebook)
utm_term This identifies paid search terms used (for example, product+analytics)
referrer The last page the user was on (for example, https://amplitude.com/behavioral-analytics-platform?ref=nav)
referring_domain The domain that the user was last on (for example, https://amplitude.com)
dclid Google campaign manager Click Identifier
gbraid Google Click Identifier for iOS device from Web to App
gclid Google Click Identifier from URL parameters
fbclid Facebook Click Identifier from URL parameters
ko_click_id Kochava Click Identifier from URL parameters
msclkid Microsoft Click Identifier
ttclid TikTok Click Identifier
twclid Twitter Click Identifier from URL parameter
wbraid Google Click Identifier for iOS device from App to Web
li_fat_id LinkedIn member indirect identifier for Members for conversion tracking, retargeting, analytics
rtd_cid Reddit Click Identifier

Disable cookies

You can opt-out of using cookies by setting disableCookies to true so that the SDK will use LocalStorage instead. LocalStorage is a great alternative, but because access to LocalStorage is restricted by subdomain, you can't track anonymous users across subdomains of your product (for example: www.amplitude.com vs analytics.amplitude.com).

Device ID lifecycle

The SDK initializes the device ID in the following order, with the device ID being set to the first valid value encountered:

  1. Device id in configuration on initialization
  2. "deviceId" value from URL parameter, for example http://example.com/?deviceId=123456789. See cross domain tracking for more details
  3. Device id in cookie storage. Refer to cookie management for more details
  4. Device id in cookie storage of Browser SDK. Refer to cookie management for more details
  5. A randomly generated 36-character UUID

When does a device ID change

A device ID changes in many scenarios:

Note

setDeviceId also updates the identity store to propagate new user info to experiment SDK and trigger a fetch if device ID has changed.

  • setDeviceId() is called explicitly
  • By default the SDK stores device IDs in cookies, so a device ID changes if a user clears cookies, uses another device, or uses privacy mode
  • On initialization, a device ID is passed in from URL parameter deviceId
  • reset() is called

Custom device ID

You can assign a new device ID using setDeviceId(). When setting a custom device ID, make sure the value is sufficiently unique. Amplitude recommends using a UUID.

1amplitude.setDeviceId(uuid());

Get device ID

You can use the helper method getDeviceId() to get the value of the current deviceId.

1const deviceId = amplitude.getDeviceId();
Was this page helpful?

Thanks for your feedback!

September 17th, 2024

Need help? Contact Support

Visit Amplitude.com

Have a look at the Amplitude Blog

Learn more at Amplitude Academy

© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.