Class: Amplitude

Amplitude

new Amplitude()

Amplitude SDK API - instance manager. Function calls directly on amplitude have been deprecated. Please call methods on the default shared instance: amplitude.getInstance() instead. See Readme for more information about this change.
Source:
Example
var amplitude = new Amplitude();

Members

__VERSION__

Get the current version of Amplitude's Javascript SDK.
Source:
Example
var amplitudeVersion = amplitude.__VERSION__;

Methods

clearUserProperties()

Clear all of the user properties for the current user. Note: clearing user properties is irreversible!
Deprecated:
  • Please use amplitude.getInstance().clearUserProperties();
Source:
Example
amplitude.clearUserProperties();

getSessionId() → {number}

Returns the id of the current session.
Deprecated:
  • Please use amplitude.getInstance().getSessionId();
Source:
Returns:
Id of the current session.
Type
number

identify(identify_obj, opt_callback)

Send an identify call containing user property operations to Amplitude servers. See Readme for more information on the Identify API and user property operations.
Parameters:
Name Type Description
identify_obj Identify the Identify object containing the user property operations to send.
opt_callback Amplitude~eventCallback (optional) callback function to run when the identify event has been sent. Note: the server response code and response body from the identify event upload are passed to the callback function.
Deprecated:
  • Please use amplitude.getInstance().identify(identify);
Source:
Example
var identify = new amplitude.Identify().set('colors', ['rose', 'gold']).add('karma', 1).setOnce('sign_up_date', '2016-03-31');
amplitude.identify(identify);

init(apiKey, opt_userId, opt_config, opt_callback)

Initializes the Amplitude Javascript SDK with your apiKey and any optional configurations. This is required before any other methods can be called.
Parameters:
Name Type Description
apiKey string The API key for your app.
opt_userId string (optional) An identifier for this user.
opt_config object (optional) Configuration options. See Readme for list of options and default values.
opt_callback function (optional) Provide a callback function to run after initialization is complete.
Deprecated:
  • Please use amplitude.getInstance().init(apiKey, opt_userId, opt_config, opt_callback);
Source:
Example
amplitude.init('API_KEY', 'USER_ID', {includeReferrer: true, includeUtm: true}, function() { alert('init complete'); });

isNewSession() → {boolean}

Returns true if a new session was created during initialization, otherwise false.
Deprecated:
  • Please use amplitude.getInstance().isNewSession();
Source:
Returns:
Whether a new session was created during initialization.
Type
boolean

logEvent(eventType, eventProperties, opt_callback)

Log an event with eventType and eventProperties
Parameters:
Name Type Description
eventType string name of event
eventProperties object (optional) an object with string keys and values for the event properties.
opt_callback Amplitude~eventCallback (optional) a callback function to run after the event is logged. Note: the server response code and response body from the event upload are passed to the callback function.
Deprecated:
  • Please use amplitude.getInstance().logEvent(eventType, eventProperties, opt_callback);
Source:
Example
amplitude.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15});

logEventWithGroups(eventType, eventProperties, groups, opt_callback)

Log an event with eventType, eventProperties, and groups. Use this to set event-level groups. Note: the group(s) set only apply for the specific event type being logged and does not persist on the user (unless you explicitly set it with setGroup). See the SDK Readme for more information about groups and Count by Distinct on the Amplitude platform.
Parameters:
Name Type Description
eventType string name of event
eventProperties object (optional) an object with string keys and values for the event properties.
groups object (optional) an object with string groupType: groupName values for the event being logged. groupName can be a string or an array of strings.
opt_callback Amplitude~eventCallback (optional) a callback function to run after the event is logged. Note: the server response code and response body from the event upload are passed to the callback function. Deprecated Please use amplitude.getInstance().logEventWithGroups(eventType, eventProperties, groups, opt_callback);
Source:
Example
amplitude.logEventWithGroups('Clicked Button', null, {'orgId': 24});

logRevenue(price, quantity, product)

Log revenue event with a price, quantity, and product identifier. DEPRECATED - use logRevenueV2
Parameters:
Name Type Description
price number price of revenue event
quantity number (optional) quantity of products in revenue event. If no quantity specified default to 1.
product string (optional) product identifier
Deprecated:
  • Please use amplitude.getInstance().logRevenueV2(revenue_obj);
Source:
Example
amplitude.logRevenue(3.99, 1, 'product_1234');

logRevenueV2(revenue_obj)

