Custom Metrics and Events
Why custom events matter
Section titled “Why custom events matter”Built-in metrics like clicks and pageviews cover common scenarios. But most businesses measure success through actions unique to their product — adding items to a cart, completing an onboarding step, generating a lead, or reaching a paywall. Custom events let you track any action and use it as an experiment metric.
How custom events work
Section titled “How custom events work”The flow has three parts:
- Create the event in the Optimizely UI — this defines the event key and makes it available as a metric
- Track the event in your code — fire the event when the user performs the action
- Attach the event as a metric to an experiment — Optimizely measures the event rate across variations
Events are identified by a unique event key (e.g., add_to_cart, lead_submitted). The key must match exactly between the UI configuration and your tracking code.
Create a custom event
Section titled “Create a custom event”In Web Experimentation
Section titled “In Web Experimentation”- Navigate to Events in the left sidebar
- Click Create New Event
- Choose the event type:
- Click event — Tracks clicks on a CSS selector (no code needed)
- Custom event — Tracks a programmatically fired event (requires code)
- Pageview event — Tracks visits to a URL pattern
- For custom events, enter the event key (e.g.,
checkout_started) - Add a description for team reference
- Click Save
In Feature Experimentation
Section titled “In Feature Experimentation”- Navigate to Events in the left sidebar
- Click Create New Event
- Enter the event key
- Add a description
- Click Save
The event is now available to attach to any experiment in the project.
Track events from your application
Section titled “Track events from your application”Web Experimentation (browser-side)
Section titled “Web Experimentation (browser-side)”Fire events using the Optimizely snippet API. This call sends the event to Optimizely’s event processor.
// Basic event
window.optimizely = window.optimizely || [];
window.optimizely.push({
type: 'event',
eventName: 'checkout_started',
});
// Event with revenue (in cents)
window.optimizely.push({
type: 'event',
eventName: 'purchase_completed',
tags: {
revenue: 7999, // $79.99 in cents
value: 79.99,
},
});
// Event with custom tags
window.optimizely.push({
type: 'event',
eventName: 'product_viewed',
tags: {
category: 'electronics',
product_id: 'SKU-12345',
},
}); Feature Experimentation (SDK-side)
Section titled “Feature Experimentation (SDK-side)”Use the SDK’s trackEvent method. The event is sent along with the user context for proper attribution.
import { createInstance } from '@optimizely/optimizely-sdk';
const optimizely = createInstance({ sdkKey: 'YOUR_SDK_KEY' });
await optimizely.onReady();
const user = optimizely.createUserContext('user-456', {
plan: 'premium',
});
// Basic event
user.trackEvent('checkout_started');
// Event with revenue
user.trackEvent('purchase_completed', {
revenue: 7999,
value: 79.99,
}); from optimizely import optimizely
client = optimizely.Optimizely(sdk_key='YOUR_SDK_KEY')
user = client.create_user_context('user-456', {
'plan': 'premium',
})
# Basic event
user.track_event('checkout_started')
# Event with revenue
user.track_event('purchase_completed', event_tags={
'revenue': 7999,
'value': 79.99,
}) using OptimizelySDK;
var optimizely = OptimizelyFactory.NewDefaultInstance("YOUR_SDK_KEY");
var user = optimizely.CreateUserContext("user-456", new UserAttributes
{
{ "plan", "premium" },
});
// Basic event
user.TrackEvent("checkout_started");
// Event with revenue
user.TrackEvent("purchase_completed", new EventTags
{
{ "revenue", 7999 },
{ "value", 79.99 },
}); Attach events as experiment metrics
Section titled “Attach events as experiment metrics”- Open your experiment and navigate to the Metrics tab
- Click Add Metric
- Select the custom event from the event picker
- Configure the metric settings:
- Direction — Increase (higher is better) or Decrease (lower is better)
- Aggregation — Unique conversions, total conversions, or revenue sum
- Role — Primary or secondary metric
- Click Save
Event tags and revenue tracking
Section titled “Event tags and revenue tracking”Event tags carry metadata alongside the event. Two tags have special meaning:
| Tag | Purpose | Format |
|---|---|---|
revenue | Monetary value for revenue metrics | Integer in cents (e.g., 4999 for $49.99) |
value | Numeric value for non-revenue numeric metrics | Float (e.g., 49.99) |
All other tags are stored as metadata and available in Data Lab for segmentation but do not affect metric calculations.
Common mistake: Sending revenue in dollars instead of cents. If your revenue metric shows values 100x too large, check the unit.
Manage events across projects
Section titled “Manage events across projects”Events are scoped to a project. If you run experiments across multiple projects, create the event in each project with the same event key.
Naming conventions
Section titled “Naming conventions”Consistent naming makes events easier to find and reduces duplicates.
| Pattern | Example |
|---|---|
{action}_{object} | click_cta, view_pricing, submit_form |
{page}_{action} | checkout_started, onboarding_completed |
{funnel_stage}_{action} | awareness_signup, activation_first_use |
Avoid generic names like event1 or click. They become indistinguishable as your event library grows.
Track events via tag manager
Section titled “Track events via tag manager”If you use Google Tag Manager or a similar tool, fire events from tags instead of application code.
- Create a GTM tag of type Custom HTML
- Add the event tracking code (see Web Experimentation example above)
- Set the trigger to the user action you want to track (e.g., form submission, button click)
- Publish the tag
This approach works for Web Experimentation. Feature Experimentation requires SDK-level tracking.
Verify event tracking
Section titled “Verify event tracking”Browser console (Web Experimentation)
Section titled “Browser console (Web Experimentation)”- Open developer tools on a page with the Optimizely snippet
- Trigger the action that fires your event
- Check the Network tab for a request to
logx.optimizely.com - The request payload includes the event key and any tags
SDK logs (Feature Experimentation)
Section titled “SDK logs (Feature Experimentation)”Enable debug logging in the SDK to see event dispatch messages.
const optimizely = createInstance({ sdkKey: 'YOUR_SDK_KEY', logLevel: 'DEBUG',});Look for log entries containing Dispatching event followed by your event key.
Troubleshooting
Section titled “Troubleshooting”| Issue | Cause | Fix |
|---|---|---|
| Metric shows zero conversions | Event key mismatch between UI and code | Verify the exact event key string in both places |
| Revenue metric shows wrong values | Revenue sent in dollars instead of cents | Multiply by 100 before sending |
| Events fire but do not appear in results | Event not attached as a metric on the experiment | Add the event as a metric in the experiment configuration |
| Duplicate events counted | Event fires multiple times per user action | Add deduplication logic or use “unique conversions” aggregation |
| Events from tag manager not tracked | Optimizely snippet loads after the tag fires | Ensure the snippet loads before your tag manager fires event tags |