Skip to main content
Aftersell handles add-to-cart itself, which means analytics tools that watch Shopify’s default cart requests may not see it. Subscribing to the SDK’s events restores tracking, and gives you a cleaner payload than scraping the Ajax API. This is the most common thing merchants use the SDK for.

The pattern

Every integration is the same three lines: subscribe to an event, read the payload, forward it to your tool.
Paste it into Cart settings → Custom script → Initialization. Subscribing is a set-up call, so it’s safe at the very top, so you don’t need to wait for the cart to load.

Klaviyo

Make sure Klaviyo onsite tracking is installed first. This script only fires the event; it doesn’t load Klaviyo.

Triple Whale

Google Analytics 4

Other events worth tracking

Use navigator.sendBeacon in a checkout handler. The page is about to unload, and a normal fetch may be cancelled mid-flight. You also can’t cancel checkout from this handler; it’s a notification, not a gate.

Things to get right

  • Prices are in cents. Divide by 100 for tools that expect a decimal amount. formatMoney is for display only, so don’t send a formatted string to an analytics tool.
  • A quantity bump isn’t item_added. Going from 1 to 3 on an existing line fires cart_updated, not item_added. If your tool needs to see that, diff against the previous state in a cart_updated handler.
  • Don’t track from cart_updated if you mean “added”. It fires on every change, including removals and quantity edits, so you’ll over-report.
  • Check for double-counting. If your tool already captures add-to-cart some other way, this script will report it twice. Verify in the tool’s live view before going live.

Testing it

Add a console.log alongside your tracking call, then open the browser console and add a product:
If the log doesn’t appear, the script isn’t running, so check it saved to the Initialization slot. If the log appears but the tool shows nothing, the problem is in the tool’s payload format, not the SDK.

Where to go next