1/** 2 * NOTICE OF LICENSE 3 * 4 * This source file is subject to a commercial license from SAS Comptoir du Code 5 * Use, copy, modification or distribution of this source file without written 6 * license agreement from the SAS Comptoir du Code is strictly forbidden. 7 * In order to obtain a license, please contact us: [email protected] 8 * 9 * @package cdc_googletagmanager 10 * @author Vincent - Comptoir du Code 11 * @copyright Copyright(c) 2015-2022 SAS Comptoir du Code 12 * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 13 * 14 * Project Name : Google Tag Manager Enhanced Ecommerce (UA) Tracking 15 * Created By : Comptoir du Code 16 * Created On : 2016-06-02 17 * Support : https://addons.prestashop.com/contact-community.php?id_product=23806 18 */ 19 20// CDC GTM Datalayer actions 21var cdcGtm = { 22 clearDatalayer : function() { 23 if (typeof dataLayer === 'undefined') { 24 dataLayer = []; 25 } 26 dataLayer.push({ ecommerce: null }); 27 }, 28 29 addToCart : function(product_id, attribute_id, qtity, addedFromProductPage, callerElement) { 30 if(product_id) { 31 cdcGtm.pushProductToDatalayer('cart-add', product_id, attribute_id, qtity); 32 } 33 }, 34 35 removeFromCart : function(product_id, attribute_id, qtity) { 36 if(product_id) { 37 cdcGtm.pushProductToDatalayer('cart-remove', product_id, attribute_id, qtity); 38 } 39 }, 40 41 productClick : function(product_id, attribute_id) { 42 if(product_id) { 43 cdcGtm.pushProductToDatalayer('product-click', product_id, attribute_id); 44 } 45 }, 46 47 /** 48 * Get product from ajax and push it to datalayer 49 * @param action 50 * @param product_id 51 * @param attribute_id 52 * @param qtity 53 */ 54 pushProductToDatalayer : function(action, product_id, attribute_id, qtity) { 55 56 // convert from NaN to default value 57 attribute_id = attribute_id || 0; 58 qtity = qtity || 1; 59 60 // get product async 61 var cdcgtmreq = new XMLHttpRequest(); 62 cdcgtmreq.onreadystatechange = function() { 63 if (cdcgtmreq.readyState == 4) { /* use 4 instead of XMLHttpRequest.DONE for browser compatibility */ 64 if (cdcgtmreq.status == 200) { 65 var datalayerJs = cdcgtmreq.responseText; 66 try { 67 let datalayerCartAction = JSON.parse(datalayerJs); 68 // debug log - data pushed to datalayer 69 // console.log(JSON.stringify(datalayerCartAction, undefined, 4)); 70 cdcGtm.clearDatalayer(); 71 dataLayer.push(datalayerCartAction); 72 } catch(e) { 73 console.log("[CDCGTM] error while parsing json"); 74 } 75 } 76 } 77 }; 78 if(typeof ajaxGetProductUrl !== 'undefined' && ajaxGetProductUrl) { 79 cdcgtmreq.open("GET", ajaxGetProductUrl + "?action=" + action + "&id=" + product_id + "&id_attribute=" + attribute_id + "&qtity=" + qtity, true); 80 cdcgtmreq.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); 81 cdcgtmreq.send(); 82 } else { 83 console.error("[CDCGTM] ajaxGetProductUrl not defined, cannot send action " + action); 84 } 85 }, 86 87 /** 88 * Use the current datalayer, add data to it 89 * then push it to GTM datalayer 90 * @param data 91 */ 92 /*pushDataToCurrentDatalayer : function(data) { 93 try { 94 dataLayer = dataLayer || []; 95 let baseDatalayer = cdcDatalayer || {} 96 let mergedDatalayer = {...baseDatalayer, ...data} 97 dataLayer.push(mergedDatalayer); 98 } catch(e) { 99 console.log("[CDCGTM][pushDataToCurrentDatalayer] ERROR "); 100 } 101 },*/ 102 103 handleValidateShippingStep : function($submitBtn, event, shippingName, doNotPreventClick) { 104 if(!$submitBtn.hasClass('clicked')) { 105 if(typeof doNotPreventClick === 'undefined' || !doNotPreventClick) { 106 event.preventDefault(); 107 } 108 109 // push infos to datalayer 110 try { 111 dataLayer = dataLayer || []; 112 let cloneDatalayer = JSON.parse(JSON.stringify(cdcDatalayer)) 113 cloneDatalayer.event = 'add_shipping_info'; 114 cloneDatalayer.ecommerce.shipping_tier = shippingName; 115 cdcGtm.clearDatalayer(); 116 dataLayer.push(cloneDatalayer); 117 } catch (e) { 118 console.error(e); 119 } 120 121 // exec button action after waiting to send the datalayer 122 if(typeof doNotPreventClick === 'undefined' || !doNotPreventClick) { 123 setTimeout(function () { 124 $submitBtn.addClass('clicked'); 125 $submitBtn.click(); 126 }, 500); 127 } 128 } 129 }, 130 131 handleValidatePayementStep : function(paymentName) { 132 // push infos to datalayer 133 try { 134 dataLayer = dataLayer || []; 135 let cloneDatalayer = JSON.parse(JSON.stringify(cdcDatalayer)) 136 cloneDatalayer.event = 'add_payment_info'; 137 cloneDatalayer.ecommerce.payment_type = paymentName; 138 cdcGtm.clearDatalayer(); 139 dataLayer.push(cloneDatalayer); 140 } catch (e) { 141 console.error(e); 142 } 143 } 144 145 146} 147 148/* CART : ADD / REMOVE PRODUCT ****************************************************************************************/ 149 150// Prestashop 1.5 || 1.6 151if(typeof(ajaxCart) != 'undefined') { 152 // override ajaxCart.add function 153 var ajaxCartAddFunc = ajaxCart.add; 154 ajaxCart.add = function(idProduct, idCombination, addedFromProductPage, callerElement, quantity, wishlist) { 155 ajaxCartAddFunc(idProduct, idCombination, addedFromProductPage, callerElement, quantity, wishlist); 156 cdcGtm.addToCart(idProduct, idCombination, quantity, addedFromProductPage, callerElement); 157 } 158 159 // override ajax.remove function 160 var ajaxCartRemoveFunc = ajaxCart.remove; 161 ajaxCart.remove = function(idProduct, idCombination, customizationId, idAddressDelivery) { 162 ajaxCartRemoveFunc(idProduct, idCombination, customizationId, idAddressDelivery); 163 cdcGtm.removeFromCart(idProduct, idCombination); 164 } 165} 166 167// Prestashop >= 1.7 168else if(typeof(prestashop) != 'undefined') { 169 $(document).ready(function () { 170 prestashop.on( 171 'updateCart', 172 function (event) { 173 let requestData = {}; 174 175 if (event && event.reason) { 176 requestData = { 177 id_product_attribute: event.reason.idProductAttribute, 178 id_product: event.reason.idProduct, 179 action: event.reason.linkAction 180 }; 181 182 let quantity = 1; 183 if(event.resp && event.resp.quantity) { 184 quantity = event.resp.quantity; 185 } 186 187 if(requestData.action == 'add-to-cart') { 188 cdcGtm.addToCart(requestData.id_product, requestData.id_product_attribute, quantity, null, null); 189 } else if(requestData.action == 'delete-from-cart') { 190 cdcGtm.removeFromCart(requestData.id_product, requestData.id_product_attribute, quantity); 191 } 192 } 193 } 194 ); 195 }); 196} 197 198// override deleteProductFromSummary (checkout page) 199var deleteProductFromSummary = (function(id) { 200 var original_deleteProductFromSummary = deleteProductFromSummary; 201 return function(id) { 202 var productId = 0; 203 var productAttributeId = 0; 204 var ids = 0; 205 ids = id.split('_'); 206 productId = parseInt(ids[0]); 207 if (typeof(ids[1]) !== 'undefined') { 208 productAttributeId = parseInt(ids[1]); 209 } 210 211 var cart_qtity = parseInt($('input[name=quantity_' + id + ']').val()); 212 213 cdcGtm.removeFromCart(productId, productAttributeId, cart_qtity); 214 original_deleteProductFromSummary(id); 215 } 216})(); 217 218// override downQuantity (checkout page) 219var downQuantity = (function(id, qty) { 220 var original_downQuantity = downQuantity; 221 return function(id, qty) { 222 var productId = 0; 223 var productAttributeId = 0; 224 var ids = 0; 225 ids = id.split('_'); 226 productId = parseInt(ids[0]); 227 if (typeof(ids[1]) !== 'undefined') { 228 productAttributeId = parseInt(ids[1]); 229 } 230 231 // qty 232 var val = $('input[name=quantity_' + id + ']').val(); 233 var newVal = val; 234 if(typeof(qty) == 'undefined' || !qty) 235 { 236 new_qty = 1; 237 newVal = val - 1; 238 } 239 else if (qty < 0) 240 new_qty = -qty; 241 242 // if qtity is > 0, decrease qtity, if qtity = 0, it will be handled by "deleteProductFromSummary" 243 if(newVal > 0) { 244 cdcGtm.removeFromCart(productId, productAttributeId, new_qty); 245 } 246 247 original_downQuantity(id, qty); 248 } 249})(); 250 251/* CART : CHOSE SHIPPING / PAYMENT ************************************************************************************/ 252$(document).ready(function () { 253
254 if(typeof ajaxShippingEvent !== 'undefined' && ajaxShippingEvent) { 255 // chose shipping - prestashop 1.7 256 $('#checkout').on('click', '#checkout-delivery-step button[type=submit]', function (e) { 257 let $submitBtn = $(this); 258 259 // get shipping name 260 let $selectedShipping = $submitBtn.closest('#checkout-delivery-step').find('.delivery-options input[type=radio]:checked').closest('.delivery-option'); 261 let shippingName = $selectedShipping.find('.carrier-name').text(); 262 263 cdcGtm.handleValidateShippingStep($submitBtn, e, shippingName); 264 }); 265 266 267 // chose shipping - prestashop 1.6 268 $('#order').on('click', 'button[name=processCarrier]', function(e) { 269 let $submitBtn = $(this); 270 271 // get shipping name 272 let $selectedShipping = $submitBtn.closest('form[name=carrier_area]').find('.delivery_options input[type=radio]:checked').closest('.delivery_option'); 273 let shippingName = $selectedShipping.find('td:not(.delivery_option_radio):not(.delivery_option_logo):not(.delivery_option_price)').find('strong').text(); 274 cdcGtm.handleValidateShippingStep($submitBtn, e, shippingName); 275 }); 276 } 277 278 279 if(typeof ajaxPaymentEvent !== 'undefined' && ajaxPaymentEvent) { 280 // chose payment - prestashop 1.7 281 $('#checkout').on('change', '#checkout-payment-step input[type=radio][name=payment-option]', function (e) { 282 // push infos to datalayer 283 let paymentName = $(this).closest('.payment-option').find('label').text().trim(); 284 cdcGtm.handleValidatePayementStep(paymentName); 285 }); 286 287 // chose payment - prestashop 1.6 288 $('#order').on('click', '#HOOK_PAYMENT .payment_module a', function (e, cdcGtmTriggered) { 289 let $submitBtn = $(this); 290 e.preventDefault(); 291 292 // push infos to datalayer 293 cdcGtm.handleValidatePayementStep($submitBtn.attr('title')); 294 295 // redirect to payment after waiting to send the datalayer 296 setTimeout(function () { 297 window.location.href = $submitBtn.attr("href"); 298 }, 500); 299 }); 300 } 301 302 303}); 304 305 306 307/* PRODUCT CLICK ******************************************************************************************************/ 308$(document).ready(function () { 309 310 // prestashop 1.7 311 $('article[data-id-product]').find('a').on('click', function() { 312 let $product = $(this).closest('article[data-id-product]'); 313 let idProduct = $product.data('id-product'); 314 let idProductAttribute = $product.data('id-product-attribute') | 0; 315 cdcGtm.productClick(idProduct, idProductAttribute); 316 }); 317 318 // prestashop 1.6 319 $('.ajax_block_product').find('a.product-name,a.product_img_link,a.lnk_view,a.quick-view-mobile').on('click', function(e) { 320 let $productInfos = $(this).closest('.ajax_block_product').find('.cdcgtm_product'); 321 let idProduct = $productInfos.data('id-product'); 322 let idProductAttribute = $productInfos.data('id-product-attribute') | 0; 323 cdcGtm.productClick(idProduct, idProductAttribute); 324 }); 325});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.