Skip to content

Instantly share code, notes, and snippets.

@kmclaugh
Created April 1, 2021 16:09
Show Gist options
  • Save kmclaugh/1d1f3b4226e0322fa70ef8d64605b492 to your computer and use it in GitHub Desktop.
Save kmclaugh/1d1f3b4226e0322fa70ef8d64605b492 to your computer and use it in GitHub Desktop.
Workwear Upsells Datalayer

The code below needs to be added/updated to the current Datalayer Events. The goal of the update is to be able to determine when a product is added to the cart via an upsell and which items in the purchase come were added via the upsell.

here's a video description

/* add an add to cart event when the user adds an upsell
* action: add to cart
* description: fire when the user adds the upsell to the cart.
* required fields: product.id, product.name, upsell:true
* reference: https://www.simoahava.com/analytics/enhanced-ecommerce-guide-for-google-tag-manager/#add-to-cart
* notes: the products list should include ONLY the product that was added to the cart, not the entire cart
*/
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "eec.add",
eecEventName: "Add to Cart",
ecommerce: {
add: {
actionField: {
list: "{{collection.title}} || cart", // 'fall collection' // Where the add to cart happened. use 'cart' if the removal happened on the cart page
},
products: [
{
// built-in enhanced ecommerce fields:
id: "{{ product.id }}", // 'P12345' // use product id not variant id
name: "{{product.title}}", // 'awesome t-shirt'
quantity: "{{product.quantity}}", // 1 // the quantity ADDED to the cart (not the quantity in the cart)
brand: "{{shop.name}}", // my-store // if there are multiple brands use the brand variable here
price: '{{product.price | money_without_currency | remove: ","}}', // 10.0 // make sure the price is a number ie 10.00 not $10.00
variant: "{{product.selected_variant.variant.name}}", // 'large - yellow'
category: "{{product.taxonomy}}", // 'clothes/shirts/t-shirts' // max five levels of hierarchy
upsell: true, // upsell should always be true for the upsell add to cart event
// requires product-scoped custom dimensions:
sku: "{{product.selected_variant.sku}}",
description:
"{{product.description | strip_newlines | strip_html | escape }}",
imageURL: "https:{{product.featured_image.src|img_url:'grande'}}",
productURL: "{{shop.secure_url}}{{product.url}}",
comparePrice:
"{{product.compare_at_price_max|money_without_currency}}", // 15.00
categories: '{{product.collections|map:"title"|json}}', // ['mens', 't-shirts']
currentCategory: "{{collection.title}}", // 't-shirts'
variantId: "{{product.selected_variant.variant.id}}", // 'l101'
},
],
},
},
});
/* update the checkout code to include a upsell dimension on each product
* action: check out
* description: fire for each step of the checkout process (ie view cart, enter shipping info, payment)
* required fields: actionField.step, product.id, product.name, product.upsell
* reference: https://www.simoahava.com/analytics/enhanced-ecommerce-guide-for-google-tag-manager/#checkout
*/
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "eec.checkout",
eecEventName: "Checkout {{checkout.step}}",
ecommerce: {
checkout: {
actionField: {
step: "{{checkout.step}}", // 1 // The checkout step (starting at 1).
option: "{{checkout.payment_method}}", // Visa // extra metadata (e.g. shipping or payment method)
},
products: [
{
// built-in enhanced ecommerce fields:
id: "{{ product.id }}", // 'P12345' // use product id not variant id
name: "{{product.title}}", // 'awesome t-shirt'
quantity: "{{product.quantity}}", // 1 // the quantity REMOVED from the cart (not the quantity remaining in the cart)
brand: "{{shop.name}}", // my-store // if there are multiple brands use the brand variable here
price: '{{product.price | money_without_currency | remove: ","}}', // 10.0 // make sure the price is a number ie 10.00 not $10.00
variant: "{{product.selected_variant.variant.name}}", // 'large - yellow'
category: "{{product.taxonomy}}", // 'clothes/shirts/t-shirts' // max five levels of hierarchy
upsell: "{{product.added_via_upsell}}", // whether or not the user add the product via an upsell
// requires product-scoped custom dimensions:
sku: "{{product.selected_variant.sku}}",
description:
"{{product.description | strip_newlines | strip_html | escape }}",
imageURL: "https:{{product.featured_image.src|img_url:'grande'}}",
productURL: "{{shop.secure_url}}{{product.url}}",
comparePrice:
"{{product.compare_at_price_max|money_without_currency}}", // 15.00
categories: '{{product.collections|map:"title"|json}}', // ['mens', 't-shirts']
currentCategory: "{{collection.title}}", // 't-shirts'
variantId: "{{product.selected_variant.variant.id}}", // 'l101'
},
],
},
},
});
/* update the purchase code to include a upsell dimension on each product
* action: purchase
* description: fire when the user actually completes the purchase
* required fields: actionField.id, actionField.revenue , product.id, product.name, , product.upsell, product.quantity, product.price, product.variant (if applicable)
* reference: https://www.simoahava.com/analytics/enhanced-ecommerce-guide-for-google-tag-manager/#purchase
*/
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "eec.purchase",
eecEventName: "Purchase",
ecommerce: {
currencyCode: "{{checkout.current_code}}", // USD // the currency of the checkout
purchase: {
actionField: {
id: "{{checkout.order_number}}", // 12345 // The order number or transaction id
affiliation: "{{shop.name}}", // my shop // Where the order took place (ie your store)
revenue:
'{{checkout.total_price | money_without_currency| remove: ","}}', // 11.00 // the total transaction value
tax: '{{checkout.tax_price | money_without_currency| remove: ","}}', // 1.0 // the tax paid
shipping:
'{{checkout.shipping_price | money_without_currency| remove: ","}}', // 2.0 // cost of shipping
coupon: "{{discount.code}}", // SUMMER2020 // the discount code used
},
products: [
{
// built-in enhanced ecommerce fields:
id: "{{ product.id }}", // 'P12345' // use product id not variant id
name: "{{product.title}}", // 'awesome t-shirt'
quantity: "{{product.quantity}}", // 1 // the quantity REMOVED from the cart (not the quantity remaining in the cart)
brand: "{{shop.name}}", // my-store // if there are multiple brands use the brand variable here
price: '{{product.price | money_without_currency | remove: ","}}', // 10.0 // make sure the price is a number ie 10.00 not $10.00
variant: "{{product.selected_variant.variant.name}}", // 'large - yellow'
category: "{{product.taxonomy}}", // 'clothes/shirts/t-shirts' // max five levels of hierarchy
upsell: "{{product.added_via_upsell}}", // whether or not the user add the product via an upsell
// requires product-scoped custom dimensions:
sku: "{{product.selected_variant.sku}}",
description:
"{{product.description | strip_newlines | strip_html | escape }}",
imageURL: "https:{{product.featured_image.src|img_url:'grande'}}",
productURL: "{{shop.secure_url}}{{product.url}}",
comparePrice:
"{{product.compare_at_price_max|money_without_currency}}", // 15.00
categories: '{{product.collections|map:"title"|json}}', // ['mens', 't-shirts']
currentCategory: "{{collection.title}}", // 't-shirts'
variantId: "{{product.selected_variant.variant.id}}", // 'l101'
},
],
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment