console.log('Initializing google analytics advanced e-commerce'); console.log('Site key is GTM-K4D3F9X'); console.log('Locale is en-US'); console.log('Setting up data layer'); window.dataLayer = window.dataLayer || []; console.log('Add event specific datalayer options for default'); console.log('Adding default information.'); window.dataLayer.push({ 'locale': 'en-US' }); console.log('Completed data layer'); function onAddedToCart(currencyCode, productArray) { var newProductArray = []; for (product in productArray) { if (productArray[product].coupon == 'undefined') { productArray[product].coupon = undefined; } } dataLayer.push({ 'event': 'Product Added', 'ecommerce': { 'currencyCode': currencyCode, // %%localeCurrency%% 'add': { 'products': productArray } } }); } function onAddedCouponAccepted(couponCode) { onAddedCouponEvent(couponCode, 'Coupon Accepted'); } function onAddedCouponRejected(couponCode) { onAddedCouponEvent(couponCode, 'Coupon Rejected'); } function onAddedCouponEvent(couponCode, event) { console.log('Coupon ' + couponCode + ' event: ' + event); window.dataLayer.push({ 'locale': 'en-US', 'event': event, 'couponCode': couponCode }); } function onRemovedFromCart(currencyCode, productArray) { console.log("removing items from cart"); window.dataLayer.push({ 'locale': 'en-US', // %%siteLocale%% 'event': 'Product Removed', 'ecommerce': { 'currencyCode': currencyCode, // %%localeCurrency%% 'remove': { 'products': productArray } } }); } function checkoutOption(step, checkoutOption) { dataLayer.push({ 'event': 'Checkout Option Selected', 'ecommerce': { 'checkout_option': { 'actionField': {'step': step, 'option': checkoutOption} } } }); } console.log('Sending information to GA'); (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-K4D3F9X');