/** * NOTICE OF LICENSE * * This source file is subject to the Academic Free License version 3.0 * that is bundled with this package in the file LICENSE.txt * It is also available through the world-wide-web at this URL: * https://opensource.org/licenses/AFL-3.0 * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade this module to a newer * versions in the future. If you wish to customize this module for your needs * please refer to CustomizationPolicy.txt file inside our module for more information. * * @author Webkul IN * @copyright Since 2010 Webkul * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ (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',googletagID); window.dataLayer = window.dataLayer || []; if(typeof trackingID != 'undefined'){ window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', trackingID); } $(document).ready(function(){ wk_new_cart_qty = 1; if (search_product_event_name) { $(document).ajaxComplete(function(event, xhr, settings) { if (typeof settings.data !== 'undefined') { var ajax_data = settings.data; var urlParams = new URLSearchParams(ajax_data); var ajax_url = settings.url; var search_url = ajax_url.match('/search'); var search_data = ajax_data.startsWith('s='); if (search_data && (typeof search_url !== 'undefined') && (search_url !== null)) { search_term = urlParams.get('s'); if ((typeof search_term !== 'undefined') && (search_term !== null)) { dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "search", ecommerce: { 'search_term': search_term } }); } } else { var add_to_cart_url = ajax_data.match('action=update'); if (add_to_cart_event_name && (typeof add_to_cart_url !== 'undefined') && (add_to_cart_url !== null)) { quantity = getParameterValues('qty', ajax_data); wk_new_cart_qty = quantity; } } } }); } function getParameterValues(param, ajax_data) { var url = ajax_data.slice(ajax_data.indexOf('?') + 1).split('&'); for (var i = 0; i < url.length; i++) { var urlparam = url[i].split('='); if (urlparam[0] == param) { return urlparam[1]; } } } setTimeout(function(){ $('.__TAG_ASSISTANT_BADGE').hide(); },500) if(controller_name == 'orderconfirmation' && purchase_order_event_name){ $.ajax({ url: wk_gtm_controller_link, method: 'POST', data: { ajax: 1, id_order: orderID, wkStaticToken: wkStaticToken, action: 'getDataForOrder' }, success:function(data){ result = JSON.parse(data); products1 = new Array(); $.each(result['products'],function(i,val){ productdata = { item_id: val.product_id, item_name: val.product_name, affiliation: shop_name, coupon: val.coupon, currency: result.currency, discount: val.discount, index: i, item_brand: val.item_brand, item_category: val.category, item_list_id: val.id_category, item_list_name: val.category, item_variant: val.variant, price: parseFloat(val.price), quantity: val.quantity } products1.push(productdata); }); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "purchase", ecommerce: { transaction_id: orderID, affiliation: shop_name, value: parseFloat(result.total_cart_price), tax: parseFloat(result.total_tax), shipping: parseFloat(result.shipping_price), currency: result.currency, coupon: result.coupon, items: products1 } }); } }); } else if (controller_name == 'product' && view_product_event_name) { dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "view_item", ecommerce: { items: [ { item_id: productID, item_name: productName, affiliation: shop_name, coupon: wkProductCoupon, currency: wkCurrencyCode, discount: wkProductDiscount, index: 0, item_brand: productBrandName, item_category: productCategory, item_list_id: productCategoryId, item_list_name: productCategory, item_variant: productVariant, price: productPrice, quantity: 1 } ] } }); } else if (controller_name == 'category' && view_category_event_name && wk_category_products) { products1 = new Array(); $.each(wk_category_products,function(i,val) { productdata = { item_id: val.product_id, item_name: val.product_name, affiliation: shop_name, coupon: val.coupon, currency: wkCurrencyCode, discount: val.discount, index: i, item_brand: val.item_brand, item_category: val.category, item_list_id: id_category, item_list_name: category_name, item_variant: val.variant, price: parseFloat(val.price), quantity: val.quantity } products1.push(productdata); }); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. window.dataLayer.push({ 'Page': 'Category', 'event': 'view_item_list', 'ecommerce': { item_list_id: id_category, item_list_name: category_name, items: products1 }, }); } else if(controller_name == 'cart' && view_cart_event_name) { $.ajax({ url: wk_gtm_controller_link, method: 'POST', data: { ajax: 1, wkStaticToken: wkStaticToken, action: 'getAllProductData' }, success:function(data) { result = JSON.parse(data); products1 = new Array(); $.each(result['products'],function(i,val) { productdata = { item_id: val.product_id, item_name: val.product_name, affiliation: shop_name, coupon: val.coupon, currency: result.currency, discount: val.discount, index: i, item_brand: val.item_brand, item_category: val.category, item_list_id: val.id_category, item_list_name: val.category, item_variant: val.variant, price: parseFloat(val.price), quantity: val.quantity } products1.push(productdata); }); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "view_cart", ecommerce: { currency: result.currency, value: result.cart_value, items: products1 } }); } }); } else if ((typeof wk_listing_products !== 'undefined') && wk_listing_products && gtm_product_listing_event_name && wk_page_name) { products1 = new Array(); $.each(wk_listing_products,function(i,val) { productdata = { item_id: val.product_id, item_name: val.product_name, affiliation: shop_name, coupon: val.coupon, currency: wkCurrencyCode, discount: val.discount, index: i, item_brand: val.item_brand, item_category: val.category, item_list_id: wk_page_name, item_list_name: wk_page_name, item_variant: val.variant, price: parseFloat(val.price), quantity: val.quantity } products1.push(productdata); }); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. window.dataLayer.push({ 'Page': wk_page_name, 'event': 'view_item_list', 'ecommerce': { item_list_id: wk_page_name, item_list_name: wk_page_name, items: products1 }, }); } $('#login-form').submit(function() { if ($(this).attr('id') == 'login-form' && customer_login_event_name) { var email = $('[name="email"').val(); localStorage.setItem("gtm_email", email); } }); if(id_customer && (getCookie("gtm_email") != null && getCookie("gtm_email") != 0)) { var wk_gtm_email = getCookie("gtm_email"); window.dataLayer = window.dataLayer || []; $.ajax({ url: wk_gtm_controller_link, data: { ajax:1, action:'getCustomerDetails', wkStaticToken: wkStaticToken, email: wk_gtm_email }, success: function(data) { result = JSON.parse(data) window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'Page': 'My Account', 'page_type': 'Customer', 'event': 'customerLogin', 'Customer Information': { 'Customer ID': result.id_customer, 'First Name': result.fname, 'Last Name': result.lname, 'Email': result.email, }, }); localStorage.setItem("gtm_email", 0); } }); } function getCookie(cname) { return localStorage.getItem(cname); } if (wk_signup_gtm_email && customer_signup_event_name) { $.ajax({ url: wk_gtm_controller_link, data: { ajax:1, action:'getCustomerDetails', wkStaticToken: wkStaticToken, email: wk_signup_gtm_email }, success: function(data) { result = JSON.parse(data) window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'Page': 'My Account', 'page_type': 'Customer', 'event': 'customerLogin', 'Customer Information': { 'Customer ID': result.id_customer, 'First Name': result.fname, 'Last Name': result.lname, 'Email': result.email, }, }); } }); } $('.checkout.js-cart-detailed-actions a.btn').click(function(e) { if(controller_name == 'cart' && begin_checkout_event_name) { e.preventDefault(); var url = $(this).attr('href'); $.ajax({ url: wk_gtm_controller_link, method: 'POST', data: { ajax: 1, wkStaticToken: wkStaticToken, action: 'getAllProductData' }, success:function(data) { result = JSON.parse(data); products1 = new Array(); $.each(result['products'],function(i,val) { productdata = { item_id: val.product_id, item_name: val.product_name, affiliation: shop_name, coupon: val.coupon, currency: result.currency, discount: val.discount, index: i, item_brand: val.item_brand, item_category: val.category, item_list_id: val.id_category, item_list_name: val.category, item_variant: val.variant, price: parseFloat(val.price), quantity: val.quantity } products1.push(productdata); }); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "begin_checkout", ecommerce: { items: products1 }, 'eventCallback': function() { window.location.href = url; } }); } }); } }); $('.thumbnail.product-thumbnail').click(function(e) { if ((controller_name == 'category' || (typeof wk_page_name != undefined && wk_page_name)) && select_product_event_name) { e.preventDefault(); var url = $(this).attr('href'); var product_id = $(this).parent().closest('.product-miniature').data('id-product'); var product_attr_id = $(this).parent().closest('.product-miniature').data('id-product-attribute'); $.ajax({ url: wk_gtm_controller_link, method: 'POST', data: { ajax: 1, wkStaticToken: wkStaticToken, action: 'getProductData', id_product: parseInt(product_id), id_product_attribute: parseInt(product_attr_id) }, success:function(data) { result = JSON.parse(data); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "select_item", ecommerce: { items: [ { item_id: result.product_id, item_name: result.product_name, affiliation: result.shop_name, coupon: result.coupon, currency: result.currency, discount: result.discount, index: 0, item_brand: result.item_brand, item_category: result.category, item_list_id: result.item_list_id, item_list_name: category_name, item_variant: result.variant, price: result.price, quantity: result.quantity } ] }, 'eventCallback': function() { window.location.href = url; } }); } }); } }); if (wk_opc_enabled) { $(document).on('change', '.wk-shipping-carriers [type="radio"]', function () { wkGTMShippingInfo(); }); $(document).on('change', 'input[name=payment-option]', function () { wkGTMPaymentInfo(); }); } else { $('[name="confirmDeliveryOption"]').click(function() { wkGTMShippingInfo(); }); $('#payment-confirmation [type="submit"]').click(function() { wkGTMPaymentInfo(); }); } if (typeof prestashop !== 'undefined') { if (view_product_event_name) { prestashop.on('updatedProduct', function(event){ var idProduct = $('#product_page_product_id').val(); var idProductAttribute = event.id_product_attribute; $.ajax({ url: wk_gtm_controller_link, method: 'POST', data: { ajax: 1, wkStaticToken: wkStaticToken, action: 'getProductData', id_product: parseInt(idProduct), id_product_attribute: parseInt(idProductAttribute) }, success:function(data) { result = JSON.parse(data); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "view_item", ecommerce: { items: [ { item_id: result.product_id, item_name: result.product_name, affiliation: result.shop_name, coupon: result.coupon, currency: result.currency, discount: result.discount, index: 0, item_brand: result.item_brand, item_category: result.category, item_list_id: result.item_list_id, item_list_name: category_name, item_variant: result.variant, price: result.price, quantity: 1 } ] } }); } }); }); } prestashop.on("updateCart", function(event) { var response = event.resp; if (add_to_cart_event_name && (typeof response.id_product !== 'undefined') && (response.id_product > 0) && (typeof response.quantity !== 'undefined') && (response.quantity > 0)) { if (parseInt(wk_new_cart_qty) > 0) { qty = parseInt(wk_new_cart_qty); } else { qty = parseInt(response.quantity) } $.ajax({ url: wk_gtm_controller_link, method: 'POST', data: { ajax: 1, wkStaticToken: wkStaticToken, action: 'getProductData', id_product: parseInt(response.id_product), id_product_attribute: parseInt(response.id_product_attribute), quantity: qty }, success:function(data){ result = JSON.parse(data); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "add_to_cart", currency: result.currency, value: result.product_value, ecommerce: { items: [ { item_id: result.product_id, item_name: result.product_name, affiliation: result.shop_name, coupon: result.coupon, discount: result.discount, index: 0, item_brand: result.item_brand, item_category: result.category, item_list_id: result.item_list_id, item_list_name: result.item_list_name, item_variant: result.variant, price: result.price, quantity: qty } ] } }); } }); } else if (remove_from_cart_event_name && (typeof response.id_product !== 'undefined') && (response.id_product > 0) && (typeof response.quantity !== 'undefined') && (response.quantity <= 0)) { $.ajax({ url: wk_gtm_controller_link, method: 'POST', data: { ajax: 1, wkStaticToken: wkStaticToken, action: 'getProductData', id_product: parseInt(response.id_product), id_product_attribute: parseInt(response.id_product_attribute) }, success:function(data){ result = JSON.parse(data); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "remove_from_cart", ecommerce: { currency: result.currency, value: result.product_value, items: [ { item_id: result.product_id, item_name: result.product_name, affiliation: result.shop_name, coupon: result.coupon, currency: result.currency, discount: result.discount, index: 0, item_brand: result.item_brand, item_category: result.category, item_list_id: result.item_list_id, item_list_name: result.item_list_name, item_variant: result.variant, price: result.price, quantity: 0 } ] } }); } }); } }); prestashop.on('clickQuickView', function(event){ var idProduct = event.dataset.idProduct; var idProductAttribute = event.dataset.id_product_attribute; $.ajax({ url: wk_gtm_controller_link, method: 'POST', data: { ajax: 1, wkStaticToken: wkStaticToken, action: 'getProductData', id_product: parseInt(idProduct), id_product_attribute: parseInt(idProductAttribute) }, success:function(data) { result = JSON.parse(data); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "view_item", ecommerce: { items: [ { item_id: result.product_id, item_name: result.product_name, affiliation: result.shop_name, coupon: result.coupon, currency: result.currency, discount: result.discount, index: 0, item_brand: result.item_brand, item_category: result.category, item_list_id: result.item_list_id, item_list_name: category_name, item_variant: result.variant, price: result.price, quantity: 1 } ] } }); } }); }); } }); function wkGTMShippingInfo(){ if (controller_name == 'order' && add_shipping_info_event_name) { var formElement = $(this); if (wk_opc_enabled) { var shipping_id = parseInt($('.wk-shipping-carriers [type="radio"]:checked').val()); var shipping_name = $('.wk-selected-shipping-'+ shipping_id +' .carrier-name').text().trim(); } else { var shipping_id = parseInt($('.delivery-options [type="radio"]:checked').val()); var shipping_name = $('.delivery-option-'+ shipping_id +' .carriere-name-container').text().trim(); } $.ajax({ url: wk_gtm_controller_link, method: 'POST', async: false, data: { ajax: 1, wkStaticToken: wkStaticToken, action: 'getAllProductData' }, success:function(data) { result = JSON.parse(data); products1 = new Array(); $.each(result['products'],function(i,val) { productdata = { item_id: val.product_id, item_name: val.product_name, affiliation: shop_name, coupon: val.coupon, currency: result.currency, discount: val.discount, index: i, item_brand: val.item_brand, item_category: val.category, item_list_id: val.id_category, item_list_name: val.category, item_variant: val.variant, price: parseFloat(val.price), quantity: val.quantity } products1.push(productdata); }); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "add_shipping_info", currency: result.currency, value: result.cart_value, coupon: result.coupon, shipping_tier: shipping_name, ecommerce: { items: products1 }, 'eventCallback': function() { formElement.submit(); } }); } }); } } function wkGTMPaymentInfo(){ if (controller_name == 'order' && add_payment_info_event_name) { var formElement = $(this); if (wk_opc_enabled) { var payment_div_id = $('[name="payment-option"]:checked').attr('id'); var wk_payment_type = $('.wk-selected-payment-'+payment_div_id+' p').text().trim(); } else { var payment_div_id = $('[name="payment-option"]:checked').parent().closest('.payment-option').attr('id'); var wk_payment_type = $('#'+payment_div_id+' label span').text().trim(); } $.ajax({ url: wk_gtm_controller_link, method: 'POST', data: { ajax: 1, wkStaticToken: wkStaticToken, action: 'getAllProductData' }, success:function(data) { result = JSON.parse(data); products1 = new Array(); $.each(result['products'],function(i,val) { productdata = { item_id: val.product_id, item_name: val.product_name, affiliation: shop_name, coupon: val.coupon, currency: result.currency, discount: val.discount, index: i, item_brand: val.item_brand, item_category: val.category, item_list_id: val.id_category, item_list_name: val.category, item_variant: val.variant, price: parseFloat(val.price), quantity: val.quantity } products1.push(productdata); }); dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. dataLayer.push({ event: "add_payment_info", currency: result.currency, value: result.cart_value, coupon: result.coupon, payment_type: wk_payment_type, ecommerce: { items: products1 }, 'eventCallback': function() { formElement.submit(); } }); } }); } }