We’re upgrading our email infrastructure — for immediate response, email andrewjgaber@gmail.com meanwhile.
Skip to main content
Part of Digital Empire
Product analytics / CDP

Amplitude amplitude.track('Purchase') / logRevenue at Shopify checkout

Amplitude · Web Pixels API replacement · confidence: high

Dies 2026-08-26

Paste-ready snippet
// Wave4-Hotel7 (2026-08-20): Amplitude Purchase event + revenue tracking
// replacement. Amplitude is heavily used by DTC brands and B2C SaaS for
// funnel analysis + retention cohorts. The legacy install pastes
// amplitude.getInstance().init(API_KEY) + amplitude.getInstance().logEvent(
// 'Purchase', {...}) + amplitude.getInstance().logRevenueV2(revenue) into
// Shopify Additional Scripts. All three call sites stop executing 2026-08-26.
//
// Two variants: pick ONE.
//
// ============================================================
// VARIANT A -- BROWSER-SIDE (Shopify Web Pixels sandbox).
// Uses Amplitude's Browser SDK v2 loaded from cdn.amplitude.com inside
// the pixel sandbox. AMPLITUDE_API_KEY is a project API key -- documented
// safe to expose to browser per Amplitude docs (project keys are write-
// only to Amplitude's ingestion endpoint, cannot read data back). Uses
// the modern Revenue helper instead of the deprecated logRevenueV2 path.
// ============================================================
(function loadAmplitude() {
  if (window.amplitude && window.amplitude.track) return;
  const s = document.createElement('script');
  s.async = true;
  s.src = 'https://cdn.amplitude.com/libs/analytics-browser-2.11.1-min.js.gz';
  s.onload = () => window.amplitude.init('AMPLITUDE_API_KEY', { defaultTracking: false });
  document.head.appendChild(s);
})();

analytics.subscribe('checkout_completed', (event) => {
  const { checkout } = event.data;
  if (!checkout || !checkout.email || !window.amplitude) return;

  // Identify the user so purchases roll up to the same profile as pre-
  // checkout behavioral events fired from theme.liquid / product pages.
  window.amplitude.setUserId(
    checkout.order ? String(checkout.order.customerId || checkout.order.id) : checkout.email
  );

  // Revenue helper -- populates Amplitude's built-in Revenue table
  // (drives LTV cohort reports). Do this BEFORE the track() call so
  // revenue is attached to the same session context.
  const revenue = new window.amplitude.Revenue()
    .setPrice(Number(checkout.totalPrice.amount))
    .setQuantity(1)
    .setEventProperties({
      order_id: checkout.order ? String(checkout.order.id) : checkout.token,
      currency: checkout.totalPrice.currencyCode,
    });
  window.amplitude.revenue(revenue);

  window.amplitude.track('Purchase', {
    order_id: checkout.order ? String(checkout.order.id) : checkout.token,
    total: Number(checkout.totalPrice.amount),
    currency: checkout.totalPrice.currencyCode,
    item_count: (checkout.lineItems || []).length,
    products: (checkout.lineItems || []).map((li) => ({
      product_id: li.variant && li.variant.product ? String(li.variant.product.id) : null,
      sku: li.variant ? li.variant.sku : null,
      name: li.title,
      price: li.variant && li.variant.price ? Number(li.variant.price.amount) : null,
      quantity: li.quantity,
    })),
  });
});

// ============================================================
// VARIANT B -- SERVER-SIDE (Amplitude HTTP V2 API + Shopify webhook).
// Recommended for accurate revenue reporting. Ad-blockers can drop
// browser-side Amplitude calls; server-side is authoritative.
// Docs: https://amplitude.com/docs/apis/analytics/http-v2
// ============================================================
//
// // Shopify webhook: orders/paid
// export async function handleShopifyOrderPaidForAmplitude(order) {
//   await fetch('https://api2.amplitude.com/2/httpapi', {
//     method: 'POST',
//     headers: { 'Content-Type': 'application/json' },
//     body: JSON.stringify({
//       api_key: process.env.AMPLITUDE_API_KEY,  // same project key, server-side
//       events: [{
//         user_id: String(order.customer ? order.customer.id : order.id),
//         event_type: 'Purchase',
//         time: new Date(order.created_at).getTime(),
//         insert_id: 'shopify-order-' + order.id,  // dedupe key
//         event_properties: {
//           order_id: String(order.id),
//           total: Number(order.total_price),
//           currency: order.currency,
//           item_count: order.line_items.length,
//         },
//         revenue: Number(order.total_price),
//         price: Number(order.total_price),
//         quantity: 1,
//         revenueType: 'purchase',
//       }],
//     }),
//   });
// }

Legacy pattern detected via: amplitude\.(getInstance\(\)\.)?logEvent\(\s*['"]Purchase['"]|amplitude\.(getInstance\(\)\.)?logRevenueV2|amplitude\.track\(\s*['"]Purchase['"]

What this does NOT cover

What this does NOT cover: pre-purchase funnel events (Product Viewed / Product Added / Checkout Started / Payment Info Entered), Amplitude Session Replay setup, Amplitude Experiment flag exposure logging, or Amplitude CDP (formerly mParticle) forwarding. Those all continue to work if pasted in theme.liquid instead of Additional Scripts; only the checkout-completed path is impacted by Aug 26.

Test-event verification checklist

  • The revenue helper (`amplitude.revenue()`) is what populates Amplitude's Revenue LTV chart and Recurring Revenue analysis -- calling only `amplitude.track('Purchase', {revenue: ...})` records the event but does NOT feed those revenue-specific views. Fire BOTH revenue() and track() together, as shown in Variant A.
  • Verify a live purchase in Amplitude within 60 seconds via User Look-Up: search by user_id or email, open the user's Event Stream, and confirm the Purchase event landed with the correct total and product array. If events appear but Revenue chart is empty, `amplitude.revenue()` was skipped or revenueType was set to something other than 'purchase'.
  • Duplicate-check: if you use Amplitude's Segment destination (via the segment-analytics-track-order-completed snippet), Segment already forwards Order Completed to Amplitude as a Purchase event -- do NOT also ship this snippet or Amplitude will double-count revenue. Choose ONE path: direct-to-Amplitude (this snippet) OR via Segment (Segment snippet + Amplitude destination configured in Segment's UI).
  • Verify live against the platform's own test-event tool: https://amplitude.com/docs/data/user-look-up