1angular.module('AboShopApp').factory('googleTagManager', ['commonConfig', '$window', 'Auth', '$rootScope', '$location', 2 function (commonConfig, $window, Auth, $rootScope, $location) { 3 let factory = {}; 4 factory.eventsTab = []; 5 let dataLayerVpv = []; 6 factory.gtmIsReady = false; 7 factory.viewEventIsReady = false; 8 factory.init = function init() { 9 if (commonConfig.googletagmanagerid !== undefined) { 10 console.log("init googletagmanager"); 11 !function (w,d,s,l,i) { 12 w[l] = w[l] || []; 13 w[l].push({ 14 'gtm.start': new Date().getTime(), 15 event: 'gtm.js' 16 }); 17 let f = d.getElementsByTagName(s)[0], 18 j = d.createElement(s), 19 dl = l != 'dataLayer' ? '&l=' + l : ''; 20 j.async = true; 21 j.src = ''; 22 if (commonConfig.googleTagManagerCustomUrl !== undefined) { 23 j.src = commonConfig.googleTagManagerCustomUrl + i.replace(/^GTM-/, '') + dl; 24 } else { 25 j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl; 26 } 27 console.log(j.src); 28 29 // Attach handlers for all browsers 30 j.onload = j.onreadystatechange = function afterLoad() { 31 if ( !factory.gtmIsReady && (!this.readyState || 32 this.readyState === "loaded" || this.readyState === "complete") ) { 33 console.log("gtm is ready"); 34 factory.gtmIsReady = true; 35 // Handle memory leak in IE 36 j.onload = j.onreadystatechange = null; 37 factory.tryRealPush(); 38 } 39 }; 40 41 f.parentNode.insertBefore(j, f); 42 } (window, document, 'script', commonConfig.googletagmanagerdatalayer, commonConfig.googletagmanagerid); 43 } 44 }; 45 46 $rootScope.$watch(function() { 47 return $location.path(); 48 }, 49 function(a){ 50 if (commonConfig.googleTagManagerGA4 && window.dataLayer !== undefined) { 51 // Nettoyage du dataLayer au changement de page 52 window.dataLayer.push({ 'ecommerce': null, 'coupon': null, 'discount': null, 'actionField': null }); // Clear the previous objects 53 } 54 if (commonConfig.marfeelTag) { 55 window.marfeel = window.marfeel || { cmd: [] }; 56 window.marfeel.cmd.push(['compass', function(compass) { 57 compass.trackNewPage(); 58 }]); 59 } 60 }); 61 62 factory.push = function push(value, forceOld) { 63 if (commonConfig.gaConsent && commonConfig.googletagmanagerid !== undefined) { 64 console.log("send to googletagmanager"); 65 factory.eventsTab.push(function () { 66 if (!commonConfig.googleTagManagerGA4) { 67 console.log("reset datalayer"); 68 $window[commonConfig.googletagmanagerdatalayer].reset(); 69 } 70 }); 71 factory.eventsTab.push(value); 72 factory.tryRealPush(); 73 } 74 }; 75 76 factory.tryRealPush = function tryRealPush() { 77 if(factory.gtmIsReady && factory.viewEventIsReady) { 78 $window[commonConfig.googletagmanagerdatalayer] = $window[commonConfig.googletagmanagerdatalayer] || []; 79 while(factory.eventsTab.length){ 80 let elem = factory.eventsTab.shift(); 81 console.log(elem); 82 $window[commonConfig.googletagmanagerdatalayer].push(elem); 83 } 84 } 85 }; 86 87 factory.pushVpv = function pushVpv(value) { 88 if (commonConfig.gaConsent) { 89 console.log("send to googletagmanager"); 90 if (commonConfig.googletagmanagerOld && commonConfig.googletagmanagerid !== undefined) { 91 dataLayerVpv.push(value); 92 } 93 } 94 }; 95 factory.sendVpv = function sendVpv() { 96 if (commonConfig.gaConsent && commonConfig.googletagmanagerOld && commonConfig.googletagmanagerid !== undefined) { 97 console.log("send to googletagmanager"); 98 let data = { 'event': 'vpv' }; 99 for(let i = 0; i < dataLayerVpv.length; ++i) { 100 if(dataLayerVpv[i] != null) { 101 for (let prop in dataLayerVpv[i]) { 102 if (prop !== 'event') { 103 data[prop] = dataLayerVpv[i][prop]; 104 } 105 } 106 } 107 } 108 factory.push(data, true); 109 dataLayerVpv = []; 110 } 111 }; 112 113 114 factory.pageCategory = null; 115 let _checkoutStep = null; 116 factory.checkoutStep = function checkoutStep(step){ 117 factory.pushVpv({'checkout_step': step}); 118 _checkoutStep = step; 119 }; 120 121 factory.createAccountEvent = function createAccountEvent(getCustomerFn) { 122 _customerInfos('CREATE_ACCOUNT', getCustomerFn);
123 } 124 factory.subscribeToNewsletterEvent = function subscribeToNewsletterEvent(getCustomerFn) { 125 _customerInfos('SUBSCRIBE_TO_NEWSLETTER', getCustomerFn); 126 } 127 factory.userInfosEvent = function userInfosEvent(getCustomerFn) { 128 _customerInfos('USER_INFORMATIONS', getCustomerFn); 129 } 130 factory.changePageInProgress = function changePageInProgress() { 131 console.log("changePageInProgress"); 132 factory.viewEventIsReady = false; 133 } 134 factory.pageViewEvent = function pageViewEvent(pageTitle) { 135 //Levent pageView doit etre le premier envoyé avant tous les autres 136 //On force donc lajout dans le tableau en premier 137 factory.eventsTab.unshift({'event': 'PAGE_VIEW', 138 'TITRE_PAGE': pageTitle, 139 'CONTENT_SITE': 'boutique', 140 'PAGE_TECHNOLOGY': 'html', 141 'PAGE_CATEGORY': factory.pageCategory 142 }); 143 factory.eventsTab.unshift(function () { 144 if (!commonConfig.googleTagManagerGA4) { 145 console.log("reset datalayer"); 146 this.reset(); 147 } 148 }); 149 factory.viewEventIsReady = true; 150 factory.tryRealPush(); 151 }; 152 factory.clickCta = function clickCta(name, category) { 153 factory.push({'event': 'CLICK_CTA', 154 'CTA_NAME': name, 155 'CTA_CATEGORY': category 156 }); 157 } 158 factory.viewItemListEvent = function viewItemListEvent(produits, listName, listId) { 159 if (produits.length > 0) { 160 factory.push(_ecommerceEvent('VIEW_ITEM_LIST', produits, listName, listId)); 161 } 162 }; 163 factory.viewItemEvent = function viewItemEvent(produit) { 164 if (factory.pageCategory === 'product') { 165 factory.push(_ecommerceEvent('VIEW_ITEM', [produit])); 166 } 167 }; 168 factory.addToCartEvent = function addToCartEvent(produit, totalTtc, orderId) { 169 let actionField = { 170 'transaction_id': orderId 171 }; 172 factory.push(_ecommerceEvent('ADD_TO_CART', [produit], null, null, actionField, totalTtc)); 173 }; 174 factory.checkoutEvent = function checkoutEvent(lignes, totalTtc, orderId) { 175 if (_checkoutStep != null) { 176 let actionField = { 177 'transaction_id': orderId, 178 'step': _checkoutStep 179 }; 180 factory.push(_ecommerceEvent('CHECKOUT', _extractProduct(lignes), undefined, undefined, actionField, totalTtc)); 181 } 182 }; 183 factory.transactionEvent = function transactionEvent(transaction, lignes) { 184 let actionField = { 185 'transaction_id': transaction.transaction_id, 186 'coupon': transaction.transaction_coupon, 187 'tax': transaction.transaction_tax, 188 'shipping': transaction.transaction_shipping, 189 'revenue': transaction.transaction_revenue 190 }; 191 factory.push(_ecommerceEvent('ECOMMERCE_PURCHASE', _extractProduct(lignes), undefined, undefined, actionField)); 192 }; 193 194 function _customerInfos(gtmEvent, getCustomerFn) { 195 if (commonConfig.googletagmanagerid !== undefined && !commonConfig.googletagmanagerOld) { 196 if (Auth.isClientConnecte()) { 197 getCustomerFn().then(function(customer) { 198 factory.push({'event': gtmEvent, 199 'ID_SSO': undefined, 200 'USER_TYPE': (customer.estAbonne) ? 'abonné' : 'inscrit', 201 'ID_ABOW': customer.codeClient, 202 'USER_AGENT': $window.navigator.userAgent 203 }); 204 }); 205 } else { 206 factory.push({'event': gtmEvent, 207 'ID_SSO': undefined, 208 'USER_TYPE': 'anonyme', 209 'ID_ABOW': undefined, 210 'USER_AGENT': $window.navigator.userAgent 211 }); 212 } 213 } 214 } 215 function _extractProduct(lignes) { 216 let produits = []; 217 for (let i = 0; i < lignes.length; ++i) { 218 produits.push(lignes[i].produit); 219 } 220 return produits; 221 } 222 let _ecommerceItems = {}; 223 let _productsInfosCart = {}; 224 function _ecommerceEvent(gtmEvent, produits, listName, listId, actionField, totalTtc) { 225 let event = { 226 'event': gtmEvent, 227 'ecommerce': {'items': []} 228 }; 229 if (gtmEvent === 'ADD_TO_CART' || gtmEvent === 'CHECKOUT') { 230 event.ecommerce.cart_value = totalTtc.replace(',', '.'); 231 } 232 if (actionField != null) { 233 event['actionField'] = actionField; 234 } 235 236 for (let i = 0; i < produits.length; ++i) { 237 let produit = produits[i]; 238 let category, index, brand; 239 240 // type de produit 241 if (produit.estUnAbonnement) { 242 category = 'abonnement'; 243 } else if (produit.estUneFormule) { 244 category = 'formule'; 245 } else if (produit.estUnArticleLibre) { 246 category = 'article libre'; 247 } else if (produit.estUnDon) { 248 category = 'don'; 249 } else if (produit.estUnProduitExterne) { 250 category = 'externe'; 251 } 252 253 // marque du produit : selon le profil, sinon selon le titre 254 if ($rootScope.profil){ 255 brand = $rootScope.profil.toLowerCase(); 256 } else if (produit.titre) { 257 brand = produit.titre.titreAbrege.toLowerCase(); 258 } 259 260 // infos sur la liste d'origine du produit 261 if (gtmEvent === 'ADD_TO_CART' || gtmEvent === 'CHECKOUT'|| gtmEvent === 'ECOMMERCE_PURCHASE') { 262 if (_productsInfosCart[produit.refProduit] == null && _ecommerceItems[produit.refProduit] != null) { 263 _productsInfosCart[produit.refProduit] = {}; 264 for (let prop in _ecommerceItems[produit.refProduit]) { 265 _productsInfosCart[produit.refProduit][prop] = _ecommerceItems[produit.refProduit][prop]; 266 } 267 } 268 if (_productsInfosCart[produit.refProduit] != null) { 269 listName = _productsInfosCart[produit.refProduit].listName; 270 listId = _productsInfosCart[produit.refProduit].listId; 271 index = _productsInfosCart[produit.refProduit].index; 272 brand = _productsInfosCart[produit.refProduit].brand; 273 } 274 } else if (listName != null) { 275 index = i; 276 _ecommerceItems[produit.refProduit] = {listId: listId, listName: listName , index: i, brand: brand}; 277 } else if(_ecommerceItems[produit.refProduit] != null) { 278 listName = _ecommerceItems[produit.refProduit].listName; 279 listId = _ecommerceItems[produit.refProduit].listId; 280 index = _ecommerceItems[produit.refProduit].index; 281 brand = _ecommerceItems[produit.refProduit].brand; 282 } 283 284 if (gtmEvent === 'VIEW_ITEM_LIST' && commonConfig.googleTagManagerGA4) { 285 event['item_list_id'] = listId; 286 event['item_list_name'] = listName; 287 } 288 289 let item = {}; 290 if (gtmEvent === 'VIEW_ITEM_LIST' && commonConfig.googleTagManagerGA4) { 291 item = { 292 'item_name': produit.libelle, 293 'item_id': (produit.tarif)? produit.tarif.codeTarif : undefined, 294 'price': (produit.tarif)? produit.tarif.montantTtc.replace(",",".") : undefined, 295 'quantity': produit.quantiteSelect, 296 'currency': 'eur', 297 'item_brand': brand, 298 'item_category': category, 299 'index': index 300 }; 301 } else { 302 item = { 303 'item_name': produit.libelle, 304 'item_id': (produit.tarif)? produit.tarif.codeTarif : undefined, 305 'price': (produit.tarif)? produit.tarif.montantTtc.replace(",",".") : undefined, 306 'quantity': produit.quantiteSelect, 307 'currency': 'eur', 308 'item_brand': brand, 309 'item_category': category, 310 'item_list_id': listId, 311 'item_list_name': listName, 312 'index': index 313 }; 314 } 315 event.ecommerce.items.push(item); 316 } 317 return event; 318 } 319 factory.getProductInfosCart = function getProductInfosCart(refProduit) { 320 return _productsInfosCart[refProduit]; 321 } 322 factory.setProductInfosCart = function setProductInfosCart(produit) { 323 if (produit && produit.refProduit && _productsInfosCart[produit.refProduit] == null) { 324 _productsInfosCart[produit.refProduit] = {listId: produit.gtmListId, listName: produit.gtmListName, index: produit.gtmIndex, brand: produit.gtmBrand}; 325 } 326 } 327 328 return factory; 329 } 330]);
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.