Skip to content

Instantly share code, notes, and snippets.

@webhasan
Created April 6, 2024 03:39
Show Gist options
  • Save webhasan/a6c128f990504012a1e58fe7cd1aeafa to your computer and use it in GitHub Desktop.
Save webhasan/a6c128f990504012a1e58fe7cd1aeafa to your computer and use it in GitHub Desktop.
Shopify Pixel Events (Assignment)
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-K9W894CT');
/**
* View Cart
*/
analytics.subscribe("cart_viewed", function(event) {
var item_value = event.data.cart.cost.totalAmount.amount;
var currencyCode = event.data.cart.cost.totalAmount.currencyCode;
var lineItems = event.data.cart.lines;
var items = [];
lineItems.forEach(function(lineItem) {
var id = lineItem.merchandise.product.id
var title = lineItem.merchandise.product.title;
var price = lineItem.merchandise.price.amount;
var quantity = lineItem.quantity;
var item = {
item_id: id,
item_name: title,
price: price,
quantity: quantity,
}
items.push(item);
})
var ecommerce = {
value: item_value,
currency: currencyCode,
items: items
};
var viewCartData = {
event: "view_cart",
ecommerce: ecommerce,
};
dataLayer.push(viewCartData);
console.log('Event: view_cart', viewCartData);
});
/**
* Todo: Complete Purchse Event
*/
analytics.subscribe('checkout_completed', function(event) {
console.log('purchase event', event);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment