# Shopify

> Measure your Shopify shop with Nvivio, with the script in your theme for visits and a custom pixel that counts every purchase with its amount.

## Visits: the script in your theme

1. In Shopify, open **Online Store › Themes**, click **…** next to your theme and choose **Edit code**.
2. Open `layout/theme.liquid` and paste your site's line from **Settings › Installation** in Nvivio just above `</head>`:

```html
<script defer src="https://nviv.io/js/NV-XXXXXXX.js"></script>
```

3. Save. Nvivio's installation page says "It works" as soon as the first visit arrives.

Add every domain your shop is reached on to the site in Nvivio, under **Settings › Domains**: your own domain and, if customers see it, `yourshop.myshopify.com` ([Domains](https://nvivio.com/docs/settings#domains)).

## Purchases: a custom pixel

Shopify's checkout does not run the scripts of your theme. A custom pixel does run there, and counts each completed order as the event `purchase`, with its total and currency ([Revenue](https://nvivio.com/docs/revenue)).

1. In Shopify, open **Settings › Customer events** and click **Add custom pixel**. Name it Nvivio.
2. Paste the code below and replace `NV-XXXXXXX` with your site code.
3. Under **Customer privacy**, choose what fits your own consent policy. Nvivio without cookies stores no personal data and needs no consent ([Privacy and GDPR](https://nvivio.com/docs/privacy)).
4. Click **Save**, then **Connect**.

```js
// Nvivio: every completed order as the event purchase, with its total and currency.
const SITE = 'NV-XXXXXXX';

analytics.subscribe('checkout_completed', (event) => {
  const checkout = event.data.checkout;
  fetch('https://nviv.io/e', {
    method: 'POST',
    keepalive: true,
    body: JSON.stringify({
      s: SITE,
      n: 'purchase',
      // A fixed address: the checkout's own address carries its token, which stays with Shopify.
      u: event.context.document.location.origin + '/checkout/thank-you',
      p: { revenue: { amount: checkout.totalPrice.amount, currency: checkout.currencyCode } }
    })
  });
});
```

- Only the total and its currency are sent: never the order number, the products or the customer.
- The purchase belongs to the visit it ended, so revenue shows per source, campaign, page and country. Without cookies, Nvivio recognises the same browser on the same day; in [cookie mode](https://nvivio.com/docs/privacy#with-one-cookie-optional) the pixel cannot read the cookie, so a purchase may start a visit of its own.
- The total includes tax and shipping, in the currency the customer paid in; Nvivio shows it in your site's currency. To leave out tax, send `checkout.subtotalPrice.amount` instead.
- Make a goal of the event `purchase` to see its conversion rate and revenue per goal ([Goals and funnels](https://nvivio.com/docs/goals-and-funnels)).
