Events

Markdown

View as Markdown

You can listen to five tracking events on your Storelink storefront to record shopper activity or send data to external tools.

Tracking events ignore any value returned by a handler. If a handler throws an error, Storelink logs it to the browser console without interrupting the shopper.

page:changed

Fires when the shopper opens a page, including the initial page load and every navigation to a new path.

Sample code to listen for page changes:

storelink.on('page:changed', function(page) {
  console.log('Visited:', page.pathname, page.search);
});

Here, the parameter page is an object containing the current path and query parameters.

FieldTypeDescription
pathnamestringCurrent URL path (for example, '/product/denim-jacket').
searchobjectQuery string parameters as key-value string pairs.

product:viewed

Fires when a product page opens, and again each time the shopper selects a different variant. Does not fire in the editor preview.

Sample code to track product views:

storelink.on('product:viewed', function(item) {
  console.log('Viewed:', item.id, item.price, item.currency);
});

Here, the parameter item is an object describing the viewed product and active variant. This event does not include a qty field.

FieldTypeDescription
idstringProduct slug combined with variant values (for example, 'denim-jacket-L'). Matches the item id in order:placed.
pricenumberThe variant's discounted price if set, otherwise its regular price.
thumbnailstringURL of the variant's first image, if available.
currencystringStore currency code (for example, 'USD').
selectedVariantobjectActive variant details, including key, value, price, discountedPrice, stock, availability, description, and images.
selectedProductobjectFull product object, including name, slug, category, and tags.

cart:item-added

Fires when a product is added to the cart from the product page, or when its quantity is increased from a product card.

Sample code to track items added to the cart:

storelink.on('cart:item-added', function(item) {
  console.log('Added:', item.selectedProduct.name, 'x', item.qty, item.currency);
});

Here, the parameter item is the cart item object that was added.

FieldTypeDescription
idstringProduct slug combined with variant values.
qtynumberQuantity added to the cart.
pricenumberThe variant's discounted price if set, otherwise its regular price.
thumbnailstringURL of the variant's first image, if available.
currencystringStore currency code.
selectedVariantobjectActive variant details, including key, value, price, discountedPrice, stock, availability, description, and images.
selectedProductobjectFull product object, including name, slug, category, and tags.

cart:item-removed

Fires when a product's quantity is lowered from a product card, or when it is removed from a product card or the cart.

Sample code to track items removed from the cart:

storelink.on('cart:item-removed', function(item) {
  console.log('Removed or decreased:', item.id, 'Remaining qty:', item.qty);
});

Here, the parameter item is the updated cart item object. Note that qty is the quantity left in the cart (0 when removed), and this event does not include a currency field.

FieldTypeDescription
idstringProduct slug combined with variant values.
qtynumberQuantity left in the cart (0 when removed completely).
pricenumberThe variant's discounted price if set, otherwise its regular price.
thumbnailstringURL of the variant's first image, if available.
selectedVariantobjectActive variant details, including key, value, price, discountedPrice, stock, availability, description, and images.
selectedProductobjectFull product object, including name, slug, category, and tags.

order:placed

Fires when an order is placed, before the shopper is sent to the order summary page or WhatsApp.

Sample code to inspect a placed order:

storelink.on('order:placed', function(order) {
  console.log('Order placed:', order.orderNumber, order.value, order.currency);
});

Here, the parameter order contains the checkout response, order total, currency, and ordered items.

FieldTypeDescription
orderPlacedstring'success' when the order succeeds.
orderSheetAppendstringGoogle Sheets sync status: 'success', 'failed', or 'skipped'.
orderNumbernumberOrder number assigned to the order.
orderIdstringUnique order identifier.
whatsappUrlstringWhatsApp redirect URL, present on stores that take orders on WhatsApp.
currencystringStore currency code.
valuenumberTotal order amount.
itemsarrayList of ordered item objects (see table below).

items Fields

Each entry in order.items contains the following fields:

FieldTypeDescription
idstringItem identifier matching the cart events.
namestringProduct name.
variantstringSelected variant values as text (for example, 'Size - L, Color - Blue').
selectedVariantKeystringVariant key string.
selectedProductobjectSummary product object with name, slug, category, and thumbnail.
thumbnailstringURL of the item image.
qtynumberQuantity ordered.
pricenumberUnit price of the item.
totalnumberLine total for the item.
originalPricenumberOriginal price when a coupon or custom function changed the price (optional).
discountnumberDiscount amount applied to the item (optional).
couponCodestringCoupon code applied to the item (optional).

Recipes

Meta Pixel

Sample code to send AddToCart and Purchase events to Meta Pixel (assuming the base Meta Pixel script is already loaded):

storelink.on('cart:item-added', function(item) {
  if (typeof fbq !== 'function') return;
  fbq('track', 'AddToCart', {
    content_ids: [item.id],
    content_name: item.selectedProduct.name,
    content_type: 'product',
    value: item.price * item.qty,
    currency: item.currency
  });
});

storelink.on('order:placed', function(order) {
  if (typeof fbq !== 'function') return;
  fbq('track', 'Purchase', {
    content_ids: order.items.map(function(item) { return item.id; }),
    content_type: 'product',
    num_items: order.items.reduce(function(sum, item) { return sum + item.qty; }, 0),
    value: order.value,
    currency: order.currency
  });
});

TikTok Pixel

Sample code to send AddToCart and CompletePayment events to TikTok Pixel (assuming the base TikTok Pixel script is already loaded):

storelink.on('cart:item-added', function(item) {
  if (typeof ttq === 'undefined') return;
  ttq.track('AddToCart', {
    contents: [{
      content_id: item.id,
      content_name: item.selectedProduct.name,
      quantity: item.qty,
      price: item.price
    }],
    value: item.price * item.qty,
    currency: item.currency
  });
});

storelink.on('order:placed', function(order) {
  if (typeof ttq === 'undefined') return;
  ttq.track('CompletePayment', {
    contents: order.items.map(function(item) {
      return {
        content_id: item.id,
        content_name: item.name,
        quantity: item.qty,
        price: item.price
      };
    }),
    value: order.value,
    currency: order.currency
  });
});

Google Analytics 4

Sample code to send add_to_cart and purchase events to Google Analytics 4 (assuming gtag is already loaded):

storelink.on('cart:item-added', function(item) {
  if (typeof gtag !== 'function') return;
  gtag('event', 'add_to_cart', {
    currency: item.currency,
    value: item.price * item.qty,
    items: [{
      item_id: item.id,
      item_name: item.selectedProduct.name,
      price: item.price,
      quantity: item.qty
    }]
  });
});

storelink.on('order:placed', function(order) {
  if (typeof gtag !== 'function') return;
  gtag('event', 'purchase', {
    transaction_id: String(order.orderNumber),
    currency: order.currency,
    value: order.value,
    items: order.items.map(function(item) {
      return {
        item_id: item.id,
        item_name: item.name,
        item_variant: item.variant,
        price: item.price,
        quantity: item.qty
      };
    })
  });
});

Send Order Data to Your Own Endpoint

Sample code to forward placed orders to your own server with fetch:

storelink.on('order:placed', function(order) {
  fetch('https://example.com/webhooks/storelink-order', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    keepalive: true,
    body: JSON.stringify({
      orderId: order.orderId,
      orderNumber: order.orderNumber,
      currency: order.currency,
      value: order.value,
      items: order.items
    })
  });
});

Here, keepalive: true lets the browser finish the request even as the storefront redirects the shopper to the summary page or WhatsApp.

What's Next: Continue to Functions.

Related Articles

Was this article helpful?