The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Track Event for the JavaScript (Browser) SDK

Describes the track event method for the JavaScript (Browser) SDK, which tracks a conversion event in Optimizely Feature Experimentation

Tracks a conversion event (in other words, an action a user takes) for a user. Logs an error message if the specified event key does not match any existing events.

Version

SDK 4.5 and higher

Description

Use this method to track events across multiple flag rules for a user. You should make only one track call per conversion (user action), even if many rules measure the conversion. You call the Track Event method on a user. For information about the user object, see OptimizelyUserContext.

The attributes passed to Track Event are only used for results segmentation.

Parameters

This table lists the required and optional parameters for the Track Event method.

ParameterTypeDescription
eventKey
required
StringThe key of the event to be tracked. This key must match the event key provided when the event was created in the Optimizely app.
eventTags
optional
MapA map of key-value pairs specifying tag names and their corresponding tag values for this particular event occurrence. Values can be strings, numbers, or booleans.

These can be used to track numeric metrics, allowing you to track actions beyond conversions, for example: revenue, load time, or total value. See details on reserved tag keys.

Returns

This method sends conversion data to Optimizely Feature Experimentation. It does not provide return values.

Examples

👍

Beta

Event properties is currently in beta. Contact your Customer Success Manager or sign up for the beta on Optimizely.com.

const attributes = { logged_in: true };
const user = optimizely.createUserContext('user123', attributes);

const tags = {
  category: 'shoes',
  purchase_count: 2
};

user.trackEvent("my_purchase_event_key", tags);
const attributes = { logged_in: true }; 
const user = optimizely.createUserContext('user123', attributes); 
const properties = { 
  category: 'shoes', 
  color: 'red' 
}; 
const tags = { 
  $opt_event_properties: properties 
}; 
user.trackEvent("my_purchase_event_key", tags); 

Side effects

The table lists other Optimizely Feature Experimentation functionality that may be triggered by using this method.

FunctionalityDescription
ConversionsCalling this method records a conversion and attributes it to the variations that the user has seen.

You can create metrics on this conversion event and add metrics to experiments even after the conversion has been tracked.

For more information, see the support documentation on Events: Tracking clicks, pageviews, and other visitor actions.

Important!

- This method will not track events when the specified event key is invalid.
- Changing the traffic allocation of running experiments affects how conversions are recorded and variations are attributed to users.
Notification listenersAccessing this method triggers a call to the TRACK notification listener.

Important!

This method will not call the TRACK notification listener when the specified event key is invalid.

Source files

The language and platform source files containing the implementation for JavaScript is available on GitHub.