1/* 2Google eCommerce GA4 for Dajbog (bez GTM, koristi direktno gtag) 3Refaktorisano da koristi gtag('event', ...) umesto dataLayer.push 4*/ 5 6const formatToCurrency = cena => { 7 cena = parseFloat(cena); 8 if (isNaN(cena)) return ''; 9 return parseFloat(cena.toFixed(2)); 10}; 11 12// function gtm_view_item_list(itemsinfo, catname, catid){ 13// gtag('event', 'view_item_list', { 14// item_list_name: catname, 15// item_list_id: catid, 16// items: itemsinfo 17// }); 18// } 19 20 21 22function gtm_view_item_list(itemsinfo, catname, catid){ 23 if (productObj.item_name) { 24 dataLayer.push({ 25 event: 'view_item_list', 26 ecommerce: { 27 item_list_name: catname, 28 item_list_id: catid, 29 items: itemsinfo 30 } 31 }); 32 } 33} 34 35 36 37 38 39 40// function select_item(productObj) { 41// gtag('event', 'select_item', { 42// items: [productObj] 43// }); 44// } 45 46 47function select_item(productObj) { 48 dataLayer.push({ 49 event: 'select_item', 50 ecommerce: { 51 items: [productObj] 52 } 53 }); 54} 55 56 57// function gtm_view_item(productObj){ 58// gtag('event', 'view_item', { 59// currency: "RSD", 60// value: formatToCurrency(productObj.price), 61// items: [productObj] 62// }); 63// } 64 65 66function gtm_view_item(productObj){ 67 if (productObj.item_name) { 68 dataLayer.push({ 69 event: 'view_item', 70 ecommerce: { 71 currency: "RSD", 72 value: formatToCurrency(productObj.price), 73 items: [productObj] 74 } 75 }); 76 } 77} 78 79function gtm_add_to_card(productObj){ 80 if (productObj.item_name) { 81 dataLayer.push({ 82 event: 'add_to_cart', 83 ecommerce: { 84 currency: "RSD", 85 value: formatToCurrency(productObj.price * productObj.quantity), 86 items: [productObj] 87 } 88 }); 89 } 90} 91 92 93 94function gtm_add_to_wishlist(productObj){ 95 if(productObj.item_name){ 96 gtag('event', 'add_to_wishlist', { 97 currency: "RSD", 98 value: formatToCurrency(productObj.price), 99 items: [productObj] 100 }); 101 } 102} 103 104// function gtm_remove_from_card(productObj){ 105// gtag('event', 'remove_from_cart', { 106// items: [productObj] 107// }); 108// } 109 110function gtm_remove_from_card(productObj){ 111 dataLayer.push({ 112 event: 'remove_from_cart', 113 ecommerce: { 114 items: [productObj] 115 } 116 }); 117} 118 119 120function gtm_click_to_checkout(itemsinfo){ 121 gtag('event', 'begin_checkout', { 122 items: itemsinfo 123 }); 124} 125 126// function gtm_purchase(itemsinfo, tid, final_price, promocode){ 127// gtag('event', 'purchase', { 128// transaction_id: tid, 129// affiliation: "Kibid DOO - online store", 130// value: formatToCurrency(final_price), 131// tax: '', 132// shipping: '', 133// currency: "RSD", 134// coupon: promocode || '', 135// items: itemsinfo 136// }); 137// } 138 139// function gtm_purchase(itemsinfo, tid, final_price, promocode,deliveryprice,tax,aptaclub){ 140// gtag('event', 'purchase', { 141// transaction_id: tid, 142// affiliation: "Kibid DOO - online store", 143// value: formatToCurrency(final_price), 144// tax: tax, 145// shipping: deliveryprice, 146// currency: "RSD", 147// aptaclub: aptaclub, 148// coupon: promocode || '', 149// items: itemsinfo 150// }); 151// } 152 153 154function gtm_purchase(itemsinfo, tid, final_price, promocode, deliveryprice, tax, aptaclub) { 155 dataLayer.push({ 156 event: 'purchase', 157 ecommerce: { 158 transaction_id: tid, 159 affiliation: "Kibid DOO - online store", 160 value: formatToCurrency(final_price), 161 tax: tax, 162 shipping: deliveryprice, 163 currency: "RSD", 164 coupon: promocode || '', 165 items: normalizeItems(itemsinfo) 166 }, 167 aptaclub: aptaclub // ako je aptaclub specifiÄan za ovaj event 168 }); 169} 170 171 172// function gtm_view_cart(itemsinfo){ 173// // let fprice = itemsinfo.reduce((sum, item) => sum + (parseFloat(item.price) * item.quantity), 0); 174// // gtag('event', 'view_cart', { 175// // currency: "RSD", 176// // value: formatToCurrency(fprice), 177// // items: itemsinfo 178// // }); 179// // } 180 181 182function gtm_view_cart(itemsinfo){ 183 let fprice = itemsinfo.reduce((sum, item) => sum + (parseFloat(item.price) * item.quantity), 0); 184 dataLayer.push({ 185 event: 'view_cart', 186 ecommerce: { 187 currency: "RSD", 188 value: formatToCurrency(fprice), 189 items: normalizeItems(itemsinfo) 190 } 191 }); 192} 193 194 195 196// 197// function gtm_add_payment_info(itemsinfo){ 198// let fprice = itemsinfo.reduce((sum, item) => sum + (parseFloat(item.price) * item.quantity), 0); 199// gtag('event', 'add_payment_info', { 200// currency: "RSD", 201// value: formatToCurrency(fprice), 202// items: itemsinfo 203// }); 204// } 205 206function gtm_add_payment_info(itemsinfo){ 207 let fprice = itemsinfo.reduce((sum, item) => sum + (parseFloat(item.price) * item.quantity), 0); 208 dataLayer.push({ 209 event: 'add_payment_info', 210 ecommerce: { 211 currency: "RSD", 212 value: formatToCurrency(fprice), 213 items: normalizeItems(itemsinfo) 214 } 215 }); 216} 217 218 219// function gtm_add_shipping_info(itemsinfo){ 220// let fprice = itemsinfo.reduce((sum, item) => sum + (parseFloat(item.price) * item.quantity), 0); 221// gtag('event', 'add_shipping_info', { 222// currency: "RSD", 223// value: formatToCurrency(fprice), 224// items: itemsinfo 225// }); 226// } 227 228function gtm_add_shipping_info(itemsinfo){ 229 let fprice = itemsinfo.reduce((sum, item) => sum + (parseFloat(item.price) * item.quantity), 0); 230 dataLayer.push({ 231 event: 'add_shipping_info', 232 ecommerce: { 233 currency: "RSD", 234 value: formatToCurrency(fprice), 235 items: normalizeItems(itemsinfo) 236 } 237 }); 238} 239 240 241// function gtm_begin_checkout(itemsinfo){ 242// let fprice = itemsinfo.reduce((sum, item) => sum + (parseFloat(item.price) * item.quantity), 0); 243// gtag('event', 'begin_checkout', { 244// currency: "RSD", 245// value: formatToCurrency(fprice), 246// items: itemsinfo 247// }); 248// } 249 250 251function gtm_begin_checkout(itemsinfo){ 252 let fprice = itemsinfo.reduce((sum, item) => sum + (parseFloat(item.price) * item.quantity), 0); 253 dataLayer.push({ 254 event: 'begin_checkout', 255 ecommerce: { 256 currency: "RSD", 257 value: formatToCurrency(fprice), 258 items: normalizeItems(itemsinfo) 259 } 260 }); 261} 262 263 264 265 266function checkout_data(itemsinfo){ 267 let fprice = itemsinfo.reduce((sum, item) => sum + (parseFloat(item.price) * item.quantity), 0); 268 dataLayer.push({ 269 event: 'checkout_data', 270 ecommerce: { 271 currency: "RSD", 272 value: formatToCurrency(fprice), 273 items: normalizeItems(itemsinfo) 274 } 275 }); 276} 277 278 279// --- Triggers i DOM interakcije --- 280 281document.addEventListener('DOMContentLoaded', function () { 282 if (typeof product_array !== 'undefined' && product_array.length > 0) { 283 const categoryListPage = document.getElementById("category_list_page"); 284 if (categoryListPage) { 285 const catname = categoryListPage.dataset.name; 286 const catid = categoryListPage.dataset.id; 287 gtm_view_item_list(product_array, catname, catid); 288 } 289 } 290 291 document.querySelectorAll('.link_to_product').forEach(link => { 292 link.addEventListener('click', function (e) { 293 e.preventDefault(); 294 const box = this.closest(".product-item__body"); 295 const product = getProductFromElement(box); 296 select_item(product); 297 window.location.href = box.dataset.url; 298 }); 299 }); 300 301 // const addToCard = document.getElementById("addtocard"); 302 // const websiteInput = document.getElementById("website"); 303 // 304 // if (addToCard && websiteInput && websiteInput.value === "") { 305 // const product = getProductFromElement(addToCard); 306 // gtm_view_item(product); 307 // 308 // addToCard.addEventListener('click', function () { 309 // const product = getProductFromElement(addToCard); 310 // 311 // // Umesto globalnog querySelector, tražimo unutar najbliže forme 312 // const container = addToCard.closest('form') || document; 313 // const quantityEl = container.querySelector('#quantityInput'); 314 // 315 // if (quantityEl) { 316 // product.quantity = parseInt(quantityEl.value) || 1; 317 // } 318 // 319 // if( product.quantity>0){ 320 // gtm_add_to_card(product); 321 // 322 // } 323 // }); 324 // } 325 326 327 const addToCard = document.getElementById("addtocard"); 328 const websiteInput = document.getElementById("website"); 329 330 if (addToCard && websiteInput && websiteInput.value === "") { 331 const product = getProductFromElement(addToCard); 332 gtm_view_item(product); 333 334 addToCard.addEventListener('click', function () { 335 const product = getProductFromElement(addToCard); 336 337 // Tražimo unutar najbliže forme da bismo uzeli ispravan quantity input 338 const container = addToCard.closest('form') || document; 339 const quantityEl = container.querySelector('#quantityInput'); 340 341 if (quantityEl) { 342 const quantity = parseInt(quantityEl.value) || 1; 343 const max = parseInt(quantityEl.getAttribute('max')) || Infinity; 344 345 // proveravamo da li je koliÄina validna 346 if (quantity > 0 && quantity <= max) { 347 product.quantity = quantity; 348 gtm_add_to_card(product); 349 } else { 350 351 return false; // prekida izvrÅ¡avanje 352 } 353 } else { 354 console.warn('Nije pronaÄen input za koliÄinu.'); 355 } 356 }); 357 } 358 359 360 document.querySelectorAll('.product-basket').forEach(button => { 361 button.addEventListener('click', function () { 362 const box = this.closest(".product-item__body"); 363 const product = getProductFromElement(box); 364 gtm_add_to_card(product); 365 }); 366 }); 367 368 document.querySelectorAll('.cl-button-wish').forEach(button => { 369 button.addEventListener('click', function () { 370 const box = this.closest(".product-item__body"); 371 const product = getProductFromElement(box); 372 gtm_add_to_wishlist(product); 373 }); 374 }); 375 376 377 378 379
380 const wishlist = document.getElementById("add-to-wishlist"); 381 if (wishlist && addToCard) { 382 wishlist.addEventListener('click', function () { 383 const product = getProductFromElement(addToCard); 384 gtm_add_to_wishlist(product); 385 }); 386 } 387 388 document.querySelectorAll(".delonshoppingcart").forEach(btn => { 389 btn.addEventListener('click', function () { 390 391 const product = getProductFromElement(this); 392 393 product.quantity = parseInt(this.dataset.kolicina) || 1; 394 gtm_remove_from_card(product); 395 }); 396 }); 397 398 399 document.querySelectorAll(".delonshoppingcart2").forEach(btn => { 400 btn.addEventListener('click', function () { 401 402 const product = getProductFromElement(this); 403 404 product.quantity = parseInt(this.dataset.kolicina) || 1; 405 gtm_remove_from_card(product); 406 }); 407 }); 408 409 410 411 412 413 414 const shoppingcart = document.getElementById("shopping-cart"); 415 if (shoppingcart && product_array.length > 0) { 416 if (!sessionStorage.getItem('view_cart_sent')) { 417 gtm_view_cart(product_array); 418 gtm_begin_checkout(product_array); 419 sessionStorage.setItem('view_cart_sent', '1'); // Obeleži da je veÄ poslato 420 } 421 } 422 423 424 425 // const seacard = document.getElementById("seacard"); 426 // 427 // if (seacard && typeof product_array !== 'undefined' && product_array.length > 0) { 428 // seacard.addEventListener('click', function () { 429 // gtm_view_cart(product_array); 430 // }); 431 // } 432 433 434 435 436 // let addpi = 0; 437 // document.getElementById("fname")?.addEventListener('keyup', function () { 438 // if (this.value && addpi === 0) { 439 // addpi = 1; 440 // if (product_array.length > 0) gtm_add_payment_info(product_array); 441 // } 442 // }); 443 444 let addpi = 0; 445 446 document.querySelectorAll('.paymenttype').forEach(input => { 447 input.addEventListener('change', function () { 448 if (addpi === 0 && product_array.length > 0) { 449 addpi = 1; 450 gtm_add_payment_info(product_array); 451 } 452 }); 453 }); 454 455 456 457 let addpi2 = 0; 458 459 document.querySelectorAll('.orderType').forEach(input => { 460 input.addEventListener('change', function () { 461 if (addpi2 === 0 && product_array.length > 0) { 462 addpi2 = 1; 463 gtm_add_shipping_info(product_array); 464 } 465 }); 466 }); 467 468 // let addpiaddress = 0; 469 // document.getElementById("address")?.addEventListener('keyup', function () { 470 // if (this.value && addpiaddress === 0) { 471 // addpiaddress = 1; 472 // if (product_array.length > 0) gtm_add_shipping_info(product_array); 473 // } 474 // }); 475 476 477 let addpiaddress = 0; 478 document.getElementById("address")?.addEventListener('keyup', function () { 479 if (this.value && addpiaddress === 0) { 480 addpiaddress = 1; 481 if (product_array.length > 0) checkout_data(product_array); 482 } 483 }); 484 485 486 487 488 489 // const cartprinfo = document.getElementById('order-status-info'); 490 // if (cartprinfo && cartprinfo.dataset.status == 1 && product_array.length > 0) { 491 // const tid = cartprinfo.dataset.idorder; 492 // const final_price = cartprinfo.dataset.finalprice; 493 // const promocode = cartprinfo.dataset.promocode; 494 // const deliveryprice = cartprinfo.dataset.deliveryprice; 495 // const tax = cartprinfo.dataset.tax; 496 // const aptaclub = cartprinfo.dataset.aptaclub; 497 // 498 // gtm_purchase(product_array, tid, final_price, promocode,deliveryprice,tax,aptaclub); 499 // } 500 501 502 const cartprinfo = document.getElementById('order-status-info'); 503 const currentPurchaseId = cartprinfo?.dataset.idorder; 504 const lastPurchaseId = sessionStorage.getItem('last_purchase_id'); 505 506 if ( 507 cartprinfo && 508 cartprinfo.dataset.status == 1 && 509 product_array.length > 0 && 510 currentPurchaseId !== lastPurchaseId 511 ) {
512 const final_price = cartprinfo.dataset.finalprice; 513 const promocode = cartprinfo.dataset.promocode; 514 const deliveryprice = cartprinfo.dataset.deliveryprice; 515 const tax = cartprinfo.dataset.tax; 516 const aptaclub = cartprinfo.dataset.aptaclub; 517 518 gtm_purchase(product_array, currentPurchaseId, final_price, promocode, deliveryprice, tax, aptaclub); 519 520 // Zabeleži ID poslednje kupljene narudžbine 521 sessionStorage.setItem('last_purchase_id', currentPurchaseId); 522 } 523 524}); 525 526function getProductFromElement(el) { 527 return { 528 item_id: el.dataset.id, 529 item_name: el.dataset.name, 530 item_brand: el.dataset.brand, 531 item_category: el.dataset.category, 532 item_category_2: el.dataset.category_2, 533 item_category_3: el.dataset.category_3, 534 item_category_4: el.dataset.category_4, 535 item_variant: '', 536 index: el.dataset.index || 1, 537 quantity: 1, 538 price: parseFloat(el.dataset.price), 539 currency: "RSD" 540 }; 541} 542 543 544function normalizeItems(items) { 545 return items.map(item => ({ 546 ...item, 547 price: parseFloat(item.price) || 0, 548 quantity: parseInt(item.quantity) || 1, 549 index: parseInt(item.index) || 1 550 })); 551}
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.