Log revenue with Revenue interface. The new revenue interface allows for more revenue fields like revenueType and event properties. See Readme for more information on the Revenue interface and logging revenue.
Parameters:
Name Type Description
revenue_obj Revenue the revenue object containing the revenue data being logged.
Deprecated:
  • Please use amplitude.getInstance().logRevenueV2(revenue_obj);
Source:
Example
var revenue = new amplitude.Revenue().setProductId('productIdentifier').setPrice(10.99);
amplitude.logRevenueV2(revenue);

regenerateDeviceId()

Regenerates a new random deviceId for current user. Note: this is not recommended unless you know what you are doing. This can be used in conjunction with `setUserId(null)` to anonymize users after they log out. With a null userId and a completely new deviceId, the current user would appear as a brand new user in dashboard. This uses src/uuid.js to regenerate the deviceId.
Deprecated:
  • Please use amplitude.getInstance().regenerateDeviceId();
Source:

setDeviceId(deviceId)

Sets a custom deviceId for current user. Note: this is not recommended unless you know what you are doing (like if you have your own system for managing deviceIds). Make sure the deviceId you set is sufficiently unique (we recommend something like a UUID - see src/uuid.js for an example of how to generate) to prevent conflicts with other devices in our system.
Parameters:
Name Type Description
deviceId string custom deviceId for current user.
Deprecated:
  • Please use amplitude.getInstance().setDeviceId(deviceId);
Source:
Example
amplitude.setDeviceId('45f0954f-eb79-4463-ac8a-233a6f45a8f0');

setDomain(domain)

Sets a customer domain for the amplitude cookie. Useful if you want to support cross-subdomain tracking.
Parameters:
Name Type Description
domain string to set.
Deprecated:
  • Please use amplitude.getInstance().setDomain(domain);
Source:
Example
amplitude.setDomain('.amplitude.com');

setGlobalUserProperties()

Set global user properties. Note this is deprecated, and we recommend using setUserProperties
Deprecated:
  • Yes
Source:

setGroup(groupType, groupName)

Add user to a group or groups. You need to specify a groupType and groupName(s). For example you can group people by their organization. In that case groupType is "orgId" and groupName would be the actual ID(s). groupName can be a string or an array of strings to indicate a user in multiple gruups. You can also call setGroup multiple times with different groupTypes to track multiple types of groups (up to 5 per app). Note: this will also set groupType: groupName as a user property. See the SDK Readme for more information.
Parameters:
Name Type Description
groupType string the group type (ex: orgId)
groupName string | list the name of the group (ex: 15), or a list of names of the groups
Deprecated:
  • Please use amplitude.getInstance().setGroup(groupType, groupName);
Source:
Example
amplitude.setGroup('orgId', 15); // this adds the current user to orgId 15.

setOptOut(enable)

Sets whether to opt current user out of tracking.
Parameters:
Name Type Description
enable boolean if true then no events will be logged or sent.
Deprecated:
  • Please use amplitude.getInstance().setOptOut(enable);
Source:

setUserId(userId)

Sets an identifier for the current user.
Parameters:
Name Type Description
userId string identifier to set. Can be null.
Deprecated:
  • Please use amplitude.getInstance().setUserId(userId);
Source:
Example
amplitude.setUserId('joe@gmail.com');

setUserProperties(userProperties)

Sets user properties for the current user.
Parameters:
Name Type Description
userProperties object object with string keys and values for the user properties to set.
boolean DEPRECATED opt_replace: in earlier versions of the JS SDK the user properties object was kept in memory and replace = true would replace the object in memory. Now the properties are no longer stored in memory, so replace is deprecated.
Deprecated:
  • Please use amplitude.getInstance.setUserProperties(userProperties);
Source:
Example
amplitude.setUserProperties({'gender': 'female', 'sign_up_complete': true})

setVersionName(versionName)

Set a versionName for your application.
Parameters:
Name Type Description
versionName string The version to set for your application.
Deprecated:
  • Please use amplitude.getInstance().setVersionName(versionName);
Source:
Example
amplitude.setVersionName('1.12.3');

Type Definitions

eventCallback(responseCode, responseBody)

This is the callback for logEvent and identify calls. It gets called after the event/identify is uploaded, and the server response code and response body from the upload request are passed to the callback function.
Parameters:
Name Type Description
responseCode number Server response code for the event / identify upload request.
responseBody string Server response body for the event / identify upload request.
Source:

eventCallback(responseCode, responseBody)

This is the callback for logEvent and identify calls. It gets called after the event/identify is uploaded, and the server response code and response body from the upload request are passed to the callback function.
Parameters:
Name Type Description
responseCode number Server response code for the event / identify upload request.
responseBody string Server response body for the event / identify upload request.
Source: