mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-08 23:46:14 +01:00
8 lines
11 KiB
JavaScript
8 lines
11 KiB
JavaScript
/**
|
|
* Angular Google Analytics - Easy tracking for your AngularJS application
|
|
* @version v0.0.15 - 2015-04-27
|
|
* @link http://github.com/revolunet/angular-google-analytics
|
|
* @author Julien Bouquillon <julien@revolunet.com>
|
|
* @license MIT License, http://www.opensource.org/licenses/MIT
|
|
*/
|
|
"use strict";angular.module("angular-google-analytics",[]).provider("Analytics",function(){var e,t,n,i,a,c,r=!1,o=!0,s="",u=!1,l="$routeChangeSuccess",g="auto",d=!1,m=!1,_=!1,f=!1,h=!1,p={allowLinker:!0},k=!1,v=!1;this._logs=[],this.setAccount=function(t){return e=t,!0},this.trackPages=function(e){return o=e,!0},this.trackPrefix=function(e){return s=e,!0},this.setDomainName=function(e){return n=e,!0},this.useDisplayFeatures=function(e){return t=!!e,!0},this.useAnalytics=function(e){return u=!!e,!0},this.useEnhancedLinkAttribution=function(e){return _=!!e,!0},this.useCrossDomainLinker=function(e){return h=!!e,!0},this.setCrossLinkDomains=function(e){return c=e,!0},this.setPageEvent=function(e){return l=e,!0},this.setCookieConfig=function(e){return g=e,!0},this.useECommerce=function(e,t){return d=!!e,m=!!t,!0},this.setRemoveRegExp=function(e){return e instanceof RegExp?(i=e,!0):!1},this.setExperimentId=function(e){return a=e,!0},this.ignoreFirstPageLoad=function(e){return f=!!e,!0},this.trackUrlParams=function(e){return k=!!e,!0},this.delayScriptTag=function(e){return v=!!e,!0},this.$get=["$document","$location","$log","$rootScope","$window",function(E,y,w,A,T){function P(e){!u&&T._gaq&&"function"==typeof e&&e()}function b(e){u&&T.ga&&"function"==typeof e&&e()}function C(e,t){return!angular.isUndefined(t)&&"name"in t&&t.name?t.name+"."+e:e}function q(e,t){return e in t&&t[e]}var I=this,j=function(){var e=k?y.url():y.path();return i?e.replace(i,""):e};return this._log=function(){arguments.length>0&&(arguments.length>1&&"warn"===arguments[0]&&w.warn(Array.prototype.slice.call(arguments,1)),this._logs.push(arguments))},this._createScriptTag=function(){if(!e)return I._log("warn","No account id set to create script tag"),void 0;if(r)return I._log("warn","Script tag already created"),void 0;T._gaq=[],T._gaq.push(["_setAccount",e]),n&&T._gaq.push(["_setDomainName",n]),_&&T._gaq.push(["_require","inpage_linkid","//www.google-analytics.com/plugins/ga/inpage_linkid.js"]),o&&!f&&(i?T._gaq.push(["_trackPageview",j()]):T._gaq.push(["_trackPageview"]));var a;return a=t?("https:"===document.location.protocol?"https://":"http://")+"stats.g.doubleclick.net/dc.js":("https:"===document.location.protocol?"https://ssl":"http://www")+".google-analytics.com/ga.js",function(){var e=E[0],t=e.createElement("script");t.type="text/javascript",t.async=!0,t.src=a;var n=e.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)}(a),r=!0},this._createAnalyticsScriptTag=function(){if(!e)return I._log("warn","No account id set to create analytics script tag"),void 0;if(r)return I._log("warn","Analytics script tag already created"),void 0;if(function(e,t,n,i,a,c,r){e.GoogleAnalyticsObject=a,e[a]=e[a]||function(){(e[a].q=e[a].q||[]).push(arguments)},e[a].l=1*new Date,c=t.createElement(n),r=t.getElementsByTagName(n)[0],c.async=1,c.src=i,r.parentNode.insertBefore(c,r)}(window,document,"script","//www.google-analytics.com/analytics.js","ga"),angular.isArray(e)?e.forEach(function(e){var t,n="cookieConfig"in e?e.cookieConfig:g;q("crossDomainLinker",e)&&(e.allowLinker=e.crossDomainLinker),angular.forEach(["name","allowLinker"],function(n){n in e&&(angular.isUndefined(t)&&(t={}),t[n]=e[n])}),angular.isUndefined(t)?T.ga("create",e.tracker,n):T.ga("create",e.tracker,n,t),t&&"allowLinker"in t&&t.allowLinker&&(T.ga(C("require",e),"linker"),q("crossLinkDomains",e)&&T.ga(C("linker:autoLink",e),e.crossLinkDomains))}):h?(T.ga("create",e,g,p),T.ga("require","linker"),c&&T.ga("linker:autoLink",c)):T.ga("create",e,g),t&&T.ga("require","displayfeatures"),o&&!f&&T.ga("send","pageview",j()),T.ga&&(d&&(m?T.ga("require","ec","ec.js"):T.ga("require","ecommerce","ecommerce.js")),_&&T.ga("require","linkid","linkid.js"),a)){var n=document.createElement("script"),i=document.getElementsByTagName("script")[0];n.src="//www.google-analytics.com/cx/api.js?experiment="+a,i.parentNode.insertBefore(n,i)}return r=!0},this._ecommerceEnabled=function(){return d?m?(this._log("warn","Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. Use AnalyticsProvider.setECommerce(true, false);"),!1):!0:(this._log("warn","ecommerce not set. Use AnalyticsProvider.setECommerce(true, false);"),!1)},this._enhancedEcommerceEnabled=function(){return d?m?!0:(this._log("warn","Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true, true);"),!1):(this._log("warn","ecommerce not set. Use AnalyticsProvider.setECommerce(true, true);"),!1)},this._trackPage=function(t,n,i){var a=this,c=arguments;t=t?t:j(),n=n?n:E[0].title,P(function(){T._gaq.push(["_set","title",n]),T._gaq.push(["_trackPageview",s+t]),a._log("_trackPageview",t,n,c)}),b(function(){var r={page:s+t,title:n};angular.isObject(i)&&angular.extend(r,i),angular.isArray(e)?e.forEach(function(e){T.ga(C("send",e),"pageview",r)}):T.ga("send","pageview",r),a._log("pageview",t,n,c)})},this._trackEvent=function(t,n,i,a,c,r){var o=this,s=arguments;P(function(){T._gaq.push(["_trackEvent",t,n,i,a,!!c]),o._log("trackEvent",s)}),b(function(){var u={};angular.isDefined(c)&&(u.nonInteraction=!!c),angular.isObject(r)&&angular.extend(u,r),angular.isArray(e)?e.forEach(function(e){q("trackEvent",e)&&T.ga(C("send",e),"event",t,n,i,a,u)}):T.ga("send","event",t,n,i,a,u),o._log("event",s)})},this._addTrans=function(e,t,n,i,a,c,r,o,s){var u=this,l=arguments;P(function(){T._gaq.push(["_addTrans",e,t,n,i,a,c,r,o]),u._log("_addTrans",l)}),b(function(){u._ecommerceEnabled()&&(T.ga("ecommerce:addTransaction",{id:e,affiliation:t,revenue:n,tax:i,shipping:a,currency:s||"USD"}),u._log("ecommerce:addTransaction",l))})},this._addItem=function(e,t,n,i,a,c){var r=this,o=arguments;P(function(){T._gaq.push(["_addItem",e,t,n,i,a,c]),r._log("_addItem",o)}),b(function(){r._ecommerceEnabled()&&(T.ga("ecommerce:addItem",{id:e,name:n,sku:t,category:i,price:a,quantity:c}),r._log("ecommerce:addItem",o))})},this._trackTrans=function(){var e=this,t=arguments;P(function(){T._gaq.push(["_trackTrans"]),e._log("_trackTrans",t)}),b(function(){e._ecommerceEnabled()&&(T.ga("ecommerce:send"),e._log("ecommerce:send",t))})},this._clearTrans=function(){var e=this,t=arguments;b(function(){e._ecommerceEnabled()&&(T.ga("ecommerce:clear"),e._log("ecommerce:clear",t))})},this._addProduct=function(e,t,n,i,a,c,r,o,s){var u=this,l=arguments;P(function(){T._gaq.push(["_addProduct",e,t,n,i,a,c,r,o,s]),u._log("_addProduct",l)}),b(function(){u._enhancedEcommerceEnabled()&&(T.ga("ec:addProduct",{id:e,name:t,category:n,brand:i,variant:a,price:c,quantity:r,coupon:o,position:s}),u._log("ec:addProduct",l))})},this._addImpression=function(e,t,n,i,a,c,r,o){var s=this,u=arguments;P(function(){T._gaq.push(["_addImpression",e,t,n,i,a,c,r,o]),s._log("_addImpression",u)}),b(function(){s._enhancedEcommerceEnabled()&&T.ga("ec:addImpression",{id:e,name:t,category:a,brand:i,variant:c,list:n,position:r,price:o}),s._log("ec:addImpression",u)})},this._addPromo=function(e,t,n,i){var a=this,c=arguments;P(function(){T._gaq.push(["_addPromo",e,t,n,i]),a._log("_addPromo",arguments)}),b(function(){a._enhancedEcommerceEnabled()&&(T.ga("ec:addPromo",{id:e,name:t,creative:n,position:i}),a._log("ec:addPromo",c))})},this._getActionFieldObject=function(e,t,n,i,a,c,r,o,s){var u={};return e&&(u.id=e),t&&(u.affiliation=t),n&&(u.revenue=n),i&&(u.tax=i),a&&(u.shipping=a),c&&(u.coupon=c),r&&(u.list=r),o&&(u.step=o),s&&(u.option=s),u},this._setAction=function(e,t){var n=this,i=arguments;P(function(){T._gaq.push(["_setAction",e,t]),n._log("__setAction",i)}),b(function(){n._enhancedEcommerceEnabled()&&(T.ga("ec:setAction",e,t),n._log("ec:setAction",i))})},this._trackTransaction=function(e,t,n,i,a,c,r,o,s){this._setAction("purchase",this._getActionFieldObject(e,t,n,i,a,c,r,o,s)),this._pageView()},this._trackRefund=function(e){this._setAction("refund",this._getActionFieldObject(e)),this._pageView()},this._trackCheckOut=function(e,t){this._setAction("checkout",this._getActionFieldObject(null,null,null,null,null,null,null,e,t)),this._pageView()},this._trackCart=function(e){-1!==["add","remove"].indexOf(e)&&(this._setAction(e),this._send("event","UX","click",e+" to cart"))},this._promoClick=function(e){this._setAction("promo_click"),this._send("event","Internal Promotions","click",e)},this._productClick=function(e){this._setAction("click",this._getActionFieldObject(null,null,null,null,null,null,e,null,null)),this._send("event","UX","click",e)},this._send=function(){var e=this,t=Array.prototype.slice.call(arguments);t.unshift("send"),b(function(){T.ga.apply(this,t),e._log(t)})},this._pageView=function(){this._send("pageview")},this._set=function(e,t){var n=this;b(function(){T.ga("set",e,t),n._log("set",e,t)})},v||(u?this._createAnalyticsScriptTag():this._createScriptTag()),o&&A.$on(l,function(){I._trackPage()}),this._trackTimings=function(e,t,n,i){this._send("timing",e,t,n,i)},{_logs:I._logs,displayFeatures:t,ecommerce:d,enhancedEcommerce:m,enhancedLinkAttribution:_,getUrl:j,experimentId:a,ignoreFirstPageLoad:f,delayScriptTag:v,setCookieConfig:I._setCookieConfig,getCookieConfig:function(){return g},createAnalyticsScriptTag:function(e){return e&&(g=e),I._createAnalyticsScriptTag()},createScriptTag:function(e){return e&&(g=e),I._createScriptTag()},ecommerceEnabled:function(){return I._ecommerceEnabled()},enhancedEcommerceEnabled:function(){return I._enhancedEcommerceEnabled()},trackPage:function(e,t,n){I._trackPage(e,t,n)},trackEvent:function(e,t,n,i,a,c){I._trackEvent(e,t,n,i,a,c)},addTrans:function(e,t,n,i,a,c,r,o,s){I._addTrans(e,t,n,i,a,c,r,o,s)},addItem:function(e,t,n,i,a,c){I._addItem(e,t,n,i,a,c)},trackTrans:function(){I._trackTrans()},clearTrans:function(){I._clearTrans()},addProduct:function(e,t,n,i,a,c,r,o,s){I._addProduct(e,t,n,i,a,c,r,o,s)},addPromo:function(e,t,n,i){I._addPromo(e,t,n,i)},addImpression:function(e,t,n,i,a,c,r,o){I._addImpression(e,t,n,i,a,c,r,o)},productClick:function(e){I._productClick(e)},promoClick:function(e){I._promoClick(e)},trackDetail:function(){I._setAction("detail"),I._pageView()},trackCart:function(e){I._trackCart(e)},trackCheckout:function(e,t){I._trackCheckOut(e,t)},trackTimings:function(e,t,n,i){I._trackTimings(e,t,n,i)},trackTransaction:function(e,t,n,i,a,c,r,o,s){I._trackTransaction(e,t,n,i,a,c,r,o,s)},setAction:function(e,t){I._setAction(e,t)},send:function(e){I._send(e)},pageView:function(){I._pageView()},set:function(e,t){I._set(e,t)}}}]}).directive("gaTrackEvent",["Analytics","$parse",function(e,t){return{restrict:"A",link:function(n,i,a){var c=t(a.gaTrackEvent)(n);i.bind("click",function(){c.length>1&&e.trackEvent.apply(e,c)})}}}]); |