Tracking Shopify customer events
Shopify's checkout and thank-you (order status) pages run in a locked-down environment where your normal site scripts, including the Testa script, cannot run. To track conversions there, use Shopify's Customer Events to send the conversion to Testa.
Add a custom pixel
In your store admin, go to Settings → Customer events → Add custom pixel, and
add the following. Replace the project ID with your own (the id in your script
URL, /projects/<uuid>.js):
js
// Your Testa project UUID
var TESTA_PROJECT = "<your-project-uuid>";
// Purchase conversion on the checkout / thank-you page
analytics.subscribe("checkout_completed", function (event) {
browser.cookie.get("_testa_uuid").then(function (uuid) {
if (!uuid) return;
var url =
"https://new.testa-soft.tech/api/pixel" +
"?uuid=" + encodeURIComponent(uuid) +
"&event=purchase" + // must match your goal's name
"&project=" + encodeURIComponent(TESTA_PROJECT) +
"&data=" + encodeURIComponent(
JSON.stringify({
amount: event.data.checkout.totalPrice,
currency: event.data.checkout.currencyCode,
}),
) +
"&_t=" + Date.now();
fetch(url, { method: "GET", keepalive: true });
});
});
// Page-view goal on any storefront page
analytics.subscribe("page_viewed", function (event) {
browser.cookie.get("_testa_uuid").then(function (uuid) {
if (!uuid) return;
var url =
"https://new.testa-soft.tech/api/pixel" +
"?uuid=" + encodeURIComponent(uuid) +
"&event=page_view" + // must match your goal's name
"&project=" + encodeURIComponent(TESTA_PROJECT) +
"&data=" + encodeURIComponent(
JSON.stringify({
url: event.context.document.location.href,
}),
) +
"&_t=" + Date.now();
fetch(url, { method: "GET", keepalive: true });
});
});Notes
- The
eventvalue must match the name of a custom goal on your experiment (for example,purchase). - You can subscribe to any other Shopify customer event the same way: subscribe to the event, then send it to the pixel with the matching goal name.