Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

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.

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?

  6. Add the snippet to your published theme. See 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.

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:

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

Last updated