How to Implement Audiohook tracking on purchases on Shopify

Shopify has sandboxed their custom events for tracking purchases, making some changes to how the Audiohook tag needs to be added for purchase events

Step 1: Log in to Shopify Admin

  1. Go to your Shopify store admin.
  1. From the left menu, navigate to Settings → Customer Events.
Notion image

Step 2: Create a New Custom Event

  1. Click Add custom pixel.
  1. Give it a clear name, such as:
    1. Audiohook Purchase Event

Notion image

Step 3: Paste in the Code

  1. Copy the following code block.
  1. Paste it into the pixel editor:
// Load Audiohook pixel inside Shopify's Customer Events sandbox
(function loadAudiohook(callback) {
  const script = document.createElement("script");
  script.src = "https://js.audiohook.com/analytics.js?audiohookId=[AUDIOHOOK ID GOES HERE]";
  script.async = true;
  script.onload = callback;
  script.onerror = () => console.warn("Failed to load Audiohook analytics.js");
  document.head.appendChild(script);
})(function onAudiohookReady() {
  // PURCHASE / REPEAT PURCHASE — routes based on Shopify's isFirstOrder flag
  // Guest checkouts (no customer record) default to "purchase"
  analytics.subscribe("checkout_completed", (event) => {
    if (typeof window.ahTrack !== "function") return;
    const checkout = event?.data?.checkout;
    if (!checkout) return;
    const customer = checkout?.order?.customer;
    const isFirstOrder = customer?.isFirstOrder;
    const ordersCount = customer?.ordersCount;
    // Only explicit returning customers route to repeatpurchase;
    // new customers and guests (undefined isFirstOrder) route to purchase
    const eventName = isFirstOrder === false ? "repeatpurchase" : "purchase";
    window.ahTrack(eventName, {
      value: checkout.totalPrice?.amount,
      currency: checkout.currencyCode,
      order_id: checkout?.order?.id,
      email: checkout.email,
      customer_orders_count: ordersCount
    });
  });
  // INITIATE CHECKOUT event (all users)
  analytics.subscribe("checkout_started", (event) => {
    if (typeof window.ahTrack !== "function") return;
    const checkout = event?.data?.checkout;
    if (!checkout) return;
    window.ahTrack("initiate_checkout", {
      value: checkout.totalPrice?.amount,
      currency: checkout.currencyCode,
      email: checkout.email
    });
  });
});


Step 4: Swap the Audiohook ID

In the code above, replace:

YOUR_AUDIOHOOK_ID

with your unique Audiohook Pixel ID.

Example:

https://js.audiohook.com/analytics.js?audiohookId=a97cc759-0f29-4c24-8287-55a115dca83b


Step 5: Save and Test

  1. Click Save in the Shopify editor.
  1. Click Connect to verify the pixel is working.
Notion image
Did this answer your question?
😞
😐
🤩