[{$smarty.block.parent}]
[{assign var="sAcceptCookies" value=$oViewConf->getOxCookie('acceptCookies')}]
[{assign var="sDisplayedCookiesNotification" value=$oViewConf->getOxCookie('displayedCookiesNotification')}]

[{assign var="oCurrency" value=$oView->getActCurrency()}]
[{assign var="sPageType" value='Homepage'}]
[{assign var="sActiveClassName" value=$oViewConf->getActiveClassName()}]
[{assign var="oActCategory" value=$oView->getActiveCategory()}]
[{assign var="sActiveLang" value=$oViewConf->getActiveLangCode()}]

[{if $sActiveClassName == 'details'}]
	[{assign var="sPageType" value='Productpage'}]
[{elseif $sActiveClassName == 'list'}]
	[{assign var="sPageType" value='Listingpage'}]
[{elseif $sActiveClassName == 'alist'}]
	[{assign var="sPageType" value='Listingpage'}]
[{elseif $sActiveClassName == 'search_result'}]
	[{assign var="sPageType" value='Search'}]
[{elseif $sActiveClassName == 'basket'}]
	[{assign var="sPageType" value='Basketpage'}]
[{elseif $sActiveClassName == 'thankyou'}]
	[{assign var="sPageType" value='Transactionpage'}]
[{elseif $sActiveClassName == 'start'}]
	[{assign var="sPageType" value='Homepage'}]
[{elseif $sActiveClassName == 'user'}]
	[{assign var="sPageType" value='Basketpage'}]
[{else}]
	[{assign var="sPageType" value=$sActiveClassName}]
[{/if}]

[{if !isset($oConfig)}]
	[{assign var="oConfig" value=$oViewConf->getConfig()}]
[{/if}]

[{assign var="sStoreCode" value=$oConfig->getConfigParam('trackingShopCode')}]

