Introduction

Markdown

View as Markdown

The Storelink storefront JavaScript API lets you listen to shopper activity and customize prices, fees, shipping rates, and checkout behavior on your store.

Use Cases

With the Storelink JavaScript API, you can perform the following:

  • Send storefront events to analytics and ad pixels such as Meta Pixel, TikTok Pixel, and Google Analytics 4.

  • Send placed order details to your own server or webhook endpoint.

  • Apply bulk or tiered product pricing based on quantity.

  • Add conditional checkout fees such as a Cash on Delivery (COD) charge or a weight-based handling fee.

  • Override shipping rates based on the delivery address or cart contents.

  • Block checkout when an order does not meet your store rules, such as a minimum order amount or an unsupported postal code.

  • Customize storefront layout and appearance using stable element selectors and CSS variables.

Where to Add Code

In the Storelink dashboard, go to Settings > Integrations, open the Custom script card, and click + Add script. Set Page to All Pages, then paste your code inside <script> tags.

See How to add custom JavaScript to my online store for step-by-step instructions.

Sample code structure for a custom script:

<script>
storelink.on('page:changed', function(page) {
  console.log('Current page:', page.pathname);
});
</script>

Global Variable

The API is exposed through the global variable storelink (window.storelink). It is defined before any custom script runs, so you can register handlers directly at the top level of your script without waiting for the page to load.

Registers a handler function for a storefront event. This is the only method on storelink. Handlers can be synchronous or async functions, and they stay active for the page session.

Sample code to register a handler when an item is added to the cart:

storelink.on('cart:item-added', function(item) {
  console.log('Added to cart:', item.id, item.qty);
});

Here, the parameter event is the event name string, and handler is a function that receives the event payload.

Tracking Events and Functions

Storelink provides two kinds of events:

  • Tracking events fire when a shopper navigates, views a product, updates the cart, or places an order. Return values are ignored, and errors inside a handler are logged to the browser console without affecting the page.

  • Functions fire when a shopper selects a quantity, changes a payment method, or updates a shipping method or delivery address. They can return values that change what the shopper sees and what is sent with the order, including prices, fees, shipping rates, and checkout availability.

Available Events

The following events are supported by storelink.on(event, handler):

EventKindDescription
page:changedTrackingFires when the shopper opens a page (first load and every navigation to a new path).
product:viewedTrackingFires when a product page opens and each time a different variant is selected.
cart:item-addedTrackingFires when a product is added to the cart or its quantity is increased from a product card.
cart:item-removedTrackingFires when a product's quantity is lowered from a product card, or it is removed from a card or the cart.
order:placedTrackingFires when an order is placed, before the shopper is sent to the summary page or WhatsApp.
product:quantity-selectedFunctionFires when quantity or variant changes on a product page or featured product, and for every cart row on load and quantity change.
checkout:payment-changedFunctionFires when the cart opens, and again whenever the payment method, subtotal, coupon, or checkout step changes.
checkout:address-changedFunctionFires when the cart opens, and again whenever the shipping method, delivery address, subtotal, coupon, or checkout step changes.

Legacy Event Names

Older scripts may use the legacy names below. They still work as aliases for the current names, but new scripts should use the current names.

Legacy NameCurrent Name
pagechangepage:changed
productviewproduct:viewed
addtocartcart:item-added
removefromcartcart:item-removed
orderplacedorder:placed

What's Next: Continue to Events.

Related Articles

Was this article helpful?