define(['jquery','underscore'],function($,_){'use strict';function GoogleAnalyticsUniversal(config){this.blockNames=config.blockNames;this.dlCurrencyCode=config.dlCurrencyCode;this.dataLayer=config.dataLayer;this.staticImpressions=config.staticImpressions;this.staticPromotions=config.staticPromotions;this.updatedImpressions=config.updatedImpressions;this.updatedPromotions=config.updatedPromotions;} GoogleAnalyticsUniversal.prototype={activeOnCategory:function(id,name,category,list,position){this.dataLayer.push({'event':'productClick','ecommerce':{'click':{'actionField':{'list':list},'products':[{'id':id,'name':name,'category':category,'list':list,'position':position}]}}});},activeOnProducts:function(id,name,list,position,category){this.dataLayer.push({'event':'productClick','ecommerce':{'click':{'actionField':{'list':list},'products':[{'id':id,'name':name,'list':list,'position':position,'category':category}]}}});},addToCart:function(id,name,price,quantity){this.dataLayer.push({'event':'addToCart','ecommerce':{'currencyCode':this.dlCurrencyCode,'add':{'products':[{'id':id,'name':name,'price':price,'quantity':quantity}]}}});},removeFromCart:function(id,name,price,quantity){this.dataLayer.push({'event':'removeFromCart','ecommerce':{'currencyCode':this.dlCurrencyCode,'remove':{'products':[{'id':id,'name':name,'price':price,'quantity':quantity}]}}});},clickBanner:function(id,name,creative,position){this.dataLayer.push({'event':'promotionClick','ecommerce':{'promoClick':{'promotions':[{'id':id,'name':name,'creative':creative,'position':position}]}}});},bindImpressionClick:function(id,type,name,category,list,position,blockType,listPosition){var productLink=[],eventBlock;switch(blockType){case'catalog.product.related':eventBlock='.products-related .products';break;case'product.info.upsell':eventBlock='.products-upsell .products';break;case'checkout.cart.crosssell':eventBlock='.products-crosssell .products';break;case'category.products.list':case'search_result_list':eventBlock='.products .products';break;} productLink=$(eventBlock+' .item:nth('+listPosition+') a');if(type==='configurable'||type==='bundle'||type==='grouped'){productLink=$(eventBlock+' .item:nth('+listPosition+') .tocart,'+ eventBlock+' .item:nth('+listPosition+') a');} productLink.each(function(index,element){$(element).on('click',function(){if(blockType!=='category.products.list'){category='';} this.activeOnProducts(id,name,list,position,category);}.bind(this));}.bind(this));},updateImpressions:function(){var pageImpressions=this.mergeImpressions(),dlImpressions={'event':'productImpression','ecommerce':{'impressions':[]}},i=0,impressionCounter=0,impression,blockName;for(blockName in pageImpressions){if(blockName==='length'||!pageImpressions.hasOwnProperty(blockName)){continue;} for(i;i0){this.dataLayer.push(dlImpressions);}},mergeImpressions:function(){var pageImpressions=[];this.blockNames.forEach(function(blockName){if(blockName in this.updatedImpressions){pageImpressions[blockName]=this.updatedImpressions[blockName];}else if(blockName in this.staticImpressions){pageImpressions[blockName]=this.staticImpressions[blockName];}},this);return pageImpressions;},updatePromotions:function(){var dlPromotions={'event':'promotionView','ecommerce':{'promoView':{'promotions':[]}}},pagePromotions=[],promotionCounter=0,bannerIds=[],i=0,promotion,self=this;if(this.updatedPromotions.length){pagePromotions=this.updatedPromotions;} if(!pagePromotions.length&&this.staticPromotions.length){pagePromotions=this.staticPromotions;} if($('[data-banner-id]').length){_.each($('[data-banner-id]'),function(banner){var $banner=$(banner),ids=($banner.data('ids')+'').split(',');bannerIds=$.merge(bannerIds,ids);});} bannerIds=$.unique(bannerIds);for(i;i0){this.dataLayer.push(dlPromotions);} $('[data-banner-id]').on('click','[data-banner-id]',function(){var bannerId=$(this).attr('data-banner-id'),promotions=_.filter(pagePromotions,function(item){return item.id===bannerId;});_.each(promotions,function(promotionItem){self.clickBanner(promotionItem.id,promotionItem.name,promotionItem.creative,promotionItem.position);});});}};return GoogleAnalyticsUniversal;});