<script>
[{if $oConfig->getConfigParam('trackingBlUseYandex')}]
<!-- Yandex.Metrika counter -->

   (function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
   m[i].l=1*new Date();k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
   (window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");

   ym(86633095, "init", {
        clickmap:true,
        trackLinks:true,
        accurateTrackBounce:true,
        webvisor:true,
        ecommerce:"dataLayer"
   });

<!-- /Yandex.Metrika counter -->
[{/if}]

window.dataLayer = window.dataLayer || [];

[{if $sAcceptCookies == 1}]

[{assign var="blLoggedIn" value=false}]
[{if $oxcmp_user}]
	[{assign var="blLoggedIn" value=true}]
	[{if $oxcmp_user->isGuest()}]
		[{assign var="blLoggedIn" value=false}]
	[{/if}]
[{/if}]
window.dataLayer.push(
	{
		visitorExistingCustomer: '[{if $blLoggedIn}]Yes[{else}]No[{/if}]',
		visitorLoginState: '[{if $blLoggedIn}]Logged in[{/if}]',
		visitorType: '[{if $blLoggedIn}][{if $oxcmp_user->isB2b()}]B2B[{else}][{if $oxcmp_user->isEmployee()}]Employee[{else}]B2C[{/if}][{/if}][{else}]Not Logged in[{/if}]', 
		visitorId: '[{if $blLoggedIn}][{$oxcmp_user->oxuser__sap_customer_number->value}][{/if}]',
		storeCode: '[{$sStoreCode}]',
		currencyCode: '[{$oCurrency->name}]', 
		PageType: '[{$sPageType}]', 
		HashedEmail: '[{if $blLoggedIn}][{$oxcmp_user->getEmailHashed()}][{/if}]',
		internalTraffic: [{if $oViewConf->isRequestSourceIsInternal()}]1[{else}]0[{/if}],
		siteType: 'shop'
	}
);
(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','GTM-NVN9GX');

[{elseif $sDisplayedCookiesNotification != 1}]
window.dataLayer.push(
	{
		storeCode: '[{$sStoreCode}]',
		currencyCode: '[{$oCurrency->name}]', 
		PageType: '[{$sPageType}]', 
		internalTraffic: [{if $oViewConf->isRequestSourceIsInternal()}]1[{else}]0[{/if}],
		siteType: 'shop'
	}
);
(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','GTM-NVN9GX');

[{/if}]
</script>

[{capture assign="dataLayerFooter"}]
[{strip}]

		const userClickedAddElementBasketEvent = function() {
			$('#toBasket').off('click').on('click', function(event) {
				const target = event.currentTarget;
				event.preventDefault();
				const product = { 
	        name: $(this).attr('data-title'),
	        id: $(this).attr('data-artnum'),
	        price: $(this).attr('data-price'),
	        brand: $(this).attr('data-brand') || 'AL-KO',
	        quantity: 1
	     	};
				$(this).off('click');
				window.dataLayer = window.dataLayer || [];
				window.dataLayer.push({
					event: 'addToCart',
				  ecommerce: {
				    currencyCode: '[{$oCurrency->name}]',
				    add: {                                
				      products: [
								product
							]
				    }
				  },
					eventCallback: function () {
        		target.click();
					},
					eventTimeout: 2000
				});
			});
		};

		const userClickedContactEvent = function() {
			$('#contactButton').off('click').on('click', function(event) {
				const target = event.currentTarget;
				event.preventDefault();
				const product = { 
	        name: $(this).attr('data-title'),
	        id: $(this).attr('data-artnum'),
	        price: $(this).attr('data-price'),
	        brand: $(this).attr('data-brand') || 'AL-KO',
	        quantity: 1
	     	};
				$(this).off('click');
				window.dataLayer = window.dataLayer || [];
				window.dataLayer.push({
					event: 'productContact',
				  ecommerce: {
				    currencyCode: '[{$oCurrency->name}]',
						langCode: '[{$sActiveLang}]',
				    add: {                                
				      products: [
								product
							]
				    }
				  },
					eventCallback: function () {
        		target.click();
					},
					eventTimeout: 2000
				});
			});
		};
		const userClickedCompareEvent = function() {
			$('.compare').off('click').on('click', function(event) {
				const target = event.currentTarget;
				event.preventDefault();
				const product = { 
	        name: $(this).attr('data-title'),
	        id: $(this).attr('data-artnum'),
	        price: $(this).attr('data-price'),
	        brand: $(this).attr('data-brand') || 'AL-KO',
			category: [{if $oActCategory && method_exists($oActCategory, 'getTitle')}]'[{$oActCategory->getTitle()}]'[{else}]'cat'[{/if}],
			dimension10: [{if $oActCategory && method_exists($oActCategory, 'getId')}]'[{$oActCategory->getIdWithoutShopId()}]'[{else}]0[{/if}],
	        quantity: 1
	     	};
				$(this).off('click');
				window.dataLayer = window.dataLayer || [];
				window.dataLayer.push({
					event: 'addToCompare',
				  ecommerce: {
				    currencyCode: '[{$oCurrency->name}]',
						langCode: '[{$sActiveLang}]',
				    add: {                                
				      products: [
								product
							]
				    }
				  },
					eventCallback: function () {
        		target.click();
					},
					eventTimeout: 2000
				});
			});
		};

		const userClickedAddElementBasketFromListEvent = function() {
			$('button[name=addToBasketFromList]').off('click').on('click', function(event) {
				event.preventDefault();

				const product = { 
	        name: $(this).attr('data-title'),
	        id: $(this).attr('data-artnum'),
	        price: $(this).attr('data-price'),
	        brand: $(this).attr('data-brand') || 'AL-KO',
	        quantity: 1
	     	};
				$(this).off('click');
				const target = event.currentTarget;
				window.dataLayer = window.dataLayer || [];
				window.dataLayer.push({
					event: 'addToCart',
				  ecommerce: {
				    currencyCode: '[{$oCurrency->name}]',
				    add: {                                
				      products: [
								product
							]
				    }
				  },
					eventCallback: function () {
        		target.click();
					},
					eventTimeout: 2000
				});
			});
		};

		const userClickedAddElementBasketFromCompareListEvent = function() {
			$('button[name=addToBasketFromCompareList]').off('click').on('click', function(event) {
				event.preventDefault();
				const product = { 
	        name: $(this).attr('data-title'),
	        id: $(this).attr('data-artnum'),
	        price: $(this).attr('data-price'),
	        brand: $(this).attr('data-brand') || 'AL-KO',
	        quantity: 1
	     	};
			 	$(this).off('click');
				const target = event.currentTarget;
				window.dataLayer = window.dataLayer || [];
				window.dataLayer.push({
					event: 'addToCart',
				  ecommerce: {
				    currencyCode: '[{$oCurrency->name}]',
				    add: {                                
				      products: [
								product
							]
				    }
				  },
					eventCallback: function () {
						target.click();
					},
					eventTimeout: 2000
				});
			});
		};

		const userClickedRemoveElementBasketEvent = function () {

			$('#basketcontents_list button[name=removeBtn]').off('click').on('click', function(event) {
				const inputForm = $(this).attr('data-input');
				$(`#${inputForm}`).val(1);
				$(this).attr('type', 'submit');

				event.preventDefault();
				const product = { 
	        name: $(this).attr('data-title'),
	        id: $(this).attr('data-artnum'),
	        price: $(this).attr('data-price'),
	        brand: $(this).attr('data-brand') || 'AL-KO',
	        quantity: 1
	     	};
		 		window.dataLayer = window.dataLayer || [];
				$(this).off('click');
				const target = event.currentTarget;
				window.dataLayer.push({
					event: 'removeFromCart',
				  ecommerce: {
				    currencyCode: '[{$oCurrency->name}]',
				    remove: {                                
				      products: [
								product
							]
			    	}
			  	},
					eventCallback: function () {
	      		target.click();
					},
					eventTimeout: 2000
				});
			});
		};

		const userClickedProductEvent = function() {
			$('#productList .productBox a.show-product-link').off('click').on('click', function(event) {
				event.preventDefault();
				const form = $(this).parents('form');
				const url =  $(this).attr('href');
				const position = form.attr('data-position').split('_').slice(-1).pop();
				const productObj = {
					name: form.attr('data-title'),
					id: form.attr('data-artnum'),
					price: form.attr('data-price'),
					position,
					url
				};
				let categoryListName = 'Category';
				if ($('#breadcrumb .breadcrumb-item').length > 2) {
					categoryListName = 'Subcategory';
				}
				const pageNo = $('.pagination .page-item.active').text().trim() || 1;
				const cat = $('.page-header h1').text().replace(/(\r\n|\n|\r)/gm, "").trim();
		    window.dataLayer = window.dataLayer || []; 
		    window.dataLayer.push({ 
		        event: 'productClick', 
		        ecommerce: { 
								currencyCode: '[{$oCurrency->name}]', 
		            click: { 
		                actionField: {
											list: categoryListName + ' - [{$sPageType}]: Page ' + pageNo,
										},
		                products: [
											{ 
		                    name: productObj.name,
		                    id: productObj.id,
		                    price: productObj.price,
		                    brand: form.attr('data-brand') || 'AL-KO',
		                    category: [{if $oActCategory && method_exists($oActCategory, 'getTitle')}]'[{$oActCategory->getTitle()}]'[{else}]cat[{/if}],
		                    dimension10: [{if $oActCategory && method_exists($oActCategory, 'getId')}]'[{$oActCategory->getIdWithoutShopId()}]'[{else}]0[{/if}],
		                    position: productObj.position
		                	}
										] 
		            } 
		        }, 
		        eventCallback: function() { 
		          document.location = productObj.url;
		        },
						eventTimeout: 2000
			  });
			});

			$('#searchList .productBox a.show-product-link').off('click').on('click', function(event) {
				event.preventDefault();
				const form = $(this).parents('form');
				const url = $(this).attr('href');
				const position = form.attr('data-position').split('_').slice(-1).pop();
				const productObj = {
					name: form.attr('data-title'),
					id: form.attr('data-artnum'),
					price: form.attr('data-price'),
					position,
					url
				};
				const pageNo = $('.pagination .page-item.active').text().trim() || 1;
		    window.dataLayer = window.dataLayer || []; 
		    window.dataLayer.push({ 
		        event: 'productClick', 
		        ecommerce: { 
								currencyCode: '[{$oCurrency->name}]', 
		            click: { 
		                actionField: {
											list: 'Search: Page ' + pageNo,
										},
		                products: [
											{ 
		                    name: productObj.name,
		                    id: productObj.id,
		                    price: productObj.price,
		                    brand: form.attr('data-brand') || 'AL-KO',
		                    category: 'search',
		                    dimension10: 0,
		                    position: productObj.position
		                	}
										] 
		            } 
		        }, 
		        eventCallback: function() { 
		          document.location = productObj.url;
		        },
						eventTimeout: 2000
			  });
			});

			$('.alko-category-teaser .article.box a').off('click').on('click', function(event) {
				event.preventDefault();
				const form = $(this).parent();
				const url =  $(this).attr('href');

				const productObj = {
					name: form.attr('data-title'),
					id: form.attr('data-artnum'),
					price: form.attr('data-price'),
					position: form.attr('data-position'),
					url
				};
				let categoryListName = 'Category';
				if ($('#breadcrumb .breadcrumb-item').length > 2) {
					categoryListName = 'Subcategory';
				}

				const listName = $(this).parents('.alko-category-teaser').attr('data-list');

				const cat = $('.page-header h1').text().replace(/(\r\n|\n|\r)/gm, "").trim();
		    window.dataLayer = window.dataLayer || []; 
		    window.dataLayer.push({ 
		        event: 'productClick', 
		        ecommerce: { 
								currencyCode: '[{$oCurrency->name}]', 
		            click: { 
		                actionField: {
											list: '[{$sPageType}] – CategoryTeaser: ' + listName,
										},
		                products: [
											{ 
		                    name: productObj.name,
		                    id: productObj.id,
		                    price: productObj.price,
		                    brand: form.attr('data-brand') || 'AL-KO',
		                    category: [{if $oActCategory && method_exists($oActCategory, 'getTitle')}]'[{$oActCategory->getTitle()}]'[{else}]cat[{/if}],
		                    dimension10: [{if $oActCategory && method_exists($oActCategory, 'getId')}]'[{$oActCategory->getIdWithoutShopId()}]'[{else}]0[{/if}],
		                    position: productObj.position
		                	}
										] 
		            } 
		        }, 
		        eventCallback: function() { 
		          document.location = productObj.url;
		        },
						eventTimeout: 2000
			  });
			});

			$('#boughtTogether .productBox a').off('click').on('click', function(event) {
				event.preventDefault();
				const form = $(this).parents('form');
				const url =  $(this).attr('href');
				const cat = $('.breadcrumb-item.active').text();
				const position = form.attr('data-position').split('_').slice(-1).pop();
				
				const productObj = {
					name: form.attr('data-title'),
					id: form.attr('data-productid'),
					price: form.attr('data-price'),
					position,
					url
				};

		    window.dataLayer = window.dataLayer || []; 
		    window.dataLayer.push({ 
		        event: 'productClick', 
		        ecommerce: { 
								currencyCode: '[{$oCurrency->name}]', 
		            click: { 
		                actionField: {
											list: 'Productdetail – Accessory: Page 1',
										},
		                products: [
											{ 
		                    name: productObj.name,
		                    id: productObj.id,
		                    price: productObj.price,
												brand: form.attr('data-brand') || 'AL-KO',
		                    category: [{if $oActCategory && method_exists($oActCategory, 'getTitle')}]'[{$oActCategory->getTitle()}]'[{else}]cat[{/if}],
		                    dimension10: [{if $oActCategory && method_exists($oActCategory, 'getId')}]'[{$oActCategory->getIdWithoutShopId()}]'[{else}]0[{/if}],
		                    position: productObj.position
		                	}
										] 
		            } 
		        }, 
		        eventCallback: function() { 
		          document.location = productObj.url;
		        },
						eventTimeout: 2000
			  });
			});


			$('#accessories .productBox a').off('click').on('click', function(event) {
				event.preventDefault();
				const form = $(this).parents('form');
				const url = $(this).attr('href');
				const position = form.attr('data-position').split('_').slice(-1).pop();
				const cat = $('.breadcrumb-item.active').text();

				const productObj = {
					name: form.attr('data-title'),
					id: form.attr('data-artnum'),
					price: form.attr('data-price'),
					position,
					url
				};
				const pageNo = $('.pagination .page-item.active').text().trim() || 1;
		    window.dataLayer = window.dataLayer || []; 
		    window.dataLayer.push({ 
		        event: 'productClick', 
		        ecommerce: { 
								currencyCode: '[{$oCurrency->name}]', 
		            click: { 
		                actionField: {
											list: 'Productdetail – Accessory: Page 1',
										},
		                products: [
											{ 
		                    name: productObj.name,
		                    id: productObj.id,
		                    price: productObj.price,
		                    brand: form.attr('data-brand') || 'AL-KO',
		                    category: [{if $oActCategory && method_exists($oActCategory, 'getTitle')}]'[{$oActCategory->getTitle()}]'[{else}]cat[{/if}],
		                    dimension10: [{if $oActCategory && method_exists($oActCategory, 'getId')}]'[{$oActCategory->getIdWithoutShopId()}]'[{else}]0[{/if}],
		                    position: productObj.position
		                	}
										] 
		            } 
		        }, 
		        eventCallback: function() { 
		          document.location = productObj.url;
		        },
						eventTimeout: 2000
			  });
			});
		};

		const sendPageCategoryList = function(update) {
			const categoryName = $('.page-header h1').text().replace(/(\r\n|\n|\r)/gm, "").trim();
			const pageNo = $('.pagination .page-item.active').text().trim() || 1;
			let categoryListName = 'Category';
			if ($('#breadcrumb .breadcrumb-item').length > 2) {
				categoryListName = 'Subcategory';
			}
			$('#productList').each(function(index, value) {
				const categoryImpressions = [];
				$(this).find('form').each(function() {
					const position = $(this).attr('data-position').split('_').slice(-1).pop();
					if ($(this).attr('data-title')) {
						categoryImpressions.push( {
							name: $(this).attr('data-title'),
							id: $(this).attr('data-artnum'),
							price: $(this).attr('data-price'),
							brand: $(this).attr('data-brand') || 'AL-KO',
							list: categoryListName + ' – ' + categoryName + ': Page ' + pageNo,
							position
						});
					}
				});
				if (categoryImpressions.length > 0) {
					window.dataLayer.push({
						event: update ? 'productImpressionUpdated' : 'productImpression',
						prod_sort: false,
						prod_filter: $('#asnActiveFilter button').length > 0,
						ecommerce: {
					    currencyCode: '[{$oCurrency->name}]', 
			    		impressions: categoryImpressions
					}});
				}
			});

			$('#searchList').each(function(index, value) {
				const categoryImpressions = [];
				$(this).find('form').each(function() {
					const position = $(this).attr('data-position').split('_').slice(-1).pop();
					if ($(this).attr('data-title')) {
						categoryImpressions.push( {
							name: $(this).attr('data-title'),
							id: $(this).attr('data-artnum'),
							price: $(this).attr('data-price'),
							brand: $(this).attr('data-brand') || 'AL-KO',
							list: 'Search: Page ' + pageNo,
							position
						});
					}
				});
				if (categoryImpressions.length > 0) {
					window.dataLayer.push({
						event: update ? 'productImpressionUpdated' : 'productImpression',
						prod_sort: false,
						prod_filter: $('#asnActiveFilter button').length > 0,
						ecommerce: {
					    currencyCode: '[{$oCurrency->name}]', 
			    		impressions: categoryImpressions
					}});
				}
			});

		};

		const handleAlkoTeaser = function() {
			$('.alko-category-teaser').each(function(index, value) {
				const teaser = $(this);
				const listName = $(this).attr('data-list');
				let i = 1;
				const impressions = [];
				$(this).find('.article.box').each(function() {
					if ($(this).attr('data-title')) {
						impressions.push( {
							name: $(this).attr('data-title'),
							id: $(this).attr('data-artnum'),
							price: $(this).attr('data-price'),
							brand: $(this).attr('data-brand') || 'AL-KO',
							list: '[{$sPageType}] – CategoryTeaser: ' + listName,
							position: i++
						});
					}
				});

				if (impressions.length > 0) {
					window.dataLayer.push({
						event: 'productImpression', 
						ecommerce: {
					    currencyCode: '[{$oCurrency->name}]', 
			    		impressions
					}});
				}
			});
		};

		const handleDdShortCode = function() {
			$('.dd-shortcode-action').each(function(index, value) {
				const actionImpressions = [];
				let actionImpressionsI = 1;
				const h2ListName = $(this).find('h2');
				let listName = '';
				if (h2ListName) {
					listName = $(h2ListName).text();
				}
				$(this).find('form[name="tobasket"]').each(function() {
					if ($(this).attr('data-title')) {
						actionImpressions.push( {
							name: $(this).attr('data-title'),
							id: $(this).attr('data-artnum'),
							price: $(this).attr('data-price'),
							brand: $(this).attr('data-brand') || 'AL-KO',
							list: '[{$sPageType}] – Action: ' + listName,
							position: actionImpressionsI++
						});
					}
				});
				if (actionImpressions.length > 0) {
					window.dataLayer.push({
						event: 'productImpression', 
						ecommerce: {
					    currencyCode: '[{$oCurrency->name}]', 
			    		impressions: actionImpressions
					}});
				}
			});

		};

	$( document ).ready(function() {
		
		handleAlkoTeaser();
		handleDdShortCode();
		userClickedProductEvent();
		sendPageCategoryList(false);
		userClickedRemoveElementBasketEvent();
		userClickedAddElementBasketEvent();
		userClickedAddElementBasketFromListEvent();
		userClickedAddElementBasketFromCompareListEvent();
		userClickedContactEvent();
		userClickedCompareEvent();

		$('body').on('DOMSubtreeModified', '#productList .row', function() {
			userClickedProductEvent();
			sendPageCategoryList(true);
		});
		
	});
[{/strip}]
[{/capture}]

[{capture assign="userEvents"}]
[{strip}]

	const userClickedRemoveElementBasketEvent = function () {
		$('#basketcontents_list button[name=removeBtn]').off('click').on('click', function(event) {
			const inputForm = $(this).attr('data-input');
			event.preventDefault();
			$(`#${inputForm}`).val(1);
			$(this).attr('type', 'submit');
			$(this).off('click');
			const target = event.currentTarget;
  		target.click();
		});
	};

	$( document ).ready(function() {
		userClickedRemoveElementBasketEvent();		
	});
[{/strip}]
[{/capture}]

[{if $sAcceptCookies == 1}]
[{oxscript add=$dataLayerFooter}]
[{else}]
[{oxscript add=$userEvents}]
[{/if}]
