Documentation menuEcommerce: Shopify

Ecommerce

Add Loogika to Shopify

On Shopify you paste the Loogika tag into the theme.liquid layout file of your theme. That file wraps every storefront page. The checkout runs on separate pages that Shopify controls, so you record purchases with a custom pixel under Customer events.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own 12-character site ID in place of YOUR_SITE_ID:

<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>

How do you add the Loogika tag to a Shopify theme?

Open the code editor of your current theme and paste the tag into the head of theme.liquid.

  1. In Shopify admin, go to Online Store, then Themes.
  2. Find your current theme, click the three-dot button beside Customize and pick Edit code.
  3. In the Layout folder, select theme.liquid.
  4. Paste the tag just before the closing </head> tag and click Save.

A theme update can overwrite theme.liquid, and Shopify has no child themes that protect your edits. Add the tag again after you update a purchased theme.

How do you check that Loogika is installed?

Bring up your store in a separate browser tab and browse a few products. The pageviews show up in the Loogika dashboard within a few seconds. If nothing arrives, open the page source and find script.js?siteId=.

How do you track product views and cart events?

You can call window.loogika.event() straight from the storefront templates. Put each call inside a DOMContentLoaded listener, because the Loogika tag loads with defer. Divide Liquid prices by 100, because Liquid gives prices in cents.

Back up your theme before you edit its code. Themes differ in their Liquid object names, such as product.selected_or_first_available_variant, and in the form selectors used below. Change both to fit your theme.

How do you track a product view?

Add the code below to your main product template. Some themes keep that template in sections/main-product.liquid, and others in templates/product.liquid.

<script>
document.addEventListener('DOMContentLoaded', function () {
  var variant = {{ product.selected_or_first_available_variant | json }};
  window.loogika?.event('view_item', {
    item_id: variant.sku || variant.id,
    item_name: {{ product.title | json }},
    price: variant.price / 100,
    currency: {{ cart.currency.iso_code | json }}
  });
});
</script>

How do you track an add to cart?

Listen for the submit of the product form in the same template. When a theme has an AJAX cart drawer, adding an item usually triggers a JavaScript event from the theme itself. That event is a more reliable trigger than the form submit on such themes.

<script>
document.addEventListener('DOMContentLoaded', function () {
  var form = document.querySelector('form[action="/cart/add"]');
  if (!form) return;
  form.addEventListener('submit', function () {
    var variant = {{ product.selected_or_first_available_variant | json }};
    var qty = form.querySelector('[name="quantity"]');
    window.loogika?.event('add_to_cart', {
      item_id: variant.sku || variant.id,
      item_name: {{ product.title | json }},
      price: variant.price / 100,
      currency: {{ cart.currency.iso_code | json }},
      quantity: qty && parseInt(qty.value, 10) > 0 ? parseInt(qty.value, 10) : 1
    });
  });
});
</script>

How do you track the start of a checkout?

Add the code below to your cart template to record clicks on the checkout button. Some themes name that file sections/main-cart-items.liquid, and others templates/cart.liquid.

<script>
document.addEventListener('DOMContentLoaded', function () {
  var button = document.querySelector('[name="checkout"]');
  if (!button) return;
  button.addEventListener('click', function () {
    var cart = {{ cart | json }};
    window.loogika?.event('begin_checkout', {
      currency: cart.currency,
      value: cart.total_price / 100
    });
  });
});
</script>

Loogika accepts one flat object of properties with text and number values, up to 2,048 characters as JSON. A larger event is rejected, not shortened. Send the cart totals as shown, not the full list of cart items.

How do you track purchases on Shopify?

Track purchases with a custom pixel, because your theme is not loaded on the checkout or on the order status page. Shopify also had an Additional scripts field for the order status page, and Shopify has deprecated it. Since August 28, 2025 the field can only be viewed, and stores outside Shopify Plus had until August 26, 2026 to migrate.

A custom pixel runs in a sandbox, a closed area with no access to window.loogika. The pixel sends the event straight to the Loogika events endpoint with an HTTP request.

  1. In Shopify admin, go to Settings, then Customer events.
  2. Choose Add custom pixel, enter Loogika as the name and confirm with Add pixel.
  3. Paste the code below and replace YOUR_SITE_ID.
  4. Click Save, then Connect.
analytics.subscribe("checkout_completed", (event) => {
  const checkout = event.data.checkout;

  fetch("https://app.loogika.com/api/track", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    keepalive: true,
    body: JSON.stringify({
      type: "custom_event",
      site_id: "YOUR_SITE_ID",
      event_name: "purchase",
      // properties is a JSON object written as a string
      properties: JSON.stringify({
        transaction_id: checkout.order?.id || checkout.token,
        value: checkout.totalPrice?.amount,
        currency: checkout.currencyCode,
      }),
    }),
  });
});

Shopify fires checkout_completed once for each placed order. The fields of the request are described on the sending events page.

The same Customer events API also offers product_added_to_cart and checkout_started. You can move the add-to-cart and checkout events into the pixel if the theme code is unreliable on your store.