The Browser SDK lets you send events to Amplitude.
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.
// Option 1, initialize with API_KEY only
amplitude.init(API_KEY);
// Option 2, initialize with user ID if it's already known
amplitude.init(API_KEY, 'user@amplitude.com');
// Option 3, initialize with configuration
amplitude.init(API_KEY, 'user@amplitude.com', options);
Configuration options
Name
Description
Default Value
instanceNamestring. The instance name.$default_instance
flushIntervalMillisnumber. Sets the interval of uploading events to Amplitude in milliseconds.1,000 (1 second)
flushQueueSizenumber. Sets the maximum number of events batched in a single upload attempt.30 events
flushMaxRetriesnumber. Sets the maximum number of retries for failed upload attempts. This is only applicable to errors that can be retried.5 times.
logLevelLogLevel.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
minIdLengthnumber. Sets the minimum length for the value of userId and deviceId properties.5
optOutboolean. Sets permission to track events. Setting a value of true prevents Amplitude from tracking and uploading events.false
serverUrlstring. Sets the URL where events are upload to.https://api2.amplitude.com/2/httpapi
serverZoneEU or US. Sets the Amplitude server zone. Set this to EU for Amplitude projects created in EU data center.US
useBatchboolean. Sets whether to upload events to Batch API instead of the default HTTP V2 API or not.false
appVersionstring. Sets an app version for events tracked. This can be the version of your application. For example: "1.0.0"undefined
deviceIdstring. Sets an identifier for the device running your application.UUID()
cookieExpirationnumber. Sets expiration of cookies created in days.365 days
cookieSameSitestring. Sets SameSite property of cookies created.Lax
cookieSecureboolean. Sets Secure property of cookies created.false
cookieStorageStorage<UserSession>. Sets a custom implementation of Storage<UserSession> to persist user identity.MemoryStorage<UserSession>
cookieUpgradeboolean. 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
disableCookiesboolean. Sets permission to use cookies. If value is true, localStorage API is used to persist user identity.The cookies is enable by default.
domainstring. Sets the domain property of cookies created.undefined
partnerIdstring. Sets partner ID. Amplitude requires the customer who built an event ingestion integration to add the partner identifier to partner_id.undefined
sessionTimeoutnumber. Sets the period of inactivity from the last tracked event before a session expires in milliseconds.1,800,000 milliseconds (30 minutes)
userIdnumber. Sets an identifier for the tracked user. Must have a minimum length of 5 characters unless overridden with the minIdLength option.undefined
trackingOptionsTrackingOptions. 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.disabledboolean. Whether disable the attribution tracking.false
config.attribution.excludeReferrersstring[]. Exclude the attribution tracking for the provided referrers stringIncluding all referrers by default.
config.attribution.initialEmptyValuestring. Customize the initial empty value for attribution related user properties to any string value.EMPTY
config.attribution.resetSessionOnNewCampaignboolean. Whether to reset user sessions when a new campaign is detected. Note a newfalse
config.attribution.trackNewCampaignsboolean. Whether tracking new campaigns on the current session.false
config.attribution.trackPageViewsboolean. Whether track page view on attribution. Note that config.defaultTracking.pageViews has higher priority over this configuration. Learn more about it here.false
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.
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
// Events queued in memory will flush when number of events exceed upload threshold
// Default value is 30
flushQueueSize: 50,
// Events queue will flush every certain milliseconds based on setting
// Default value is 10000 milliseconds
flushIntervalMillis: 20000,
// Using batch mode with batch API endpoint, `https://api2.amplitude.com/batch`
useBatch: true
});
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.
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
serverZone: 'EU',
});
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.
amplitude.init(AMPLITUDE_API_KEY, OPTIONAL_USER_ID, {
logLevel: amplitude.Types.LogLevel.Warn,
});
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.
amplitude.init(AMPLITUDE_API_KEY, OPTIONAL_USER_ID, {
loggerProvider: new MyLogger(),
});
Enable the debug mode by setting the logLevel to "Debug", for example:
amplitude.init(AMPLITUDE_API_KEY, OPTIONAL_USER_ID, {
logLevel: amplitude.Types.LogLevel.Debug,
});
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.Events represent how users interact with your application. For example, "Button Clicked" might be an action you want to track.
// Track a basic event
amplitude.track('Button Clicked');
// Track events with optional properties
const eventProperties = {
buttonColor: 'primary',
};
amplitude.track('Button Clicked', eventProperties);
You can also pass a BaseEvent object to track. For more information, see the BaseEvent interface for all available fields.
const event_properties = {
buttonColor: 'primary',
};
const event = {
event_type: "Button Clicked",
event_properties,
groups: { 'role': 'engineering' },
group_properties: { 'groupPropertyKey': 'groupPropertyValue' }
};
amplitude.track(event);
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.
const defaultInstance = amplitude.createInstance();
defaultInstance.init(API_KEY_DEFAULT);
const envInstance = amplitude.createInstance();
envInstance.init(API_KEY_ENV, {
instanceName: 'env',
});
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:
| 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 URLSee 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 DestinationSee 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 URLSee 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.
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
defaultTracking: {
pageViews: true,
sessions: true,
formInteractions: true,
fileDownloads: true,
},
});
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.
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
defaultTracking: true,
});
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.
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
defaultTracking: {
pageViews: true,
},
});
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.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.
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
defaultTracking: {
pageViews: {
trackOn: () => {
return window.location.pathname.includes('home');
},
},
},
});
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.
Set config.defaultTracking.sessions to true to enable Amplitude to track sessions.
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
defaultTracking: {
sessions: true,
},
});
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.
Set config.defaultTracking.formInteractions to true to enable Amplitude to track form interactions.
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
defaultTracking: {
formInteractions: true,
},
});
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:
<form id="subscriber-form" name="subscriber-form" action="/subscribe">
<input type="text" />
<input type="submit" />
</form>
Set config.defaultTracking.fileDownloads to true to enable Amplitude to track file downloads.
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
defaultTracking: {
fileDownloads: true,
},
});
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 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.
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.
const identifyEvent = new amplitude.Identify();
amplitude.identify(identifyEvent);
This method sets the value of a user property. For example, you can set a role property of a user.
const identifyEvent = new amplitude.Identify();
identifyEvent.set('location', 'LAX');
amplitude.identify(identifyEvent);
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.
const identifyEvent = new amplitude.Identify();
identifyEvent.setOnce('initial-location', 'SFO');
identify(identifyEvent);
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.
const identifyEvent = new amplitude.Identify();
identifyEvent.add('travel-count', 1);
amplitude.identify(identifyEvent);
You can use arrays as user properties. Directly set arrays or use prepend, append, preInsert and postInsert to generate an array.
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.
const identifyEvent = new Identify();
identifyEvent.prepend('visited-locations', 'LAX');
identify(identifyEvent);
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.
const identifyEvent = new amplitude.Identify();
identifyEvent.append('visited-locations', 'SFO');
amplitude.identify(identifyEvent);
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.
const identifyEvent = new amplitude.Identify();
identifyEvent.preInsert('unique-locations', 'LAX');
identify(identifyEvent);
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..
const identifyEvent = new amplitude.Identify();
identifyEvent.postInsert('unique-locations', 'SFO');
amplitude.identify(identifyEvent);
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.
const identifyEvent = new amplitude.Identify();
identifyEvent.remove('unique-locations', 'JFK')
amplitude.identify(identifyEvent);
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.
groupName would be '15'.
// set group with a single group name
amplitude.setGroup('orgId', '15');
If Joe is in 'sport' 'soccer' and 'tennis', then the groupName would be '["tennis", "soccer"]'.
// set group with multiple group names
amplitude.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.
amplitude.track({
event_type: 'event type',
event_properties: { eventPropertyKey: 'event property value' },
groups: { 'orgId': '15' }
})
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.
const event = new amplitude.Revenue()
.setProductId('com.company.productId')
.setPrice(3.99)
.setQuantity(3);
amplitude.revenue(event);
| 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 |
The flush method triggers the client to send buffered events immediately.
amplitude.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:
amplitude.init(API_KEY).promise.then(function() {
amplitude.track('Button Clicked');
amplitude.flush();
});
If your app has its login system that you want to track users with, you can call setUserId at any time.
amplitude.setUserId('user@amplitude.com');
You can also assign the User ID as an argument to the init call.
amplitude.init(API_KEY, 'user@amplitude.com');
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).
amplitude.setSessionId(Date.now());
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.
amplitude.setDeviceId(uuid());
reset is a shortcut to anonymize users after they log out, by:
userId to undefineddeviceId to a new UUID valueWith an undefined userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
amplitude.reset();
You can turn off logging for a given user by setting setOptOut to true.
amplitude.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.
amplitude.setOptOut(false);
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 |
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
trackingOptions: {
deviceManufacturer: false,
deviceModel: false,
ipAddress: false,
language: false,
osName: false,
osVersion: false,
platform: false,
},
});
All asynchronous APIs are optionally awaitable through a Promise interface. This also serves as a callback interface.
amplitude.init("apikey", "12321.com").promise.then(function() {
// init callback
})
amplitude.track('Button Clicked').promise.then(function(result) {
result.event; // {...} (The final event object sent to Amplitude)
result.code; // 200 (The HTTP response status code of the request.
result.message; // "Event tracked successfully" (The response message)
});
// Using async/await
const initResult = await amplitude.init("apikey", "12321.com").promise;
const results = await amplitude.track('Button Clicked').promise;
result.event; // {...} (The final event object sent to Amplitude)
result.code; // 200 (The HTTP response status code of the request.
result.message; // "Event tracked successfully" (The response message)
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().
addThe add method adds a plugin to Amplitude. Plugins can help processing and sending events.
amplitude.add(new Plugin());
removeThe remove method removes the given plugin name from the client instance if it exists.
amplitude.remove(plugin.name);
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().
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.
Here's an example of a plugin that sends each event that's instrumented to a target server URL using your preferred HTTP client.
function myDestinationPlugin (serverUrl) {
const name = 'my-destination-plugin';
const type = amplitude.Types.PluginType.DESTINATION;
let amplitudeConfig;
/**
* setup() is called on plugin installation
* example: amplitude.add(new myDestinationPlugin());
*/
const setup = function (config) {
amplitudeConfig = config;
}
/**
* execute() is called on each event instrumented
* example: amplitude.track('New Event');
*/
const execute = function (event) {
const payload = {
key: 'secret',
data: event,
};
return fetch(this.serverUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: '*/*',
},
body: JSON.stringify(payload),
}).then(function(response) {
return {
code: response.status,
event: event,
message: response.statusText,
};
});
}
return {
name,
type,
setup,
execute,
},
}
amplitude.init(API_KEY);
amplitude.add(myDestinationPlugin('https://custom.domain.com'));
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.
const addEventIdPlugin = () => {
const name = 'add-event-id';
const type = amplitude.Types.PluginType.ENRICHMENT;
let currentId = 100;
let amplitudeConfig;
/**
* setup() is called on plugin installation
* example: amplitude.add(new AddEventIdPlugin());
*/
const setup = function (config) {
amplitudeConfig = config;
}
/**
* execute() is called on each event instrumented
* example: client.track('New Event');
*/
const execute = function (event: Event) {
event.event_id = currentId++;
return event;
}
return {
name,
type,
setup,
execute,
}
}
amplitude.init(API_KEY);
amplitude.add(addEventIdPlugin());
Download the plugin-web-attribution-browser package and add the webAttributionPlugin before you call the init method.
npm install @amplitude/plugin-web-attribution-browser
yarn add @amplitude/plugin-web-attribution-browser
Add the plugin to the Amplitude instance.
amplitude.add(webAttributionPlugin());
amplitude.init(API_KEY);
See the configuration options.
Learn more about what the Web Attribution Plugin supports.
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.
Download the plugin-page-view-tracking-browser and add pageViewTrackingPlugin before calling the init method.
npm install @amplitude/plugin-page-view-tracking-browser
yarn add @amplitude/plugin-page-view-tracking-browser
Add plugin to the Amplitude instance.
amplitude.add(pageViewTrackingPlugin());
amplitude.init(API_KEY);
See the configuration options.
Learn more about what the Page View Plugin supports.
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.
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.
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."
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.
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.
The following are common issues specific to Browser SDK. For more general common issues, see SDK Troubleshooting and Debugging.
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.
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.
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.
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:
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.
To make track() synchronous, add the await keyword before the call.
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:
www.example.comwww.example.orgUsers 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.
getDeviceId().www.example.com?deviceId=device_id_from_site_1)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.
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.
class MyTransport {
send(serverUrl, payload) {
// check example: https://github.com/amplitude/Amplitude-TypeScript/blob/main/packages/analytics-client-common/src/transports/fetch.ts
}
}
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
transportProvider: new MyTransport(),
});
Unlike standard network requests, sendBeacon sends events in the background, even if the user closes the browser or leaves the page.
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.To send an event using sendBeacon, set the transport SDK option to 'beacon' in one of two ways
amplitude.init(API_KEY, 'user@amplitude.com',
{
transport: TransportType.SendBeacon,
// To make sure the event will be scheduled right away.
flushIntervalMillis: 0,
flushQueueSize: 1,
}
);
Amplitude recommends adding your own event listener for pagehide event.
window.addEventListener('pagehide',
() => {
amplitude.setTransport('beacon')
// Sets https transport to use `sendBeacon` API
amplitude.flush()
},
);
If your web app configures the strict Content Security Policy (CSP) for security concerns, adjust the policy to whitelist the Amplitude domains:
https://*.amplitude.com to script-src.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 prefix and the first ten digits of the API key: AMP_{first_ten_digits_API_KEY}.AMP_MKTG and the first ten digits of the API key: AMP_MKTG_{first_ten_digits_API_KEY}.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_TLDTEST 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_TLDTEST 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
optOutRequired. A flag to opt this device out of Amplitude tracking. If this flag is set, no extra information will be stored for the user
userIdUpon 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
deviceIdA 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
sessionIdA randomly generated string for each session
lastEventTimeTime of the last event. Used to decide when to expire and create a new session Id
lastEventIdId of the last event
Marketing campaign cookies
Name
Description
utm_campaignThis identifies a specific campaign used (for example, "summer_sale")
utm_contentThis identifies what brought the user to the site and is commonly used for A/B testing (for example, "banner-link", "text-link")
utm_idAn optional parameter for tracking unique IDs or transaction IDs associated with the link.
utm_mediumThis identifies a specific campaign used (for example, "summer_sale")
utm_sourceThis identifies which website sent the traffic (for example, Google, Facebook)
utm_termThis identifies paid search terms used (for example, product+analytics)
referrerThe last page the user was on (for example,
https://amplitude.com/behavioral-analytics-platform?ref=nav)
referring_domainThe domain that the user was last on (for example,
https://amplitude.com)
dclidGoogle campaign manager Click Identifier
gbraidGoogle Click Identifier for iOS device from Web to App
gclidGoogle Click Identifier from URL parameters
fbclidFacebook Click Identifier from URL parameters
ko_click_idKochava Click Identifier from URL parameters
msclkidMicrosoft Click Identifier
ttclidTikTok Click Identifier
twclidTwitter Click Identifier from URL parameter
wbraidGoogle Click Identifier for iOS device from App to Web
li_fat_idLinkedIn member indirect identifier for Members for conversion tracking, retargeting, analytics
rdt_cidReddit Click Identifier
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).
The SDK initializes the device ID in the following order, with the device ID being set to the first valid value encountered:
http://example.com/?deviceId=123456789. See cross domain tracking for more detailsA device ID changes in many scenarios:
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 explicitlydeviceIdreset() is calledYou 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.
amplitude.setDeviceId(uuid());
You can use the helper method getDeviceId() to get the value of the current deviceId.
const deviceId = amplitude.getDeviceId();
September 17th, 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.