> For the complete documentation index, see [llms.txt](https://docs.elevateab.com/elevate-helpcenter/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.elevateab.com/elevate-helpcenter/experiment-setup/custom-events/custom-javascript-events.md).

# Custom JavaScript Events

Track any Shopify storefront action in Elevate A/B Testing with a Custom JavaScript event: the snippet, where to add it, and how to test it.

***

A Custom JavaScript event fires when your own code tells it to. Use it for actions the no-code event types cannot detect, such as a form submitting successfully, a variant selection changing, or a product finder reaching its last step. Elevate A/B Testing generates the snippet, and you decide when it runs.

If the action is a page visit, click, element view, or scroll, use a no-code event instead. See [Create a Custom Event](https://docs.elevateab.com/elevate-helpcenter/experiment-setup/custom-events/create-a-custom-event.md).

**Prerequisites:** access to edit your theme code, or a developer who has it.

***

### How do I create a Custom JavaScript event?

1. In the Elevate A/B Testing sidebar, under **Configure**, select **Custom Events**, then **Create Custom Event**. You can also start from an experiment, with **Browse all metrics** then **Add Custom Metric**, with **Custom Metrics** then **Add new custom event**, or with **Custom Triggers** then **Add new custom event**.
2. Select **Custom JavaScript event**.
3. Enter an **Event name** and, optionally, a **Description**.
4. Under **Code snippet**, select **Copy Code**.
5. Edit the snippet so it fires on the action you want to track. See [What do I change in the snippet?](#what-do-i-change-in-the-snippet)
6. Add the snippet to your published theme. See [Where do I put the snippet?](#where-do-i-put-the-snippet)
7. On your live storefront, perform the action once.
8. Return to the panel and select **Validate**.

Validation confirms Elevate A/B Testing received the event. Until it passes, you have no evidence the tracking works, and an experiment measuring an event that never fires produces a flat result that looks like a real finding.

The event is stored in **Custom Events** and can be reused in any experiment.

### What does the snippet look like?

The generated snippet looks like this. Your `tracking_id` is unique to the event you created.

```html
<script>
  // Fire when the visitor performs the action you want to track.
  // Replace '#your-element' with the element they interact with.
  document.querySelector('#your-element')?.addEventListener('click', () => {
    window.Shopify &&
      window.Shopify.analytics &&
      window.Shopify.analytics.publish('eab:custom_event', {
        tracking_id: 'XXXX', // Required - do not change
        event_target: 'custom_event',
      });
  });
</script>
```

### What do I change in the snippet?

| Part            | Change it?                                                                                                            |
| --------------- | --------------------------------------------------------------------------------------------------------------------- |
| `#your-element` | Yes. Replace it with a CSS selector for the element visitors interact with, such as `.finder-submit` or `#notify-me`. |
| `'click'`       | Optional. Change it if the action is not a click.                                                                     |
| `tracking_id`   | No. It tells Elevate A/B Testing which event fired. Editing it breaks the event.                                      |
| `event_target`  | No.                                                                                                                   |

### Can I fire the event on something other than a click?

Yes. The `window.Shopify.analytics.publish` call is what records the event, so you can run it from any condition your theme can detect. This example fires only when a back in stock form submits:

```html
<script>
  document.addEventListener('submit', (event) => {
    if (!event.target.matches('form.back-in-stock')) return;
    window.Shopify &&
      window.Shopify.analytics &&
      window.Shopify.analytics.publish('eab:custom_event', {
        tracking_id: 'XXXX', // Replace with the tracking_id from your snippet
        event_target: 'custom_event',
      });
  });
</script>
```

Listening on `document` and checking the target, as this example does, also works for elements an app or script adds to the page after it loads. A plain `querySelector` at load time does not find those elements.

### Where do I put the snippet?

Add the snippet where the element exists. A snippet listening for an element that is not on the page does nothing.

| The action happens                                     | Put the snippet in                            |
| ------------------------------------------------------ | --------------------------------------------- |
| On many pages, such as a header or cart drawer element | `layout/theme.liquid`, just before `</body>`  |
| In one section, such as a product form                 | That section's file, below the section markup |

To edit theme code in Shopify, go to **Online Store**, then **Themes**, open the menu next to your theme, and select **Edit code**.

Add the snippet in one place only. The same snippet in two files fires the event twice per action.

### Does the snippet run on every variation?

Only if every variation uses a template or theme that contains it. Variation templates and alternative themes do not automatically carry code added elsewhere. If the snippet exists only on the control, the variation records nothing and loses by default.

Before launching a page or theme experiment, add the snippet to every variation template or theme, then preview each variation and search the page source for `eab:custom_event`.

### Next steps

* [Use Custom Events as Goals and Metrics](https://docs.elevateab.com/elevate-helpcenter/experiment-setup/custom-events/use-custom-events-as-goals-and-metrics.md) to add the event to an experiment
* [Troubleshooting Custom Events](https://docs.elevateab.com/elevate-helpcenter/experiment-setup/custom-events/troubleshooting-custom-events.md) if **Validate** does not pass
* [Custom Events](https://docs.elevateab.com/elevate-helpcenter/experiment-setup/custom-events.md) for an overview of event types and where to use them
