learnshopify.dev/Workspace
code reading

The agency's pixel extension. Trace what it sends, and when.

// extensions/northwind-pixel/src/index.js
import { register } from "@shopify/web-pixels-extension";

register(({ analytics, settings }) => {
  const endpoint = settings.endpoint;

  const send = (payload) =>
    fetch(endpoint, {
      method: "POST",
      body: JSON.stringify(payload),
      keepalive: true,
    });

  analytics.subscribe("product_viewed", (event) => {
    const variant = event.data.productVariant;
    send({
      event: event.name,
      variant: variant.title,
      price: variant.price.amount,
    });
  });

  analytics.subscribe("checkout_completed", (event) => {
    const checkout = event.data.checkout;
    send({
      event: event.name,
      total: checkout.totalPrice.amount,
      items: checkout.lineItems.length,
    });
  });
});
1. Why does this code run in a worker sandbox with no `document`?
2. An EU visitor hasn't accepted tracking. What do these subscribers do?
3. What makes `checkout_completed` a standard event?
4. Why can't a theme `<script>` report this conversion?
5. Why does every request pass `keepalive: true`?
0 / 1 task solved