- Create a custom event in Optimizely Web Experimentation to track behaviors that cannot be measured in pageviews or clicks
- Add your custom event to an experiment
Custom tracking events allow you to capture and report on visitor actions or events that might not be related exclusively to clicks or page views. Using custom events lets you track an element that is not a pageview or a click.
When a custom event is triggered, it calls the Optimizely Web Experimentation snippet and passes along the ID of the event. The snippet takes that ID and uses it to generate an HTTP request that attributes the conversion event to your experiment results.
You can use Optimizely Web Experimentation's custom event tracking to capture revenue. For additional details on how to set this up, check out our article on revenue tracking.
This article explains how to set up a custom event.
Specific examples where custom events are helpful
-
Form submissions. For example, maybe there is a form on your page that does not send visitors to a unique confirmation page. You can track successful form submissions—rather than just clicks on the button—by tying a custom event to the logic on your page that qualifies the form submission and sends the user to the next step in the process. Custom events would also be useful for AJAX form submissions that do not generate a new page URL.
-
Tracking viewed pop-ups. If there is a pop-up that appears at specific instances in the flow, you can track views by triggering a custom event when the pop-up is shown.
-
Content is added to the page as the user moves through a flow, but the URL does not change. You can use the bind() function to attach event handlers to the documents element of the page or you can bind a custom event that listens for the presence of clicks on the element when it appears.
-
Tracking dropoff within forms. If you want to track dropoff within a form and learn which fields visitors tend to fill out before clicking away, you can do so by tying a custom event to actions taken in a specific input field.
-
Tracking visitor behavior on Single-Page App frameworks, such as AngularJS, BackboneJS or EmberJS.
Format to add a custom event for web experiments
The basic format for custom events in web experiments is shown below. Depending on the event you want to track, you can either add the code to your website or you can add it to your experiment's shared code. Custom events can also be called from the project JavaScript.
Our developer docs include an example of implementing a common custom event goal, allowing you to track scroll depth.
For mobile and Optimizely Feature Experimentation experiments, we have a dedicated section covering custom events in our developer docs.
Here is an example of the code needed to trigger a custom event goal on your page:
// ensures the optimizely object is defined globally using
window['optimizely'] = window['optimizely'] || [];
// sends a tracking call to Optimizely for the given event name.
window['optimizely'].push({
type: "event",
eventName: "eventName"
});
In this code snippet, the eventName
is tracked and associated with the appropriate visitor/experiment/variation. Choose a value for eventName
that correlates with the intent of your goal, for example “form_success” if you are looking to track form submits.
Write down the value you choose for eventName
, as you will need it later when you create the custom event in the Optimizely dashboard. This is covered in the Create a new custom event section below.
You can also include the call inside of a function that is called elsewhere.
var trackCustomEvent = function() {
window['optimizely'] = window['optimizely'] || [];window['optimizely'].push({
type: "event",
eventName: "eventName"
});
You can manually trigger the custom event to track the number of actions that complete a specific action using jQuery. You will need to enable jQuery in project settings or include it in your code. Check out this article to learn more about jQuery and the $ variable in Optimizely Web Experimentation.
window['optimizely'] = window['optimizely'] || [];
$("#ButtonID").bind("mousedown", function() {
window['optimizely'].push({
type: "event",
eventName: "eventName"
});
});
Have a question about custom events? Contact your Customer Success Manager (CSM).
Create a new custom event in Optimizely Web Experimentation
-
Navigate to Implementation > Events.
-
Click Create New Event.
-
Select Custom.
-
Below New Custom Event, enter a name for the event and an API name. Based on your entry in the API Name field, the code in the API Call field will populate the
eventName
. -
Add the API call to your site. For more information about implementing the API call, see our developer documentation.
-
Click Save Event.