# Klaviyo

URL: https://docs.ablyft.com/developers/integrations/klaviyo/

> Send the experiments and variations a visitor saw to Klaviyo as an event on the visitor's profile.



With a Custom Integration, ABlyft can send an `Experiment Viewed` event to Klaviyo whenever a visitor sees an
experiment. You can then use the variation in Klaviyo charts and segments.

## What you need

* The Klaviyo onsite JavaScript is installed on the pages where your experiments run.
* The ABlyft snippet is installed on the same pages.
* Access to **Project → Integrations → Custom Integrations** in ABlyft.

## Create the integration in ABlyft

1. Open **Project → Integrations → Custom Integrations** and create a new integration, for example `Klaviyo`.
2. Under **Trigger Settings**, choose **Experiment viewed** (default) or **User bucketed**. See
   [Custom Integrations](https://docs.ablyft.com/developers/integrations/custom-integrations/) for the difference.
3. Paste the code below into **Integration Code** and save.

## Integration code

```js
if (!window.klaviyo) {
  throw new Error('Klaviyo is not loaded yet');
}

window.klaviyo.track('Experiment Viewed', {
  experiment_id: experimentId,
  experiment_name: experimentName,
  variation_id: variationId,
  variation_name: variationName
});
```

ABlyft retries the code every 100 ms, up to 20 times, until it runs without an error. The throw on the first line makes
it wait for Klaviyo. The code uses the direct call `klaviyo.track(...)`, not `klaviyo.push`.

## Where you see the data in Klaviyo

The event `Experiment Viewed` is a custom metric in Klaviyo. Its properties `experiment_name` and `variation_name` can
be used to:

* find the event in a profile's activity feed,
* build charts of the metric, split by property,
* create segments, for example profiles that viewed a certain variation.

Events belong to a profile. A visitor without a known email address or phone number is not identified, so see the note
below.

> **Check before use:** Klaviyo's JavaScript API documentation describes `klaviyo.track` for identified users and `klaviyo.identify` with an
> `email` or `phone_number`. It does not describe in detail what happens to events of anonymous visitors, nor how
> exactly the metric and its properties appear in the Klaviyo interface. If a visitor is anonymous, the event may not
> be attached to a profile until the visitor is identified. Check the Klaviyo settings for backfilling anonymous
> activity and test with a known profile. Do not send an email address from the integration code unless your site
> already has it and your consent setup allows it.

## Check the setup

1. Turn on **Run in preview mode** for the integration, so it also runs in preview.
2. Open a page with a running experiment in preview mode, as a visitor whose profile Klaviyo knows. Open the browser
   console. It reports an error if the code still fails after 20 attempts.
3. In Klaviyo, open the profile and check its activity feed for `Experiment Viewed`.

## Notes

> **Consent and limits:** Custom integrations do not run when tracking is disabled for the visitor (opt-out or Do Not Track), except in preview
> mode. Klaviyo may need its own consent category in your consent tool. With **Experiment viewed**, the event is sent
> on every view, which increases your Klaviyo event volume.

[Klaviyo JavaScript API documentation](https://developers.klaviyo.com/en/docs/javascript_api)

