Skip to content

Instantly share code, notes, and snippets.

@analyticssr
Last active June 6, 2023 18:00
Show Gist options
  • Save analyticssr/aaa0aa27f44dba6880c2 to your computer and use it in GitHub Desktop.
Save analyticssr/aaa0aa27f44dba6880c2 to your computer and use it in GitHub Desktop.
Enhanced E-commerce
// information made available through the dataLayer is easily used in Google Tag Manager.
// GTM-XXXXXX has to be changed to the actual GTM-code as shown in the implementation document
dataLayer = [{
'pageTitle': 'page title variable', //example
'loggedIn': 'TRUE', //example
'CLV': '2249.50', //example (only use . seperator for cents)
'userID': 'AB12345678' //example
}];
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(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=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
<!-- End Google Tag Manager -->
<script>
// script 'product impression' wordt verstuurd zodra een product wordt getoond op webpagina.
// productweergave
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'productImpression',
'ecommerce': {
'currencyCode': 'EUR',
'impressions': [ // Array of impression Objects.
{ // Impression product 1!
'name': 'productName', // Name or ID is required, all other variables are optional. Example 'PHILIPS 22PFK4209/12'
'id': 'productId', // ID has to be unique (SKU). Example '372078934'
'category': 'productCategory', // category corresponts preferably with website level structure differentiated with '/'. Example 'televisies/LED tv'
'brand': 'productBrand', // brand name product. Example 'Philips'
'variant': 'productVariant', // product variations like color, size, location. Example 'White'
'price': 'productPrice', // price of product in local currency (euro, dollar). Example '2634.99' (use . seperator for cents, and use single quotes!)
'list': 'listType', // see documentation for used lists.
'listPage': 'listPage', // On which listpage is the list on. (just like a searchresult can have more then 1, resultpages.). Example 'listPage3'
'position': position // position of the product within the list. Example : 12 (numeric value, therefor without single quote '')
},
{ // Impression product 2!
'name': 'productName',
'id': 'productSKU',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice',
'list': 'listType',
'listPage': 'listPage',
'position': position
}]
}
});
</script>
// productObj An object representing a product.
// productlijst prestaties
<script>
window.dataLayer = window.dataLayer || [];
function(productObj) {
dataLayer.push({
'event': 'productClick',
'ecommerce': {
'click': {
'actionField': {'list': 'listType'}, // Optional list property. corresponds with product impression 'List'
'products': [{
'name': 'productName',
'id': 'productId',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice'
}]
}
},
'eventCallback': function() {
document.location = 'productUrl' // url of productpage. Example 'https://www.myshop.com/naam'
}
});
}
</script>
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'productDetail',
'ecommerce': {
'detail': {
'actionField': {'list': 'listType'}, // Optional list property.
'products': [{
'name': 'productName',
'id': 'productId',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice'
}]
}
}
});
</script>
// promotie van een artikel in bijvoorbeeld de slider op de homepage of als een actieproduct in het menu-item getoond wordt
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'promotionImpression',
'ecommerce': {
'promoView': {
'promotions': [ // Array of promotional field objects.
{
'name': 'promoName', // Example: 'slideshow'
'id': 'promoId', // get name from image name if possible. Example: 'Samsung smart tv'
'creative': 'promoCreative', // not obligatory, so may be left empty. Example: 'folderaanbieding'
'position': promoPosition // not obligatory, so may be left empty. Example: 1 (numeric value, therefor without single quote '')
}]
}
}
});
</script>
/**
* Call this function when a user clicks on a promotion. This function uses the eventCallBack
* datalayer variable to handle navigation after the ecommerce data is sent to Google Analytics.
* @param {Object} promoObj An object representing an internal site promotion.
*/
<script>
window.dataLayer = window.dataLayer || [];
function onPromoClick(promoObj) {
dataLayer.push({
'event': 'promotionClick',
'ecommerce': {
'promoClick': {
'promotions': [ // Array of promoClickObjects.
{
'id': 'promoId',
'name': 'promoNaam',
'creative': 'promoCreative',
'position': promoPosition
}]
}
},
'eventCallback': function() {
document.location = 'promoDestination'; // Destionation URL of promotion. Example: 'folder-philips-22PFK4209/12-white'
}
});
}
</script>
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'addToCart',
'ecommerce': {
'currencyCode': 'EUR',
'add': { // 'add' actionFieldObject measures.
'products': [{ // adding a product to a shopping cart.
'name': 'productName',
'id': 'productId',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice',
'quantity': productQty // Number of product items that were added to the cart. Example: '1'
}]
}
}
});
</script>
// Measure the removal of a product from a shopping cart.
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'removeFromCart',
'ecommerce': {
'remove': { // 'remove' actionFieldObject measures.
'products': [{ // removing a product to a shopping cart.
'name': 'productName',
'id': 'productId',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice',
'quantity': productQty
}]
}
}
});
</script>
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'checkout',
'ecommerce': {
'checkout': {
'actionField': {'step': 1}, // Step number within the shoppingcart funnel (numeric value, therefor without single quote '')
'products': [{
'name': 'productName',
'id': 'productId',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice',
'quantity': productQty // number of items
}]
}
}
});
</script>
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'checkoutRegister',
'ecommerce': {
'checkout': {
'actionField': {'step': 2},
'products': [{
'name': 'productName',
'id': 'productId',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice',
'quantity': productQty
}]
}
}
});
</script>
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'checkoutDelivery',
'ecommerce': {
'checkout': {
'actionField': {'step': 4},
'products': [{
'name': 'productName',
'id': 'productId',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice',
'quantity': productQty
}]
}
}
});
dataLayer.push({
'event': 'checkoutOption',
'ecommerce': {
'checkout_option': {
'actionField': {'step': 4, 'option': 'ophalen | bezorgen | reserveren' } // 1 of .. options must be filled in here (numeric value, therefor without single quote '')
}
}
});
</script>
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'checkoutPayment',
'ecommerce': {
'checkout': {
'actionField': {'step': 5},
'products': [{
'name': 'productName',
'id': 'productId',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice',
'quantity': productQty
}]
}
}
});
dataLayer.push({
'event': 'checkoutOption',
'ecommerce': {
'checkout_option': {
'actionField': {'step': 5, 'option': 'new customer | existing customer' }
}
}
});
</script>
// Send transaction data with a pageview if available
// when the page loads. Otherwise, use an event when the transaction
// data becomes available.
// productprestaties, verkoopprestaties,
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'purchase',
'ecommerce': {
'purchase': {
'actionField': {
'id': 'transId', // Transaction ID. Example: '140609-33123'
'affiliation': 'transAffiliation', // name of partner sales is attributed to. Example: 'Store Amsterdam'
'revenue': 'transRevenue', // Total transaction value, including taxes and shipping. Example: '476.21' (exeption: use single quotes!)
'tax':'transTax', // taxes. Example: '96.18'
'shipping': 'transShipping', // shipping (if applicable). Example: '4.95'
'coupon': 'transCoupon' // optional: coupons used. Example: 'zomeraanbieding'
},
'products': [{ // List of productFieldObjects.
'name': 'productName', // Name or ID is required. Example: 'PHILIPS 22PFK4209/12'
'id': 'productSKU',
'category': 'productCategory',
'brand': 'productBrand',
'variant': 'productVariant',
'price': 'productPrice',
'quantity': productQty,
'coupon': 'Coupon' // optional: coupons used for individual product. Example: 'zomeraanbieding'
}]
}
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment