PageSourceSearch

https://pustet.at/wp-content/plugins/datalayer-for-woocommerce/ass…/js/main-script-render-datalayer.js?ver=3.9

js pustet.at collected 2026-10-02 11:39:13 UTC 13,840 bytes, 417 lines download raw bytes

1/**
2 * Script Render Datalayer
3 *
4 * @version 3.0
5 * @package 'datalayer'
6 */
7
8jQuery( function ( $ ) {
9	if (RenderDatalayer.addToCartJsEvents) {
10		jQuery( 'a[href*=add-to-cart],body.single-product button[name=add-to-cart],div[data-block-name=\'woocommerce/single-product\'] button[name=add-to-cart]' ).on( 'click', function(){
11			var quantity = jQuery( this ).data( 'quantity' );
12			if ( ! quantity) {
13				var quantity = jQuery( 'body' ).find( 'input[name=quantity]' ).val();
14				if ( ! quantity) {
15					quantity = 1;
16				}
17			}
18
19
20			var product_id = jQuery( this ).data( 'product_id' );
21			var id         = jQuery( this ).val();
22
23			var productClicked = false;
24			if (product_id) {
25				productClicked = RenderDatalayer.products.find( element => element.id_match == product_id );
26			}
27			if (id) {
28				productClicked = RenderDatalayer.products.find( element => element.id_match == id );
29			}
30
31			if (jQuery( this ).hasClass('single_add_to_cart_button')) {
32				productClicked = RenderDatalayer.currentProduct[0];
33			}
34
35			var value = quantity * productClicked.price;
36			if (isNaN(value) || value === undefined){
37				value = productClicked.price;
38			}
39
40			if (productClicked) {
41				delete productClicked.id_match;
42				if (RenderDatalayer.datalayerOption == 'GA4') {
43					productClicked.quantity = quantity;
44					dataLayer.push( { ecommerce: null } );
45					dataLayer.push(
46						{
47							'event': 'add_to_cart',
48							'event_id': RenderDatalayer.eventId,
49							'ecommerce': {
50								'currency': RenderDatalayer.currencyCode,
51								'value': parseFloat(value.toFixed(2)),
52								'items': [ productClicked ]
53							}
54						}
55					);
56				}
57			}
58		});
59
60		jQuery( "body.single-product .woocommerce-variation-add-to-cart button.single_add_to_cart_button" ).on(
61			"click",
62			function(){
63				if ( ! jQuery( this ).hasClass( 'disabled' )) {
64					var quantity = jQuery( 'body' ).find( 'input[name=quantity]' ).val();
65					if(quantity === undefined){
66						quantity   = 1;
67					}
68					var product_id = jQuery( this ).parent().find( 'input[name=variation_id]' ).val();
69					if (product_id === undefined){
70						product_id = jQuery( 'body' ).find( 'input[name=variation_id]' ).val();
71					}
72					var variant = RenderDatalayer.currentProduct[0].variations.find( element => element.id == product_id );
73
74					RenderDatalayer.currentProduct[0].price = variant.variation_price;
75					var value = quantity * RenderDatalayer.currentProduct[0].price;
76					if (isNaN(value) || value === undefined){
77						value = RenderDatalayer.currentProduct[0].price;
78					}
79
80					let currentProductCloned = Object.assign({}, RenderDatalayer.currentProduct[0]);
81
82					delete currentProductCloned.id_match;
83					delete currentProductCloned.variations;
84
85					if (RenderDatalayer.datalayerOption == 'GA4') {
86						currentProductCloned.item_variant = variant.variation_name;
87						currentProductCloned.item_name = variant.variation_name_product;
88						currentProductCloned.item_id = variant.variation_id;
89						currentProductCloned.quantity = quantity;
90						if(variant.variation_discount !== 0){
91							currentProductCloned.discount = variant.variation_discount;
92						}
93						dataLayer.push( { ecommerce: null } );
94						dataLayer.push(
95							{
96								'event': 'add_to_cart',
97								'event_id': RenderDatalayer.eventId,
98								'ecommerce': {
99									'currency': RenderDatalayer.currencyCode,
100									'value': parseFloat(value.toFixed(2)),
101									'items': [ currentProductCloned ]
102								}
103							}
104						);
105					}
106				}
107			}
108		);
109	}
110
111	jQuery( 'li.product,div.product-list-item,div.product-grid-item,li.wc-block-grid__product,.products > div.type-product' ).click(
112		function () {
113			var product_id     = jQuery( this ).find( 'a[href*=add-to-cart],a[href*=add_to_wishlist] ,a.product_type_variable, a.product_type_external' ).data( 'product_id' );
114			var id             = jQuery( this ).find( 'div' ).data( 'id' );
115			var classes 	   = jQuery( this ).attr('class').split(' ');
116			var postClass = classes.find(function(className) {
117				return className.startsWith('post-');
118			});
119
120
121			var productClicked = false;
122			if (product_id) {
123				productClicked = RenderDatalayer.products.find( element =>
123 element.id_match == product_id );
124			}
125			if (id) {
126				productClicked = RenderDatalayer.products.find( element => element.id_match == id );
127			}
128			if ( undefined === id && undefined === product_id && postClass){
129				productClicked = RenderDatalayer.products.find( element => element.id_match == postClass.split('-')[1] );
130			}
131			if (productClicked) {
132				delete productClicked.id_match;
133				delete productClicked.variations;
134				if (RenderDatalayer.datalayerOption == 'GA4') {
135					dataLayer.push( { ecommerce: null } );
136					dataLayer.push(
137						{
138							'event': 'select_item',
139							'event_id': RenderDatalayer.eventId,
140							'ecommerce': {
141								'currency': RenderDatalayer.currencyCode,
142								'items': [ productClicked ]
143							}
144						}
145					);
146
147					if(productClicked.promotion_id){
148						dataLayer.push( { ecommerce: null } );
149						dataLayer.push(
150							{
151								'event': 'select_promotion',
152								'event_id': RenderDatalayer.eventId,
153								'ecommerce': {
154									'creative_name': productClicked.creative_name,
155									'creative_slot': productClicked.creative_slot,
156									'promotion_id': productClicked.promotion_id,
157									'promotion_name': productClicked.promotion_name,
158									'items': [ productClicked ]
159								}
160							}
161						);
162					}
163				}
164			}
165		}
166	);
167
168	function wishlist(){
169		jQuery('a[href*=add_to_wishlist],.wlfwc-add-to-wishlist,a.nmww-button,.wt-wishlist-button,a.tinvwl_add_to_wishlist_button').off('click');
170		jQuery( 'a[href*=add_to_wishlist],.wlfwc-add-to-wishlist,a.nmww-button,.wt-wishlist-button,a.tinvwl_add_to_wishlist_button').on( 'click', function(){
171			var quantity = jQuery( this ).data( 'quantity' );
172			if ( ! quantity) {
173				var quantity = jQuery( 'body' ).find( 'input[name=quantity]' ).val();
174				if ( ! quantity) {
175					quantity = 1;
176				}
177			}
178
179
180			var product_id = jQuery( this ).data( 'product-id' ) || jQuery( this ).data( 'productid' ) || jQuery( this ).data( 'product_id' ) || jQuery( this ).data( 'tinv-wl-product' );
181			var id         = jQuery( this ).val();
182
183			var productClicked = false;
184			if (product_id) {
185				productClicked = RenderDatalayer.products.find( element => element.id_match == product_id );
186				if(productClicked === undefined){
187					productClicked = RenderDatalayer.currentProduct[0];
188				}
189			}
190			if (id) {
191				productClicked = RenderDatalayer.products.find( element => element.id_match == id );
192			}
193
194			var value = quantity * productClicked.price;
195			if (isNaN(value) || value === undefined){
196				value = productClicked.price;
197			}
198
199			if (productClicked) {
200				if (RenderDatalayer.datalayerOption == 'GA4') {
201					let randomString = Array(2).fill().map(() => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.charAt(Math.floor(Math.random() * 62))).join('');
202					productClicked.quantity = quantity;
203					dataLayer.push( { ecommerce: null } );
204					dataLayer.push(
205						{
206							'event': 'add_to_wishlist',
207							'event_id': RenderDatalayer.eventId+randomString,
208							'ecommerce': {
209								'currency': RenderDatalayer.currencyCode,
210								'value': parseFloat(value.toFixed(2)),
211								'items': [ productClicked ]
212							}
213						}
214					);
215				}
216			}
217		});
218	}
219	wishlist();
220	jQuery( document ).ajaxStop(
221		function() {
222			wishlist();
223		}
224	);
225
226	function remove_item(){
227		var links = document.querySelectorAll("a[href*='?remove_item']");
228		links.forEach(function(link) {
229			link.addEventListener('click', function() {
230				var id = link.getAttribute("data-product_id");
231				var name = link.getAttribute("data-product_name");
232				var sku = link.getAttribute("data-product_sku");
233				var price = link.getAttribute("data-product_price");
234				var category = link.getAttribute("data-product_category");
235				var variant = link.getAttribute("data-product_variant");
236				var quantity = link.getAttribute("data-product_quantity");
237				var brand = link.getAttribute("data-product_brand");
238				var discount = link.getAttribute("data-product_discount");
239				var idOrSku = RenderDatalayer.idOrSku;
240				if (idOrSku=='SKU'){
241					id = sku;
242				}
243				var value = price * quantity;
244				if (isNaN(value) || value === undefined){
245					value = price;
246				}
247				const categoryString = category;
248				const lastIndex = categoryString.lastIndexOf("-");
249				const sanitizedCategoryString = (lastIndex !== -1) ? categoryString.slice(0, lastIndex) : categoryString;
250				const categoryArray = sanitizedCategoryString.split('-');
251				const categoriesObject = {};
252				categoryArray.forEach((category, index) => {
253					const categoryKey = (index === 0) ? 'item_category' : `item_category${index + 1}`;
254					categoriesObject[categoryKey] = category;
255				});
256
257				var productItems = {
258					'item_id': id,
259					'item_name': name,
260					'price': price,
261					...categoriesObject,
262					'quantity': quantity,
263					'item_brand': brand,
264					'item_variant': variant
265				};
266
267				if(discount !== '0'){
268					productItems['discount'] = discount;
269				}
270
271				if (RenderDatalayer.datalayerOption == 'GA4') {
272					dataLayer.push( { ecommerce: null } );
273					dataLayer.push(
274						{
275							'event': 'remove_from_cart',
276							'event_id': RenderDatalayer.eventId,
277							'ecommerce': {
278								'currency': RenderDatalayer.currencyCode,
279								'value': value,
280								'items': [ productItems ]
281							}
282						}
283					);
284				}
285			});
286		});
287	}
288	let ajaxStopExecuted = false;
289	jQuery(document).off("ajaxStop").on("ajaxStop", function () {
290		if (!ajaxStopExecuted) {
291			remove_item();
292			ajaxStopExecuted = true;
293		} else {
294			remove_item();
295		}
296	});
297	if (!ajaxStopExecuted) {
298		setTimeout(function () {
299			if (!ajaxStopExecuted && jQuery.active === 0) {
300				remove_item();
301				ajaxStopExecuted = true;
302			}
303		}, 100);
304	}
305
306	if (RenderDatalayer.checkoutJsEvents) {
307
308		document.body.addEventListener('change', function (e){
309			var input_name   = e.target.getAttribute('name');
310			if (e.target && input_name) {
311				const type = e.target.value;
312				if( RenderDatalayer.add_shipping_info === 'selected' ){
313					if ( input_name.includes('shipping_method') || input_name.includes('radio-control-0') ) {
314						add_shipping_info( type );
315					}
316				}
317				if( RenderDatalayer.add_payment_info === 'selected' ){
318					if ( input_name === 'payment_method' || input_name === 'radio-control-wc-payment-method-options' ) {
319						add_payment_info( type );
320					}
321				}
322			}
323		});
324		document.body.addEventListener('click', function(e) {
325			var input_name   = e.target.getAttribute('name');
326			var button = e.target.closest('.wc-block-components-checkout-place-order-button');
327			if ( input_name === 'woocommerce_checkout_place_order' || button ){
328				if( RenderDatalayer.add_shipping_info === 'button' ){
329					var checked_shipping_method = document.querySelector('input[name="shipping_method[0]"]:checked') || document.querySelector('input[name="radio-control-0"]:checked');
330					if(checked_shipping_method){
331						add_shipping_info(checked_shipping_method.value);
332					}
333					else{
334						var checked_shipping_method = document.querySelector('input[name="shipping_method[0]"]') || document.querySelector('input[name="radio-control-0"]');
335						if(checked_shipping_method){
336							add_shipping_info(checked_shipping_method.value);
337						}
338					}
339				}
340				if( RenderDatalayer.add_payment_info === 'button' ){
341					var checked_payment_method = document.querySelector('input[name="payment_method"]:checked') || document.querySelector('input[name="radio-control-wc-payment-method-options"]:checked');
342					if(checked_payment_method){
343						add_payment_info(checked_payment_method.value);
344					}
345				}
346			}
347		});
348
349		function add_payment_info( type ){
350			dataLayer.push( { ecommerce: null } );
351			dataLayer.push(
352				{
353					'event': 'add_payment_info',
354					'event_id': RenderDatalayer.eventId,
355					'ecommerce': {
356						'currency': RenderDatalayer.currencyCode,
357						'value': RenderDatalayer.value,
358						'payment_type': type,
359						'items': RenderDatalayer.products
360					}
361				}
362			);
363		}
364
365		function add_shipping_info( type ){
366			dataLayer.push( { ecommerce: null } );
367			dataLayer.push(
368				{
369					'event': 'add_shipping_info',
370					'event_id': RenderDatalayer.eventId,
371					'ecommerce': {
372						'currency': RenderDatalayer.currencyCode,
373						'value': RenderDatalayer.value,
374						'shipping_tier': type,
375						'items': RenderDatalayer.products
376					}
377				}
378			);
379		}
380	}
381
382	if (RenderDatalayer.viewItemJsEvents) {
383		jQuery(document).on('found_variation', function (event, product_variation) {
384
385			var quantity   = 1;
386			var product_id = product_variation.variation_id;
387
388			const RenderActuallyDatalayer = structuredClone(RenderDatalayer);
389			var variant = RenderActuallyDatalayer.currentProduct[0].variations.find( element => element.id == product_id );
390
391			RenderActuallyDatalayer.currentProduct[0].price = variant.variation_price;
392			delete RenderActuallyDatalayer.currentProduct[0].id_match;
393			delete RenderActuallyDatalayer.currentProduct[0].variations;
394
395			if (RenderActuallyDatalayer.datalayerOption == 'GA4') {
396				RenderActuallyDatalayer.currentProduct[0].item_variant = variant.variation_name;
397				RenderActuallyDatalayer.currentProduct[0].item_name = variant.variation_name_product;
398				RenderActuallyDatalayer.currentProduct[0].item_id = variant.variation_id;
399				if(variant.variation_discount !== 0){
400					RenderActuallyDatalayer.currentProduct[0].discount = variant.variation_discount;
401				}
402				dataLayer.push( { ecommerce: null } );
403				dataLayer.push(
404					{
405						'event': 'view_item',
406						'event_id': RenderActuallyDatalayer.eventId,
407						'ecommerce': {
408							'currency': RenderActuallyDatalayer.currencyCode,
409							'items': RenderActuallyDatalayer.currentProduct
410						}
411					}
412				);
413			}
414
415		});
416	}
417});

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.