Browse articles

Google Analytics is missing the add-to-cart event

Send add-to-cart events to your own data layer or Google Tag Manager setup with EliteCart's storefront event.

Last updated

If you track add-to-cart with your own data layer or a Google Tag Manager trigger that watches your theme's add-to-cart button, those events can go missing once EliteCart handles add to cart. EliteCart sends its own storefront event for every add to cart it handles, and you can push your analytics event from there.

The event EliteCart sends

EliteCart dispatches eliteAddProductToCart on document:

  • When: the moment EliteCart takes over an add-to-cart click or product form submit in your theme (product page, quick-add buttons), before Shopify confirms the add.
  • Not for: products added from inside the cart, such as upsells, addons and gift rewards.
  • Data: none. The event only says "a product is being added". Take the product details from your page, as your data layer usually does already.

Listen to it in your data layer

Replace the code that listens to your add-to-cart button with a listener for eliteAddProductToCart. Plain JavaScript, no jQuery needed:

<script>
  document.addEventListener("eliteAddProductToCart", function () {
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
      event: "add_to_cart",
      // Add the ecommerce object your data layer already builds for this page,
      // for example the selected variant's ID, name and price.
    });
  });
</script>

Put it where your data layer code lives, usually in your theme (for example a data layer snippet included in theme.liquid), so it's in place before the first add to cart.

Other events you can use

If you need the cart's contents or want to track every cart change:

Event or methodWhen it firesWhat you get
eliteCartUpdated on documentAfter EliteCart has added a product, including upsells and addonsNo data
eliteCartRendered on documentWhen the cart has rendered with new contentsThe cart in event.detail.cart
window.EliteCart.onCartChange(callback)Every time the cart changesThe cart, passed to your callback. Returns a function that stops listening
window.EliteCart.getCurrentCart()Whenever you call itThe current cart

Example: log the number of items whenever the cart renders.

<script>
  document.addEventListener("eliteCartRendered", function (event) {
    var cart = event.detail.cart;
    console.log("Items in cart:", cart.item_count);
  });
</script>

The full list of methods and events is in the EliteCart API article.

Still stuck?

Chat with us and we'll help you get your cart live. Or email contact@elitecart.app.