HomeGuidesAPI Reference
ChangelogHelp CenterCommunityContact Us
These docs are for v2022-10-17. Click to read the latest docs for v2024-02-15.

Custom event tracking with JavaScript

Define any activity and track it within Klaviyo. Use events to trigger messages through flows or for custom reporting.

❗️

This guide refers to Klaviyo's v1/v2 legacy APIs

We are in the process of updating these guides, please check back later! For more information about the differences between legacy and new APIs, check out the comparison chart and [new API overview] (ref:api_overview).

Events, also referred to as metrics, record actions that customers take on your website, in response to an email, or through any other data source where customer behavior takes place. Using the _learnq object that's automatically added by the Klaviyo onsite tracking snippet, the track method can be leveraged to capture all kinds of custom events.

Klaviyo's event tracking and analytics are very flexible; customize events to keep track of what's important to your business, whether you're building a custom integration or looking to track additional events within a native Klaviyo integration.

Track an event

If you already have our onsite javascript, known as Klaviyo.js, _learnq should be available to you. You will likely have installed Klaviyo.js during your account setup, however, if you need to install it, check out our custom integration guide.

📘

When you add the Klaviyo.js snippet to your site, we are only able to track the browsing activity of "known browsers" - browsers that have visited and engaged at least once before. Klaviyo will not track anonymous browsers. Read more about Klaviyo onsite tracking.

The track method accepts the custom event name as a string, and can be given any name you require:
_learnq.push(['track', 'Custom Event Name'])

It also accepts an optional dictionary or hash of properties associated with that event. For example, if you wanted to include properties associated with viewed items:

<script type="text/javascript">
   var _learnq = _learnq || [];
   var item = {
     "ProductName": "Winnie the Pooh",
     "ProductID": "1111",
     "SKU": "WINNIEPOOH",
     "Categories": ["Fiction", "Children"],
     "ImageURL": "http://www.example.com/path/to/product/image.png",
     "URL": "http://www.example.com/path/to/product",
     "Brand": "Kids Books",
     "Price": 9.99,
     "CompareAtPrice": 14.99
   };
   _learnq.push(["track", "Viewed Product", item]);
</script>

The track method accepts a variety data types:

  • strings
  • numbers
  • booleans
  • dates

📘

This code uses the legacy _learnq object

We recommend using the new Klaviyo object, which offers robust support for callbacks and promises. Check out this introduction to the Klaviyo object to get started!

You can also track events when a customer clicks a custom button you have defined. In this example, an event listener tracks when a customer clicks a "Like" button on an item, creating an Added Like custom event. We can then set up a custom flow trigger to send emails to customers about items they've "liked" or view reports on "liked" items.

<script type="text/javascript">
	var _learnq = _learnq || [];
	document.getElementById("Like").addEventListener('click',function (){
 		_learnq.push(['track', 'Added Like', item]);
	});
</script>

Note that if you wish to send custom events to a customer's profile, that customer must be cookied. Ensure that profiles are tracked prior to capturing custom event data.

Using custom events in flows

Once your event tracking is set up, you can create a new flow and select your custom metric as the trigger. The Flows Library also has a variety of prebuilt flows to choose from which can be modified to suit the purpose of your custom event. For example, if you'd like to remind customers of items they've previously "liked" you can modify a standard winback flow with your Added Like metric.

Read more about creating a metric-triggered flow. The metric trigger option allows you to queue people for a flow when they take a certain action. This action can be any event activity captured through an integration (e.g., started a checkout, placed an order, filled out a form) or events created via the Klaviyo API. For example, an abandoned cart flow would trigger off the Started Checkout event, with an additional flow filter to restrict the flow only to those who have not followed through with placing an order. Although this guide covers metrics standard to Klaviyo's native integrations, any custom metric can be used to trigger a flow.

Report on custom events

Klaviyo’s pre-built, customizable reports enable you to dive deeper into metrics, campaign, flow, or product performance. To track the performance of your custom events, set up a custom report on those metrics.

To access custom reports, click on the Analytics tab and select Custom Reports from the main dashboard. Create a new report by selecting Create from scratch.

Next, click on Select report type and choose Single Metric Deep Dive Report from the dropdown menu. With the single metric report, you can choose any metric in your account to build a report around and can further customize it by selecting different timeframes, properties, and groupings.

Select your custom metric to report on. From the designated dropdown, you can select any metric available in your account. Finally, customize and run your report.

Read more about creating a single metric deep dive report.