Docs Platform guides
Shopify
Add OpL Insights to a Shopify store: theme script and checkout pixel.
Shopify runs your store on its own servers, so there is nothing to install: you add the OpL Insights script to your theme, and a custom pixel for the checkout. Shopify doesn't allow scripts on its checkout pages, so checkout steps and orders are recorded as events; the checkout itself is not replayed.
1. Create the site
In OpL Insights, go to Sites > Add site, choose Shopify and enter your store's address. Copy the site ID: it replaces SITE_ID below.
2. Add the script to your theme
In Shopify admin, go to Online Store > Themes, open the menu next to your live theme and choose Edit code. Open layout/theme.liquid and paste this just before </head>:
<script>
window.OPL_INSIGHTS = {
m: 'https://app.oplinsights.com/', s: SITE_ID,
cart: '/cart.js', cartFormat: 'shopify', cartRe: '/cart/(add|change|update|clear)',
uid: {% if customer %}'{{ customer.id }}'{% else %}''{% endif %},
ctx: {
route: {% case template.name %}{% when 'index' %}'common/home'{% when 'product' %}'product/product'{% when 'collection' %}'product/category'{% when 'search' %}'product/search'{% when 'cart' %}'checkout/cart'{% when '404' %}'error/not_found'{% else %}'page/{{ template.name }}'{% endcase %},
cur: '{{ shop.currency }}'{% if template.name == 'product' %},
product: { sku: {{ product.selected_or_first_available_variant.sku | default: product.id | json }}, name: {{ product.title | json }}, cat: {{ product.type | json }}, price: {{ product.price | divided_by: 100.0 }} }{% endif %}{% if template.name == 'collection' %},
category: {{ collection.title | json }}{% endif %}{% if template.name == 'search' %},
search: {{ search.terms | json }}, search_count: {{ search.results_count }}{% endif %}{% if template.name == '404' %},
not_found: 1{% endif %}
}
};
</script>
<script async src="https://app.oplinsights.com/plugins/OplReplay/javascripts/opl-insights.js"></script>
To see customers' e-mail addresses instead of their IDs, change customer.id to customer.email.
3. Track the checkout and orders
Go to Settings > Customer events > Add custom pixel, name it OpL Insights, paste the code below with your site ID, then Save and Connect.
const M = 'https://app.oplinsights.com/', S = SITE_ID;
// the storefront visitor, so checkout steps and the order join the visit they belong to
async function visitor(e) {
try {
const host = (e.context.document.location.hostname || '').replace(/^www\./, '');
const d = await crypto.subtle.digest('SHA-1', new TextEncoder().encode(host + '/'));
const h = Array.from(new Uint8Array(d)).map(b => b.toString(16).padStart(2, '0')).join('').slice(0, 4);
const c = await browser.cookie.get('_pk_id.' + S + '.' + h);
return c ? c.split('.')[0] : '';
} catch (err) { return ''; }
}
async function send(e, params) {
const id = await visitor(e);
const q = new URLSearchParams(Object.assign({ idsite: S, rec: 1, apiv: 1, send_image: 0, url: e.context.document.location.href, rand: Math.random() }, id ? { _id: id } : {}, params));
fetch(M + 'matomo.php?' + q.toString(), { method: 'POST', mode: 'no-cors', keepalive: true });
}
const amount = m => (m && m.amount) || 0;
analytics.subscribe('checkout_started', e => send(e, { e_c: 'Checkout', e_a: 'begin_checkout' }));
analytics.subscribe('checkout_shipping_info_submitted', e => send(e, { e_c: 'Checkout', e_a: 'add_shipping_info' }));
analytics.subscribe('payment_info_submitted', e => send(e, { e_c: 'Checkout', e_a: 'add_payment_info' }));
analytics.subscribe('checkout_completed', e => {
const c = e.data.checkout;
const items = (c.lineItems || []).map(i => [
(i.variant && i.variant.sku) || String((i.variant && i.variant.id) || ''), i.title,
(i.variant && i.variant.product && i.variant.product.type) || '', amount(i.variant && i.variant.price), i.quantity
]);
send(e, {
idgoal: 0, ec_id: (c.order && c.order.id) || c.token, revenue: amount(c.totalPrice), ec_st: amount(c.subtotalPrice),
ec_tx: amount(c.totalTax), ec_sh: amount(c.shippingLine && c.shippingLine.price), ec_dt: amount(c.discountsAmount),
ec_items: JSON.stringify(items)
});
});
4. Check it works
Open your store in a private window, add something to the cart and place a test order. The visit, the cart update, the checkout steps and the order show in OpL Insights within a few minutes.