E-Commerce Tracking via Google Analytic 4 #37
-
Hi all, Does anyone have experience of creating E-Commerce tracking via Google Analytic 4 with Swell? Basically tracking events such ass Add to cart, View Items and Purchase etc. I can easily implement tracking on page views with my Swell store. But not sure about e-Commerce event based tracking. Or could Swell create an article about this? I think this is very important tool to have. Regards, |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
I found the best way is to use google tag manager to send data to GA4. In your front end code, you add the GTM global script, and then you can post Here's the google documentation for how to post events to the dataLayer: https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtm So, you basically get the cart data from swell and create a dataLayer event from it. You can customize the data to post as little or as much data as you need to track. Then from within GTM, you send the data to GA4 using the GA4 tag. It takes a bit of planning - knowing what you want to end up with in the end on in GA4. Which framework are you using to run your store on? I could post some code examples. |
Beta Was this translation helpful? Give feedback.
-
Here's a helper function to post dataLayer events from swell data that I use in our site front end. This requires that you have the GTM script loaded in every page. /**
* Add an ecommerce event to the datalayer for google tag manager
* @param name - event name, e.g. 'add_to_cart'
* @param items - array of swell products or swell cart items that are included in the event
* @param value - dollar value of the event
* @param cart - pass in cart data if we want the current cart contents updated on the dataLayer
* (separate from the `ecommerce` event data)
*/
export default function gtmEcommEvent( name, items, value, cart) {
// add cart contents to dataLayer if cart object was passed in before adding event
if (cart) {
window.dataLayer.push({ cart: null })
window.dataLayer.push({
cart: {
items: cart.items.map((item) => {
return {
item_id: item.product.sku,
item_name: item.product.name,
quantity: item.quantity,
price: item.price
}
}),
checkout_url: cart.checkout_url,
value: cart.sub_total,
cart_id: cart.id
}
})
}
if (items?.length) {
window.dataLayer.push({ ecommerce: null }) // Clear the previous ecommerce object.
// check if type is swell product
if (items[0].hasOwnProperty('sku')) {
window.dataLayer.push({
event: name,
ecommerce: {
items: (items).map((product) => {
return {
item_id: product.sku,
item_name: product.name,
quantity: 1,
price: product.price
}
}),
value,
currency: 'USD'
}
})
} else {
// we know it's not a product, so it must be a cart item
window.dataLayer.push({
event: name,
ecommerce: {
items: (items).map((item) => {
return {
item_id: item.product.sku,
item_name: item.product.name,
quantity: item.quantity,
price: item.price
}
}),
value,
currency: 'USD'
}
})
}
}
} examples: import gtmEcommEvent from "utils/gtmEcommEvent"
// add to cart
gtmEcommEvent('add_to_cart', [product], product.price * quantity, cart) // cart is optional
// view cart
gtmEcommEvent('view_cart', cart.items, cart.sub_total, cart)
//begin checkout
gtmEcommEvent('begin_checkout', cart.items, cart.sub_total, cart) |
Beta Was this translation helpful? Give feedback.
-
Thank you @vettloffah |
Beta Was this translation helpful? Give feedback.
Here's a helper function to post dataLayer events from swell data that I use in our site front end. This requires that you have the GTM script loaded in every page.