const TRACKING_ID = “G-XXXXXXXXXX”;
const script = document.createElement(“script”);
script.setAttribute(
“src”,
`https://www.googletagmanager.com/gtag/js?id=${TRACKING_ID}`
);
script.setAttribute(“async”, “”);
document.head.appendChild(script);
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag(“js”, new Date());
gtag(“config”, TRACKING_ID, { send_page_view: false });
const Data = {
getItemsFromLineItems(lineItems) {
let items = [];
for (const item of lineItems) {
items.push({
item_id: item.variant.product.id,
item_name: item.variant.product.title,
})
}
return items;
},
getPageViewData(event) {
let actionload = event.context;
return {
page_location: actionload.document.location.href,
page_title: actionload.document.title,
language: actionload.language,
};
},
getViewItemData(event) {
return {
currency: event.data.productVariant.price.currencyCode,
value: event.data.productVariant.price.amount,
items: [
{ item_id: event.data.productVariant.id,
item_name: event.data.productVariant.product.title
}
],
};
},
getCollectionViewData(event) {
return {
collection_id: event.data.collection.id,
collection_name: event.data.collection.title,
}
},
getAddToCartData(event) {
return {
currency: event.data.cartLine.merchandise.price.currencyCode,
value: event.data.cartLine.merchandise.price.amount,
items: [
{ item_id: event.data.cartLine.merchandise.id,
item_name: event.data.cartLine.merchandise.product.title
}
],
};
},
getPaymentInfoData(event) {
return {
currency: event.data.checkout.currencyCode,
value: event.data.checkout.totalPrice.amount,
items: this.getItemsFromLineItems(event.data.checkout.lineItems),
};
},
getCheckoutData(event) {
return {
currency: event.data.checkout.currencyCode,
value: event.data.checkout.totalPrice.amount,
items: this.getItemsFromLineItems(event.data.checkout.lineItems),
};
},
getCheckoutCompleteData(event) {
return {
transaction_id: event.data.checkout.order.id,
currency: event.data.checkout.currencyCode,
value: event.data.checkout.totalPrice.amount,
items: this.getItemsFromLineItems(event.data.checkout.lineItems),
};
},
};
analytics.subscribe(“page_viewed”, async (event) => {
gtag(“event”, “page_view”, Data.getPageViewData(event));
});
analytics.subscribe(“product_viewed”, async (event) => {
gtag(“event”, “view_item”, Data.getViewItemData(event));
});
analytics.subscribe(“collection_viewed”, async (event) => {
gtag(“event”, “collection_view”, Data.getCollectionViewData(event));
});
analytics.subscribe(“search_submitted”, async (event) => {
gtag(“event”, “search”, {
search_term: event.data.searchResult.query,
});
});
analytics.subscribe(“product_added_to_cart”, async (event) => {
gtag(“event”, “add_to_cart”, Data.getAddToCartData(event));
});
analytics.subscribe(“payment_info_submitted”, async (event) => {
gtag(“event”, “add_payment_info”, Data.getPaymentInfoData(event));
});
analytics.subscribe(“checkout_started”, async (event) => {
gtag(“event”, “begin_checkout”, Data.getCheckoutData(event));
});
analytics.subscribe(“checkout_completed”, async (event) => {
gtag(“event”, “purchase”, Data.getCheckoutCompleteData(event));
});