From 38070a66b0bc6d6c8866cb4d82b2ed7f079efbb9 Mon Sep 17 00:00:00 2001 From: Julien Bouquillon Date: Mon, 24 Nov 2014 12:54:04 +0100 Subject: [PATCH] bump release 0.0.9 --- bower.json | 4 +- dist/angular-google-analytics.js | 1474 +++++++++++++------------ dist/angular-google-analytics.min.js | 4 +- package.json | 6 +- test/unit/angular-google-analytics.js | 4 +- 5 files changed, 771 insertions(+), 721 deletions(-) diff --git a/bower.json b/bower.json index ca74ee5..781f11d 100644 --- a/bower.json +++ b/bower.json @@ -2,7 +2,7 @@ "author": "revolunet", "name": "angular-google-analytics", "description": "Easy Analytics for your AngularJS application", - "version": "0.0.8", + "version": "0.0.9", "homepage": "https://github.com/revolunet/angular-google-analytics", "repository": { "type": "git", @@ -16,4 +16,4 @@ "angular-mocks": ">=1", "angular-scenario": ">=1" } -} \ No newline at end of file +} diff --git a/dist/angular-google-analytics.js b/dist/angular-google-analytics.js index 26a2c67..0846e8a 100644 --- a/dist/angular-google-analytics.js +++ b/dist/angular-google-analytics.js @@ -1,729 +1,779 @@ /** * Angular Google Analytics - Easy tracking for your AngularJS application - * @version v0.0.8 - 2014-10-26 + * @version v0.0.9 - 2014-11-24 * @link http://github.com/revolunet/angular-google-analytics * @author Julien Bouquillon * @license MIT License, http://www.opensource.org/licenses/MIT */ -/* global angular, console */ - 'use strict'; angular.module('angular-google-analytics', []) - .provider('Analytics', function() { - var created = false, - trackRoutes = true, - accountId, - displayFeatures, - trackPrefix = '', - domainName, - analyticsJS = false, - pageEvent = '$routeChangeSuccess', - cookieConfig = 'auto', - ecommerce = false, - enhancedEcommerce = false, - enhancedLinkAttribution = false, - removeRegExp, - experimentId, - ignoreFirstPageLoad = false, - crossDomainLinker = false, - crossLinkDomains, - linkerConfig = {'allowLinker': true}; - - this._logs = []; - - // config methods - this.setAccount = function(id) { - accountId = id; - return true; - }; - this.trackPages = function(doTrack) { - trackRoutes = doTrack; - return true; - }; - - this.trackPrefix = function(prefix) { - trackPrefix = prefix; - return true; - }; - - this.setDomainName = function(domain) { - domainName = domain; - return true; - }; - - this.useDisplayFeatures = function(val) { - displayFeatures = !!val; - return true; - }; - - this.useAnalytics = function(val) { - analyticsJS = !!val; - return true; - }; - - this.useEnhancedLinkAttribution = function (val) { - enhancedLinkAttribution = !!val; - return true; - }; - - this.useCrossDomainLinker = function(val) { - crossDomainLinker = !!val; - return true; - }; - - this.setCrossLinkDomains = function(domains) { - crossLinkDomains = domains; - return true; - }; - - this.setPageEvent = function(name) { - pageEvent = name; - return true; - }; - - this.setCookieConfig = function (config) { - cookieConfig = config; - return true; - }; - - this.useECommerce = function (val,enhanced) { - ecommerce = !!val; - enhancedEcommerce = !!enhanced; - return true; - }; - - - this.setRemoveRegExp = function (regex) { - if (regex instanceof RegExp) { - removeRegExp = regex; - return true; - } - return false; - }; - - this.setExperimentId = function (id) { - experimentId = id; - return true; - }; - - this.ignoreFirstPageLoad = function (val) { - ignoreFirstPageLoad = !!val; - }; - - // public service - this.$get = ['$document', '$rootScope', '$location', '$window', function($document, $rootScope, $location, $window) { - var getUrl = function () { - var url = $location.path(); - if (removeRegExp) { - return url.replace(removeRegExp, ''); - } - return url; - }; - - // private methods - function _createScriptTag() //noinspection JSValidateTypes - { - // inject the google analytics tag - if (!accountId) return; - $window._gaq = []; - $window._gaq.push(['_setAccount', accountId]); - if(domainName) $window._gaq.push(['_setDomainName', domainName]); - if (enhancedLinkAttribution) { - $window._gaq.push(['_require', 'inpage_linkid', '//www.google-analytics.com/plugins/ga/inpage_linkid.js']); - } - if (trackRoutes && !ignoreFirstPageLoad) { - if (removeRegExp) { - $window._gaq.push(['_trackPageview', getUrl()]); - } else { - $window._gaq.push(['_trackPageview']); - } - } - var gaSrc; - if(displayFeatures) { - gaSrc = ('https:' === document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'; - } else { - gaSrc = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; - } - (function() { - var document = $document[0]; - var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; - ga.src = gaSrc; - var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); - })(gaSrc); - created = true; + .provider('Analytics', function () { + var created = false, + trackRoutes = true, + accountId, + displayFeatures, + trackPrefix = '', + domainName, + analyticsJS = false, + pageEvent = '$routeChangeSuccess', + cookieConfig = 'auto', + ecommerce = false, + enhancedEcommerce = false, + enhancedLinkAttribution = false, + removeRegExp, + experimentId, + ignoreFirstPageLoad = false, + crossDomainLinker = false, + crossLinkDomains, + linkerConfig = {'allowLinker': true}; + + this._logs = []; + + // config methods + this.setAccount = function (id) { + accountId = id; + return true; + }; + + this.trackPages = function (doTrack) { + trackRoutes = doTrack; + return true; + }; + + this.trackPrefix = function (prefix) { + trackPrefix = prefix; + return true; + }; + + this.setDomainName = function (domain) { + domainName = domain; + return true; + }; + + this.useDisplayFeatures = function (val) { + displayFeatures = !!val; + return true; + }; + + this.useAnalytics = function (val) { + analyticsJS = !!val; + return true; + }; + + this.useEnhancedLinkAttribution = function (val) { + enhancedLinkAttribution = !!val; + return true; + }; + + this.useCrossDomainLinker = function (val) { + crossDomainLinker = !!val; + return true; + }; + + this.setCrossLinkDomains = function (domains) { + crossLinkDomains = domains; + return true; + }; + + this.setPageEvent = function (name) { + pageEvent = name; + return true; + }; + + this.setCookieConfig = function (config) { + cookieConfig = config; + return true; + }; + + this.useECommerce = function (val, enhanced) { + ecommerce = !!val; + enhancedEcommerce = !!enhanced; + return true; + }; + + this.setRemoveRegExp = function (regex) { + if (regex instanceof RegExp) { + removeRegExp = regex; + return true; + } + return false; + }; + + this.setExperimentId = function (id) { + experimentId = id; + return true; + }; + + this.ignoreFirstPageLoad = function (val) { + ignoreFirstPageLoad = !!val; + return true; + }; + + /** + * Public Service + */ + this.$get = ['$document', '$location', '$log', '$rootScope', '$window', function ($document, $location, $log, $rootScope, $window) { + var me = this; + + var getUrl = function () { + var url = $location.path(); + return removeRegExp ? url.replace(removeRegExp, '') : url; + }; + + /** + * Private Methods + */ + + function _gaJs(fn) { + if (!analyticsJS && $window._gaq && typeof fn === 'function') { + fn(); + } + } + + function _analyticsJs(fn) { + if (analyticsJS && $window.ga && typeof fn === 'function') { + fn(); + } + } + + function _createScriptTag() { + if (!accountId) { + me._log('warn', 'No account id set to create script tag'); + return; + } + + // inject the google analytics tag + $window._gaq = []; + $window._gaq.push(['_setAccount', accountId]); + if(domainName) { + $window._gaq.push(['_setDomainName', domainName]); + } + if (enhancedLinkAttribution) { + $window._gaq.push(['_require', 'inpage_linkid', '//www.google-analytics.com/plugins/ga/inpage_linkid.js']); + } + if (trackRoutes && !ignoreFirstPageLoad) { + if (removeRegExp) { + $window._gaq.push(['_trackPageview', getUrl()]); + } else { + $window._gaq.push(['_trackPageview']); } - - function _createAnalyticsScriptTag() { - if (!accountId) { - return console.warn('No account id set for Analytics.js'); - } - - (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ - (i[r].q=i[r].q||[]).push(arguments);},i[r].l=1*new Date();a=s.createElement(o), - m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m); - })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); - - if (angular.isArray(accountId)) { - accountId.forEach(function (trackerObj) { - $window.ga('create', trackerObj.tracker, cookieConfig, { name: trackerObj.name }); - }); - } else if(crossDomainLinker) { - $window.ga('create', accountId, cookieConfig, linkerConfig); - $window.ga('require', 'linker'); - if(crossLinkDomains) { - $window.ga('linker:autoLink', crossLinkDomains ); - } + } + var gaSrc; + if (displayFeatures) { + gaSrc = ('https:' === document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'; + } else { + gaSrc = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; + } + (function () { + var document = $document[0]; + var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; + ga.src = gaSrc; + var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); + })(gaSrc); + created = true; + } + + function _createAnalyticsScriptTag() { + if (!accountId) { + me._log('warn', 'No account id set to create analytics script tag'); + return; + } + + // inject the google analytics tag + (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ + (i[r].q=i[r].q||[]).push(arguments);},i[r].l=1*new Date();a=s.createElement(o), + m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m); + })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); + + if (angular.isArray(accountId)) { + accountId.forEach(function (trackerObj) { + $window.ga('create', trackerObj.tracker, cookieConfig, { name: trackerObj.name }); + }); + } else if (crossDomainLinker) { + $window.ga('create', accountId, cookieConfig, linkerConfig); + $window.ga('require', 'linker'); + if(crossLinkDomains) { + $window.ga('linker:autoLink', crossLinkDomains ); + } + } else { + $window.ga('create', accountId, cookieConfig); + } + + if (displayFeatures) { + $window.ga('require', 'displayfeatures'); + } + + if (trackRoutes && !ignoreFirstPageLoad) { + $window.ga('send', 'pageview', getUrl()); + } + + if ($window.ga) { + if (ecommerce) { + if (!enhancedEcommerce) { + $window.ga('require', 'ecommerce', 'ecommerce.js'); } else { - $window.ga('create', accountId, cookieConfig); - } - - if(displayFeatures) { - $window.ga('require', 'displayfeatures'); + $window.ga('require', 'ec', 'ec.js'); } - - if (trackRoutes && !ignoreFirstPageLoad) { - $window.ga('send', 'pageview', getUrl()); - } - - if ($window.ga) { - if (ecommerce) { - if (!enhancedEcommerce) - $window.ga('require', 'ecommerce', 'ecommerce.js'); - else - $window.ga('require', 'ec', 'ec.js'); - } - if (enhancedLinkAttribution) { - $window.ga('require', 'linkid', 'linkid.js'); - } - if (experimentId) { - var expScript = document.createElement('script'), - s = document.getElementsByTagName('script')[0]; - expScript.src = "//www.google-analytics.com/cx/api.js?experiment=" + experimentId; - s.parentNode.insertBefore(expScript, s); - } - } - } + if (enhancedLinkAttribution) { + $window.ga('require', 'linkid', 'linkid.js'); + } + if (experimentId) { + var expScript = document.createElement('script'), + s = document.getElementsByTagName('script')[0]; + expScript.src = "//www.google-analytics.com/cx/api.js?experiment=" + experimentId; + s.parentNode.insertBefore(expScript, s); + } + } + } - this._log = function() { - // for testing - //console.info('analytics log:', arguments); - this._logs.push(arguments); - }; - - this._trackPage = function(url, title) { - title = title ? title : $document[0].title; - if (trackRoutes && !analyticsJS && $window._gaq) { - // http://stackoverflow.com/questions/7322288/how-can-i-set-a-page-title-with-google-analytics - $window._gaq.push(["_set", "title", title]); - $window._gaq.push(['_trackPageview', trackPrefix + url]); - this._log('_trackPageview', arguments); - } else if (trackRoutes && analyticsJS && $window.ga) { - if (angular.isArray(accountId)) { - accountId.forEach(function (trackerObj) { - $window.ga(trackerObj.name + '.send', 'pageview', { - 'page': trackPrefix + url, - 'title': title - }); - }); - } else { - $window.ga('send', 'pageview', { - 'page': trackPrefix + url, - 'title': title - }); - } - this._log('pageview', arguments); - } - }; - - this._trackEvent = function(category, action, label, value) { - if (!analyticsJS && $window._gaq) { - $window._gaq.push(['_trackEvent', category, action, label, value]); - this._log('trackEvent', arguments); - } else if ($window.ga) { - $window.ga('send', 'event', category, action, label, value); - this._log('event', arguments); - } - - }; - - /** - * Add transaction - * https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce#_gat.GA_Tracker_._addTrans - * https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#addTrans - * @param transactionId - * @param affiliation - * @param total - * @param tax - * @param shipping - * @param city - * @param state - * @param country - * @private - */ - this._addTrans = function (transactionId, affiliation, total, tax, shipping, city, state, country, currency) { - if (!analyticsJS && $window._gaq) { - $window._gaq.push(['_addTrans', transactionId, affiliation, total, tax, shipping, city, state, country]); - this._log('_addTrans', arguments); - } else if ($window.ga) { - if (!ecommerce) { - console.warn('ecommerce no set. Use AnalyticsProvider.setECommerce(true,false);'); - } else if(enhancedEcommerce ){ - console.warn('Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. ' + - 'Use AnalyticsProvider.setECommerce(true,false);'); - } else { - $window.ga('ecommerce:addTransaction', { - id: transactionId, - affiliation: affiliation, - revenue: total, - tax: tax, - shipping: shipping, - currency: currency || 'USD' - }); - this._log('ecommerce:addTransaction', arguments); - } - - } - }; - - /** - * Add item to transaction - * https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce#_gat.GA_Tracker_._addItem - * https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#addItem - * @param transactionId - * @param sku - * @param name - * @param category - * @param price - * @param quantity - * @private - */ - this._addItem = function (transactionId, sku, name, category, price, quantity) { - if (!analyticsJS && $window._gaq) { - $window._gaq.push(['_addItem', transactionId, sku, name, category, price, quantity]); - this._log('_addItem', arguments); - } else if ($window.ga) { - if (!ecommerce) { - console.warn('ecommerce no set. Use AnalyticsProvider.setECommerce(true,false);'); - } else if(enhancedEcommerce ){ - console.warn('Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. ' + - 'Use AnalyticsProvider.setECommerce(true,false);'); - } else { - $window.ga('ecommerce:addItem', { - id: transactionId, - name: name, - sku: sku, - category: category, - price: price, - quantity: quantity - }); - this._log('ecommerce:addItem', arguments); - } - } - }; - - /** - * Track transaction - * https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce#_gat.GA_Tracker_._trackTrans - * https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#sendingData - * @private - */ - this._trackTrans = function () { - if (!analyticsJS && $window._gaq) { - $window._gaq.push(['_trackTrans']); - this._log('_trackTrans', arguments); - } else if ($window.ga) { - if (!ecommerce) { - console.warn('ecommerce no set. Use AnalyticsProvider.setECommerce(true,false);'); - } else if(enhancedEcommerce ){ - console.warn('Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. ' + - 'Use AnalyticsProvider.setECommerce(true,false);'); - } else { - $window.ga('ecommerce:send'); - this._log('ecommerce:send', arguments); - } - } - }; - - /** - * Clear transaction - * https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#clearingData - * - * @private - */ - this._clearTrans = function () { - if ($window.ga) { - if (!ecommerce) { - console.warn('ecommerce no set. Use AnalyticsProvider.setECommerce(true,false);'); - } else if(enhancedEcommerce ){ - console.warn('Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. ' + - 'Use AnalyticsProvider.setECommerce(true,false);'); - } else { - $window.ga('ecommerce:clear'); - this._log('ecommerce:clear', arguments); - } - } - }; - - /** - Enhanced Ecommerce - */ - - /** - * Add product data - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#product-data - * @param productId - * @param name - * @param category - * @param brand - * @param variant - * @param price - * @param quantity - * @param coupon - * @param position - */ - this._addProduct = function (productId, name, category, brand, variant, price, quantity, coupon,position) { - if (!analyticsJS && $window._gaq) { - $window._gaq.push(['_addProduct', productId, name, category, brand, variant, price, quantity, coupon,position]); - this._log('_addProduct', arguments); - } else if ($window.ga) { - if (!ecommerce) { - console.warn('ecommerce not set. Use AnalyticsProvider.setECommerce(true,true);'); - } else if(!enhancedEcommerce ){ - console.warn('Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true,true);'); - } else { - $window.ga('ec:addProduct', { - id: productId, - name: name, - category: category, - brand: brand, - variant: variant, - price: price, - quantity: quantity, - coupon: coupon, - position: position - }); - this._log('ec:addProduct', arguments); - } - } - }; - - /** - * Add Impression data - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#impression-data - * @param id - * @param name - * @param list - * @param brand - * @param category - * @param variant - * @param position - * @param price - */ - this._addImpression = function(id, name, list, brand, category, variant, position, price){ - if (!analyticsJS && $window._gaq) { - $window._gaq.push(['_addImpression', id, name, list, brand, category, variant, position, price]); - this._log('_addImpression', arguments); - } else if ($window.ga) { - if (!ecommerce) { - console.warn('ecommerce not set. Use AnalyticsProvider.setECommerce(true,true);'); - } else if(!enhancedEcommerce ){ - console.warn('Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true,true);'); - } else { - $window.ga('ec:addImpression', { - id: id, - name: name, - category: category, - brand: brand, - variant: variant, - list: list, - position: position, - price: price - }); - } - this._log('ec:addImpression', arguments); - } - }; - - /** - * Add promo data - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce - * @param productId - * @param name - * @param creative - * @param position - */ - this._addPromo = function (productId, name, creative,position) { - if (!analyticsJS && $window._gaq) { - $window._gaq.push(['_addPromo', productId, name, creative, position]); - this._log('_addPromo', arguments); - } else if ($window.ga) { - if (!ecommerce) { - console.warn('ecommerce not set. Use AnalyticsProvider.setECommerce(true,true);'); - } else if(!enhancedEcommerce ){ - console.warn('Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true,true);'); - } else { - $window.ga('ec:addPromo', { - id: productId, - name: name, - creative: creative, - position: position - }); - this._log('ec:addPromo', arguments); - } - } - }; - - /** - * get ActionFieldObject - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#action-data - * @param id - * @param affliation - * @param revenue - * @param tax - * @param shipping - * @param coupon - * @param list - * @param step - * @param option - */ - this._getActionFieldObject = function (id,affiliation,revenue,tax,shipping,coupon,list,step,option) { - var obj = {}; - if (id) obj.id = id; - if (affiliation) obj.affiliation = affiliation; - if (revenue) obj.revenue = revenue; - if (tax) obj.tax = tax; - if (shipping) obj.shipping = shipping; - if (coupon) obj.coupon = coupon; - if (list) obj.list = list; - if (step) obj.step = step; - if (option) obj.option = option; - return obj; - }; - - /** - * Set Action being performed - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-actions - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#action-types - * @param action - * @param name - * @param obj - */ - this._setAction = function(action,obj){ - if (!analyticsJS && $window._gaq) { - $window._gaq.push(['_setAction',action,obj]); - this._log('__setAction', arguments); - } else if ($window.ga) { - if (!ecommerce) { - console.warn('ecommerce not set. Use AnalyticsProvider.setECommerce(true,true);'); - } else if(!enhancedEcommerce ){ - console.warn('Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true,true);'); - } else { - $window.ga('ec:setAction',action,obj); - this._log('ec:setAction', arguments); - } - } - }; - - /** - * Track Transaction - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-transactions - * @param transactionId - * @param affiliation - * @param revenue - * @param tax - * @param shipping - * @param coupon - * @param list - * @param step - * @param option - */ - this._trackTransaction = function (transactionId,affiliation,revenue,tax,shipping,coupon,list,step,option) { - this._setAction('purchase',this._getActionFieldObject(transactionId,affiliation,revenue,tax,shipping,coupon,list,step,option)); - this._pageView(); - }; - - /** - * Track Refund - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-refunds - * @param transactionId - * - */ - this._trackRefund = function (transactionId) { - this._setAction('refund',this._getActionFieldObject(transactionId)); - this._pageView(); - }; - - /** - * Track Checkout - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-checkout - * @param step - * @param option - * - */ - this._trackCheckOut = function (step,option) { - this._setAction('checkout',this._getActionFieldObject(null,null,null,null,null,null,null,step,option)); - this._pageView(); - }; - - /** - * Track add/remove to cart - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#add-remove-cart - * @param action - * - */ - this._trackCart = function (action){ - if(['add','remove'].indexOf(action) !== -1){ - this._setAction(action); - this._send('event','UX','click', action + 'to cart'); - } - }; - - /** - * Track promo click - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-promo-clicks - * @param promotionName - * - */ - this._promoClick = function (promotionName){ - this._setAction('promo_click'); - this._send('event','Internal Promotions','click', promotionName); - }; - - /** - * Track product click - * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-promo-clicks - * @param promotionName - * - */ - this._productClick = function (listName){ - this._setAction('click',this._getActionFieldObject(null,null,null,null,null,null,listName,null,null)); - this._send('event','UX','click', listName); - }; - - /** - * Send custom events - * https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings#implementation - * https://developers.google.com/analytics/devguides/collection/analyticsjs/social-interactions#implementation - * - * @param obj - * @private - */ - this._send = function (obj) { - if ($window.ga) { - $window.ga('send', obj); - this._log('send', obj); - } - }; - - this._pageView = function() { - this._send('pageview'); - }; - /** - * Set custom dimensions, metrics or experiment - * https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets - * https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#customs - * - * @param name - * @param value - * @private - */ - this._set = function (name, value) { - if ($window.ga) { - $window.ga('set', name, value); - this._log('set', name, value); - } - }; - - - - // creates the ganalytics tracker - if (analyticsJS) { - _createAnalyticsScriptTag(); + this._log = function () { + if (arguments.length > 0) { + if (arguments.length > 1 && arguments[0] === 'warn') { + $log.warn(Array.prototype.slice.call(arguments, 1)); + } + this._logs.push(arguments); + } + }; + + this._ecommerceEnabled = function () { + if (!ecommerce) { + this._log('warn', 'ecommerce not set. Use AnalyticsProvider.setECommerce(true, false);'); + return false; + } else if (enhancedEcommerce) { + this._log('warn', 'Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. ' + + 'Use AnalyticsProvider.setECommerce(true, false);'); + return false; + } + return true; + }; + + this._enhancedEcommerceEnabled = function () { + if (!ecommerce) { + this._log('warn', 'ecommerce not set. Use AnalyticsProvider.setECommerce(true, true);'); + return false; + } else if (!enhancedEcommerce) { + this._log('warn', 'Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true, true);'); + return false; + } + return true; + }; + + /** + * Track page + https://developers.google.com/analytics/devguides/collection/gajs/ + https://developers.google.com/analytics/devguides/collection/analyticsjs/pages + * @param url + * @param title + * @private + */ + this._trackPage = function (url, title) { + var that = this, args = arguments; + url = url ? url : getUrl(); + title = title ? title : $document[0].title; + _gaJs(function () { + // http://stackoverflow.com/questions/7322288/how-can-i-set-a-page-title-with-google-analytics + $window._gaq.push(["_set", "title", title]); + $window._gaq.push(['_trackPageview', trackPrefix + url]); + that._log('_trackPageview', url, title, args); + }); + _analyticsJs(function () { + if (angular.isArray(accountId)) { + accountId.forEach(function (trackerObj) { + $window.ga(trackerObj.name + '.send', 'pageview', { + 'page': trackPrefix + url, + 'title': title + }); + }); } else { - _createScriptTag(); + $window.ga('send', 'pageview', { + 'page': trackPrefix + url, + 'title': title + }); } - - - var me = this; - - // activates page tracking - if (trackRoutes) $rootScope.$on(pageEvent, function() { - me._trackPage(getUrl()); + that._log('pageview', url, title, args); + }); + }; + + /** + * Track event + https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide + https://developers.google.com/analytics/devguides/collection/analyticsjs/events + * @param category + * @param action + * @param label + * @param value + * @param noninteraction + * @private + */ + this._trackEvent = function (category, action, label, value, noninteraction) { + var that = this, args = arguments; + _gaJs(function () { + $window._gaq.push(['_trackEvent', category, action, label, value, !!noninteraction]); + that._log('trackEvent', args); + }); + _analyticsJs(function () { + $window.ga('send', 'event', category, action, label, value); + that._log('event', args); + }); + }; + + /** + * Add transaction + * https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce#_gat.GA_Tracker_._addTrans + * https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#addTrans + * @param transactionId + * @param affiliation + * @param total + * @param tax + * @param shipping + * @param city + * @param state + * @param country + * @private + */ + this._addTrans = function (transactionId, affiliation, total, tax, shipping, city, state, country, currency) { + var that = this, args = arguments; + _gaJs(function () { + $window._gaq.push(['_addTrans', transactionId, affiliation, total, tax, shipping, city, state, country]); + that._log('_addTrans', args); + }); + _analyticsJs(function () { + if (that._ecommerceEnabled()) { + $window.ga('ecommerce:addTransaction', { + id: transactionId, + affiliation: affiliation, + revenue: total, + tax: tax, + shipping: shipping, + currency: currency || 'USD' }); - - return { - _logs: me._logs, - cookieConfig: cookieConfig, - displayFeatures: displayFeatures, - ecommerce: ecommerce, - enhancedEcommerce: enhancedEcommerce, - enhancedLinkAttribution: enhancedLinkAttribution, - getUrl: getUrl, - experimentId: experimentId, - ignoreFirstPageLoad: ignoreFirstPageLoad, - trackPage: function(url, title) { - // add a page event - me._trackPage(url, title); - }, - trackEvent: function(category, action, label, value) { - // add an action event - me._trackEvent(category, action, label, value); - }, - addTrans: function (transactionId, affiliation, total, tax, shipping, city, state, country, currency) { - me._addTrans(transactionId, affiliation, total, tax, shipping, city, state, country, currency); - }, - addItem: function (transactionId, sku, name, category, price, quantity) { - me._addItem(transactionId, sku, name, category, price, quantity); - }, - trackTrans: function () { - me._trackTrans(); - }, - clearTrans: function () { - me._clearTrans(); - }, - addProduct: function (productId,name,category,brand,variant,price,quantity,coupon,position){ - me._addProduct(productId,name,category,brand,variant,price,quantity,coupon,position); - }, - addPromo: function(productId, name, creative, position){ - me._addPromo(productId, name, creative, position); - }, - addImpression: function(productId, name, list, brand, category, variant, position, price){ - me._addImpression(productId, name, list, brand, category, variant, position, price); - }, - productClick: function(listName){ - me._productClick(listName); - }, - promoClick : function (promotionName){ - me._promoClick(promotionName); - }, - trackDetail: function(){ - me._setAction('detail'); - me._pageView(); - }, - trackCart: function(action){ - me._trackCart(action); - }, - trackCheckout: function(step,option){ - me._trackCheckOut(step,option); - }, - trackTransaction: function(transactionId,affiliation,revenue,tax,shipping,coupon,list,step,option){ - me._trackTransaction(transactionId,affiliation,revenue,tax,shipping,coupon,list,step,option); - }, - setAction: function (action,obj) { - me._setAction(action,obj); - }, - send: function (obj) { - me._send(obj); - }, - pageView: function () { - me._pageView(); - }, - set: function (name, value) { - me._set(name, value); - } - }; - }]; - - }); + that._log('ecommerce:addTransaction', args); + } + }); + }; + + /** + * Add item to transaction + * https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce#_gat.GA_Tracker_._addItem + * https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#addItem + * @param transactionId + * @param sku + * @param name + * @param category + * @param price + * @param quantity + * @private + */ + this._addItem = function (transactionId, sku, name, category, price, quantity) { + var that = this, args = arguments; + _gaJs(function () { + $window._gaq.push(['_addItem', transactionId, sku, name, category, price, quantity]); + that._log('_addItem', args); + }); + _analyticsJs(function () { + if (that._ecommerceEnabled()) { + $window.ga('ecommerce:addItem', { + id: transactionId, + name: name, + sku: sku, + category: category, + price: price, + quantity: quantity + }); + that._log('ecommerce:addItem', args); + } + }); + }; + + /** + * Track transaction + * https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiEcommerce#_gat.GA_Tracker_._trackTrans + * https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#sendingData + * @private + */ + this._trackTrans = function () { + var that = this, args = arguments; + _gaJs(function () { + $window._gaq.push(['_trackTrans']); + that._log('_trackTrans', args); + }); + _analyticsJs(function () { + if (that._ecommerceEnabled()) { + $window.ga('ecommerce:send'); + that._log('ecommerce:send', args); + } + }); + }; + + /** + * Clear transaction + * https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#clearingData + * @private + */ + this._clearTrans = function () { + var that = this, args = arguments; + _analyticsJs(function () { + if (that._ecommerceEnabled()) { + $window.ga('ecommerce:clear'); + that._log('ecommerce:clear', args); + } + }); + }; + + /** + * Enhanced Ecommerce + */ + + /** + * Add product data + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#product-data + * @param productId + * @param name + * @param category + * @param brand + * @param variant + * @param price + * @param quantity + * @param coupon + * @param position + */ + this._addProduct = function (productId, name, category, brand, variant, price, quantity, coupon, position) { + var that = this, args = arguments; + _gaJs(function () { + $window._gaq.push(['_addProduct', productId, name, category, brand, variant, price, quantity, coupon, position]); + that._log('_addProduct', args); + }); + _analyticsJs(function () { + if (that._enhancedEcommerceEnabled()) { + $window.ga('ec:addProduct', { + id: productId, + name: name, + category: category, + brand: brand, + variant: variant, + price: price, + quantity: quantity, + coupon: coupon, + position: position + }); + that._log('ec:addProduct', args); + } + }); + }; + + /** + * Add Impression data + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#impression-data + * @param id + * @param name + * @param list + * @param brand + * @param category + * @param variant + * @param position + * @param price + */ + this._addImpression = function (id, name, list, brand, category, variant, position, price){ + var that = this, args = arguments; + _gaJs(function () { + $window._gaq.push(['_addImpression', id, name, list, brand, category, variant, position, price]); + that._log('_addImpression', args); + }); + _analyticsJs(function () { + if (that._enhancedEcommerceEnabled()) { + $window.ga('ec:addImpression', { + id: id, + name: name, + category: category, + brand: brand, + variant: variant, + list: list, + position: position, + price: price + }); + } + that._log('ec:addImpression', args); + }); + }; + + /** + * Add promo data + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce + * @param productId + * @param name + * @param creative + * @param position + */ + this._addPromo = function (productId, name, creative, position) { + var that = this, args = arguments; + _gaJs(function () { + $window._gaq.push(['_addPromo', productId, name, creative, position]); + that._log('_addPromo', arguments); + }); + _analyticsJs(function () { + if (that._enhancedEcommerceEnabled()) { + $window.ga('ec:addPromo', { + id: productId, + name: name, + creative: creative, + position: position + }); + that._log('ec:addPromo', args); + } + }); + }; + + /** + * get ActionFieldObject + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#action-data + * @param id + * @param affliation + * @param revenue + * @param tax + * @param shipping + * @param coupon + * @param list + * @param step + * @param option + */ + this._getActionFieldObject = function (id, affiliation, revenue, tax, shipping, coupon, list, step, option) { + var obj = {}; + if (id) { obj.id = id; } + if (affiliation) { obj.affiliation = affiliation; } + if (revenue) { obj.revenue = revenue; } + if (tax) { obj.tax = tax; } + if (shipping) { obj.shipping = shipping; } + if (coupon) { obj.coupon = coupon; } + if (list) { obj.list = list; } + if (step) { obj.step = step; } + if (option) { obj.option = option; } + return obj; + }; + + /** + * Set Action being performed + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-actions + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#action-types + * @param action + * @param name + * @param obj + */ + this._setAction = function (action, obj){ + var that = this, args = arguments; + _gaJs(function () { + $window._gaq.push(['_setAction', action, obj]); + that._log('__setAction', args); + }); + _analyticsJs(function () { + if (that._enhancedEcommerceEnabled()) { + $window.ga('ec:setAction', action, obj); + that._log('ec:setAction', args); + } + }); + }; + + /** + * Track Transaction + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-transactions + * @param transactionId + * @param affiliation + * @param revenue + * @param tax + * @param shipping + * @param coupon + * @param list + * @param step + * @param option + */ + this._trackTransaction = function (transactionId, affiliation, revenue, tax, shipping, coupon, list, step, option) { + this._setAction('purchase', this._getActionFieldObject(transactionId, affiliation, revenue, tax, shipping, coupon, list, step, option)); + this._pageView(); + }; + + /** + * Track Refund + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-refunds + * @param transactionId + * + */ + this._trackRefund = function (transactionId) { + this._setAction('refund', this._getActionFieldObject(transactionId)); + this._pageView(); + }; + + /** + * Track Checkout + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-checkout + * @param step + * @param option + * + */ + this._trackCheckOut = function (step, option) { + this._setAction('checkout', this._getActionFieldObject(null, null, null, null, null, null, null, step, option)); + this._pageView(); + }; + + /** + * Track add/remove to cart + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#add-remove-cart + * @param action + * + */ + this._trackCart = function (action) { + if (['add', 'remove'].indexOf(action) !== -1) { + this._setAction(action); + this._send('event', 'UX', 'click', action + 'to cart'); + } + }; + + /** + * Track promo click + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-promo-clicks + * @param promotionName + * + */ + this._promoClick = function (promotionName) { + this._setAction('promo_click'); + this._send('event', 'Internal Promotions', 'click', promotionName); + }; + + /** + * Track product click + * https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-promo-clicks + * @param promotionName + * + */ + this._productClick = function (listName) { + this._setAction('click', this._getActionFieldObject(null, null, null, null, null, null, listName, null, null)); + this._send('event', 'UX', 'click', listName); + }; + + /** + * Send custom events + * https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings#implementation + * https://developers.google.com/analytics/devguides/collection/analyticsjs/social-interactions#implementation + * + * @param obj + * @private + */ + this._send = function (obj) { + var that = this; + _analyticsJs(function () { + $window.ga('send', obj); + that._log('send', obj); + }); + }; + + this._pageView = function() { + this._send('pageview'); + }; + + /** + * Set custom dimensions, metrics or experiment + * https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets + * https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#customs + * @param name + * @param value + * @private + */ + this._set = function (name, value) { + var that = this; + _analyticsJs(function () { + $window.ga('set', name, value); + that._log('set', name, value); + }); + }; + + // creates the ganalytics tracker + if (analyticsJS) { + _createAnalyticsScriptTag(); + } else { + _createScriptTag(); + } + + // activates page tracking + if (trackRoutes) { + $rootScope.$on(pageEvent, function () { + me._trackPage(); + }); + } + + return { + _logs: me._logs, + cookieConfig: cookieConfig, + displayFeatures: displayFeatures, + ecommerce: ecommerce, + enhancedEcommerce: enhancedEcommerce, + enhancedLinkAttribution: enhancedLinkAttribution, + getUrl: getUrl, + experimentId: experimentId, + ignoreFirstPageLoad: ignoreFirstPageLoad, + ecommerceEnabled: function () { + return me._ecommerceEnabled(); + }, + enhancedEcommerceEnabled: function () { + return me._enhancedEcommerceEnabled(); + }, + trackPage: function (url, title) { + me._trackPage(url, title); + }, + trackEvent: function (category, action, label, value, noninteraction) { + me._trackEvent(category, action, label, value, noninteraction); + }, + addTrans: function (transactionId, affiliation, total, tax, shipping, city, state, country, currency) { + me._addTrans(transactionId, affiliation, total, tax, shipping, city, state, country, currency); + }, + addItem: function (transactionId, sku, name, category, price, quantity) { + me._addItem(transactionId, sku, name, category, price, quantity); + }, + trackTrans: function () { + me._trackTrans(); + }, + clearTrans: function () { + me._clearTrans(); + }, + addProduct: function (productId, name, category, brand, variant, price, quantity, coupon, position) { + me._addProduct(productId, name, category, brand, variant, price, quantity, coupon, position); + }, + addPromo: function (productId, name, creative, position) { + me._addPromo(productId, name, creative, position); + }, + addImpression: function (productId, name, list, brand, category, variant, position, price) { + me._addImpression(productId, name, list, brand, category, variant, position, price); + }, + productClick: function (listName) { + me._productClick(listName); + }, + promoClick : function (promotionName) { + me._promoClick(promotionName); + }, + trackDetail: function () { + me._setAction('detail'); + me._pageView(); + }, + trackCart: function (action) { + me._trackCart(action); + }, + trackCheckout: function (step, option) { + me._trackCheckOut(step, option); + }, + trackTransaction: function (transactionId, affiliation, revenue, tax, shipping, coupon, list, step, option){ + me._trackTransaction(transactionId, affiliation, revenue, tax, shipping, coupon, list, step, option); + }, + setAction: function (action, obj) { + me._setAction(action, obj); + }, + send: function (obj) { + me._send(obj); + }, + pageView: function () { + me._pageView(); + }, + set: function (name, value) { + me._set(name, value); + } + }; + }]; + }); \ No newline at end of file diff --git a/dist/angular-google-analytics.min.js b/dist/angular-google-analytics.min.js index bcd1803..68ee837 100644 --- a/dist/angular-google-analytics.min.js +++ b/dist/angular-google-analytics.min.js @@ -1,8 +1,8 @@ /** * Angular Google Analytics - Easy tracking for your AngularJS application - * @version v0.0.8 - 2014-10-26 + * @version v0.0.9 - 2014-11-24 * @link http://github.com/revolunet/angular-google-analytics * @author Julien Bouquillon * @license MIT License, http://www.opensource.org/licenses/MIT */ -"use strict";angular.module("angular-google-analytics",[]).provider("Analytics",function(){var e,t,n,a,i,c,s=!1,r=!0,o="",u=!1,l="$routeChangeSuccess",g="auto",m=!1,d=!1,_=!1,h=!1,p=!1,f={allowLinker:!0};this._logs=[],this.setAccount=function(t){return e=t,!0},this.trackPages=function(e){return r=e,!0},this.trackPrefix=function(e){return o=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 p=!!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 m=!!e,d=!!t,!0},this.setRemoveRegExp=function(e){return e instanceof RegExp?(a=e,!0):!1},this.setExperimentId=function(e){return i=e,!0},this.ignoreFirstPageLoad=function(e){h=!!e},this.$get=["$document","$rootScope","$location","$window",function(k,v,w,y){function A(){if(e){y._gaq=[],y._gaq.push(["_setAccount",e]),n&&y._gaq.push(["_setDomainName",n]),_&&y._gaq.push(["_require","inpage_linkid","//www.google-analytics.com/plugins/ga/inpage_linkid.js"]),r&&!h&&(a?y._gaq.push(["_trackPageview",P()]):y._gaq.push(["_trackPageview"]));var i;i=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=k[0],t=e.createElement("script");t.type="text/javascript",t.async=!0,t.src=i;var n=e.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)}(i),s=!0}}function E(){if(!e)return console.warn("No account id set for Analytics.js");if(function(e,t,n,a,i,c,s){e.GoogleAnalyticsObject=i,e[i]=e[i]||function(){(e[i].q=e[i].q||[]).push(arguments)},e[i].l=1*new Date,c=t.createElement(n),s=t.getElementsByTagName(n)[0],c.async=1,c.src=a,s.parentNode.insertBefore(c,s)}(window,document,"script","//www.google-analytics.com/analytics.js","ga"),angular.isArray(e)?e.forEach(function(e){y.ga("create",e.tracker,g,{name:e.name})}):p?(y.ga("create",e,g,f),y.ga("require","linker"),c&&y.ga("linker:autoLink",c)):y.ga("create",e,g),t&&y.ga("require","displayfeatures"),r&&!h&&y.ga("send","pageview",P()),y.ga&&(m&&(d?y.ga("require","ec","ec.js"):y.ga("require","ecommerce","ecommerce.js")),_&&y.ga("require","linkid","linkid.js"),i)){var n=document.createElement("script"),a=document.getElementsByTagName("script")[0];n.src="//www.google-analytics.com/cx/api.js?experiment="+i,a.parentNode.insertBefore(n,a)}}var P=function(){var e=w.path();return a?e.replace(a,""):e};this._log=function(){this._logs.push(arguments)},this._trackPage=function(t,n){n=n?n:k[0].title,r&&!u&&y._gaq?(y._gaq.push(["_set","title",n]),y._gaq.push(["_trackPageview",o+t]),this._log("_trackPageview",arguments)):r&&u&&y.ga&&(angular.isArray(e)?e.forEach(function(e){y.ga(e.name+".send","pageview",{page:o+t,title:n})}):y.ga("send","pageview",{page:o+t,title:n}),this._log("pageview",arguments))},this._trackEvent=function(e,t,n,a){!u&&y._gaq?(y._gaq.push(["_trackEvent",e,t,n,a]),this._log("trackEvent",arguments)):y.ga&&(y.ga("send","event",e,t,n,a),this._log("event",arguments))},this._addTrans=function(e,t,n,a,i,c,s,r,o){!u&&y._gaq?(y._gaq.push(["_addTrans",e,t,n,a,i,c,s,r]),this._log("_addTrans",arguments)):y.ga&&(m?d?console.warn("Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. Use AnalyticsProvider.setECommerce(true,false);"):(y.ga("ecommerce:addTransaction",{id:e,affiliation:t,revenue:n,tax:a,shipping:i,currency:o||"USD"}),this._log("ecommerce:addTransaction",arguments)):console.warn("ecommerce no set. Use AnalyticsProvider.setECommerce(true,false);"))},this._addItem=function(e,t,n,a,i,c){!u&&y._gaq?(y._gaq.push(["_addItem",e,t,n,a,i,c]),this._log("_addItem",arguments)):y.ga&&(m?d?console.warn("Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. Use AnalyticsProvider.setECommerce(true,false);"):(y.ga("ecommerce:addItem",{id:e,name:n,sku:t,category:a,price:i,quantity:c}),this._log("ecommerce:addItem",arguments)):console.warn("ecommerce no set. Use AnalyticsProvider.setECommerce(true,false);"))},this._trackTrans=function(){!u&&y._gaq?(y._gaq.push(["_trackTrans"]),this._log("_trackTrans",arguments)):y.ga&&(m?d?console.warn("Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. Use AnalyticsProvider.setECommerce(true,false);"):(y.ga("ecommerce:send"),this._log("ecommerce:send",arguments)):console.warn("ecommerce no set. Use AnalyticsProvider.setECommerce(true,false);"))},this._clearTrans=function(){y.ga&&(m?d?console.warn("Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. Use AnalyticsProvider.setECommerce(true,false);"):(y.ga("ecommerce:clear"),this._log("ecommerce:clear",arguments)):console.warn("ecommerce no set. Use AnalyticsProvider.setECommerce(true,false);"))},this._addProduct=function(e,t,n,a,i,c,s,r,o){!u&&y._gaq?(y._gaq.push(["_addProduct",e,t,n,a,i,c,s,r,o]),this._log("_addProduct",arguments)):y.ga&&(m?d?(y.ga("ec:addProduct",{id:e,name:t,category:n,brand:a,variant:i,price:c,quantity:s,coupon:r,position:o}),this._log("ec:addProduct",arguments)):console.warn("Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true,true);"):console.warn("ecommerce not set. Use AnalyticsProvider.setECommerce(true,true);"))},this._addImpression=function(e,t,n,a,i,c,s,r){!u&&y._gaq?(y._gaq.push(["_addImpression",e,t,n,a,i,c,s,r]),this._log("_addImpression",arguments)):y.ga&&(m?d?y.ga("ec:addImpression",{id:e,name:t,category:i,brand:a,variant:c,list:n,position:s,price:r}):console.warn("Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true,true);"):console.warn("ecommerce not set. Use AnalyticsProvider.setECommerce(true,true);"),this._log("ec:addImpression",arguments))},this._addPromo=function(e,t,n,a){!u&&y._gaq?(y._gaq.push(["_addPromo",e,t,n,a]),this._log("_addPromo",arguments)):y.ga&&(m?d?(y.ga("ec:addPromo",{id:e,name:t,creative:n,position:a}),this._log("ec:addPromo",arguments)):console.warn("Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true,true);"):console.warn("ecommerce not set. Use AnalyticsProvider.setECommerce(true,true);"))},this._getActionFieldObject=function(e,t,n,a,i,c,s,r,o){var u={};return e&&(u.id=e),t&&(u.affiliation=t),n&&(u.revenue=n),a&&(u.tax=a),i&&(u.shipping=i),c&&(u.coupon=c),s&&(u.list=s),r&&(u.step=r),o&&(u.option=o),u},this._setAction=function(e,t){!u&&y._gaq?(y._gaq.push(["_setAction",e,t]),this._log("__setAction",arguments)):y.ga&&(m?d?(y.ga("ec:setAction",e,t),this._log("ec:setAction",arguments)):console.warn("Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true,true);"):console.warn("ecommerce not set. Use AnalyticsProvider.setECommerce(true,true);"))},this._trackTransaction=function(e,t,n,a,i,c,s,r,o){this._setAction("purchase",this._getActionFieldObject(e,t,n,a,i,c,s,r,o)),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(e){y.ga&&(y.ga("send",e),this._log("send",e))},this._pageView=function(){this._send("pageview")},this._set=function(e,t){y.ga&&(y.ga("set",e,t),this._log("set",e,t))},u?E():A();var q=this;return r&&v.$on(l,function(){q._trackPage(P())}),{_logs:q._logs,cookieConfig:g,displayFeatures:t,ecommerce:m,enhancedEcommerce:d,enhancedLinkAttribution:_,getUrl:P,experimentId:i,ignoreFirstPageLoad:h,trackPage:function(e,t){q._trackPage(e,t)},trackEvent:function(e,t,n,a){q._trackEvent(e,t,n,a)},addTrans:function(e,t,n,a,i,c,s,r,o){q._addTrans(e,t,n,a,i,c,s,r,o)},addItem:function(e,t,n,a,i,c){q._addItem(e,t,n,a,i,c)},trackTrans:function(){q._trackTrans()},clearTrans:function(){q._clearTrans()},addProduct:function(e,t,n,a,i,c,s,r,o){q._addProduct(e,t,n,a,i,c,s,r,o)},addPromo:function(e,t,n,a){q._addPromo(e,t,n,a)},addImpression:function(e,t,n,a,i,c,s,r){q._addImpression(e,t,n,a,i,c,s,r)},productClick:function(e){q._productClick(e)},promoClick:function(e){q._promoClick(e)},trackDetail:function(){q._setAction("detail"),q._pageView()},trackCart:function(e){q._trackCart(e)},trackCheckout:function(e,t){q._trackCheckOut(e,t)},trackTransaction:function(e,t,n,a,i,c,s,r,o){q._trackTransaction(e,t,n,a,i,c,s,r,o)},setAction:function(e,t){q._setAction(e,t)},send:function(e){q._send(e)},pageView:function(){q._pageView()},set:function(e,t){q._set(e,t)}}}]}); \ No newline at end of file +"use strict";angular.module("angular-google-analytics",[]).provider("Analytics",function(){var e,t,n,i,c,a,o=!1,r=!0,s="",u=!1,d="$routeChangeSuccess",l="auto",g=!1,m=!1,_=!1,h=!1,f=!1,p={allowLinker:!0};this._logs=[],this.setAccount=function(t){return e=t,!0},this.trackPages=function(e){return r=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 f=!!e,!0},this.setCrossLinkDomains=function(e){return a=e,!0},this.setPageEvent=function(e){return d=e,!0},this.setCookieConfig=function(e){return l=e,!0},this.useECommerce=function(e,t){return g=!!e,m=!!t,!0},this.setRemoveRegExp=function(e){return e instanceof RegExp?(i=e,!0):!1},this.setExperimentId=function(e){return c=e,!0},this.ignoreFirstPageLoad=function(e){return h=!!e,!0},this.$get=["$document","$location","$log","$rootScope","$window",function(k,v,E,w,y){function A(e){!u&&y._gaq&&"function"==typeof e&&e()}function P(e){u&&y.ga&&"function"==typeof e&&e()}function b(){if(!e)return C._log("warn","No account id set to create script tag"),void 0;y._gaq=[],y._gaq.push(["_setAccount",e]),n&&y._gaq.push(["_setDomainName",n]),_&&y._gaq.push(["_require","inpage_linkid","//www.google-analytics.com/plugins/ga/inpage_linkid.js"]),r&&!h&&(i?y._gaq.push(["_trackPageview",T()]):y._gaq.push(["_trackPageview"]));var c;c=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=k[0],t=e.createElement("script");t.type="text/javascript",t.async=!0,t.src=c;var n=e.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)}(c),o=!0}function q(){if(!e)return C._log("warn","No account id set to create analytics script tag"),void 0;if(function(e,t,n,i,c,a,o){e.GoogleAnalyticsObject=c,e[c]=e[c]||function(){(e[c].q=e[c].q||[]).push(arguments)},e[c].l=1*new Date,a=t.createElement(n),o=t.getElementsByTagName(n)[0],a.async=1,a.src=i,o.parentNode.insertBefore(a,o)}(window,document,"script","//www.google-analytics.com/analytics.js","ga"),angular.isArray(e)?e.forEach(function(e){y.ga("create",e.tracker,l,{name:e.name})}):f?(y.ga("create",e,l,p),y.ga("require","linker"),a&&y.ga("linker:autoLink",a)):y.ga("create",e,l),t&&y.ga("require","displayfeatures"),r&&!h&&y.ga("send","pageview",T()),y.ga&&(g&&(m?y.ga("require","ec","ec.js"):y.ga("require","ecommerce","ecommerce.js")),_&&y.ga("require","linkid","linkid.js"),c)){var n=document.createElement("script"),i=document.getElementsByTagName("script")[0];n.src="//www.google-analytics.com/cx/api.js?experiment="+c,i.parentNode.insertBefore(n,i)}}var C=this,T=function(){var e=v.path();return i?e.replace(i,""):e};return this._log=function(){arguments.length>0&&(arguments.length>1&&"warn"===arguments[0]&&E.warn(Array.prototype.slice.call(arguments,1)),this._logs.push(arguments))},this._ecommerceEnabled=function(){return g?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 g?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){var i=this,c=arguments;t=t?t:T(),n=n?n:k[0].title,A(function(){y._gaq.push(["_set","title",n]),y._gaq.push(["_trackPageview",s+t]),i._log("_trackPageview",t,n,c)}),P(function(){angular.isArray(e)?e.forEach(function(e){y.ga(e.name+".send","pageview",{page:s+t,title:n})}):y.ga("send","pageview",{page:s+t,title:n}),i._log("pageview",t,n,c)})},this._trackEvent=function(e,t,n,i,c){var a=this,o=arguments;A(function(){y._gaq.push(["_trackEvent",e,t,n,i,!!c]),a._log("trackEvent",o)}),P(function(){y.ga("send","event",e,t,n,i),a._log("event",o)})},this._addTrans=function(e,t,n,i,c,a,o,r,s){var u=this,d=arguments;A(function(){y._gaq.push(["_addTrans",e,t,n,i,c,a,o,r]),u._log("_addTrans",d)}),P(function(){u._ecommerceEnabled()&&(y.ga("ecommerce:addTransaction",{id:e,affiliation:t,revenue:n,tax:i,shipping:c,currency:s||"USD"}),u._log("ecommerce:addTransaction",d))})},this._addItem=function(e,t,n,i,c,a){var o=this,r=arguments;A(function(){y._gaq.push(["_addItem",e,t,n,i,c,a]),o._log("_addItem",r)}),P(function(){o._ecommerceEnabled()&&(y.ga("ecommerce:addItem",{id:e,name:n,sku:t,category:i,price:c,quantity:a}),o._log("ecommerce:addItem",r))})},this._trackTrans=function(){var e=this,t=arguments;A(function(){y._gaq.push(["_trackTrans"]),e._log("_trackTrans",t)}),P(function(){e._ecommerceEnabled()&&(y.ga("ecommerce:send"),e._log("ecommerce:send",t))})},this._clearTrans=function(){var e=this,t=arguments;P(function(){e._ecommerceEnabled()&&(y.ga("ecommerce:clear"),e._log("ecommerce:clear",t))})},this._addProduct=function(e,t,n,i,c,a,o,r,s){var u=this,d=arguments;A(function(){y._gaq.push(["_addProduct",e,t,n,i,c,a,o,r,s]),u._log("_addProduct",d)}),P(function(){u._enhancedEcommerceEnabled()&&(y.ga("ec:addProduct",{id:e,name:t,category:n,brand:i,variant:c,price:a,quantity:o,coupon:r,position:s}),u._log("ec:addProduct",d))})},this._addImpression=function(e,t,n,i,c,a,o,r){var s=this,u=arguments;A(function(){y._gaq.push(["_addImpression",e,t,n,i,c,a,o,r]),s._log("_addImpression",u)}),P(function(){s._enhancedEcommerceEnabled()&&y.ga("ec:addImpression",{id:e,name:t,category:c,brand:i,variant:a,list:n,position:o,price:r}),s._log("ec:addImpression",u)})},this._addPromo=function(e,t,n,i){var c=this,a=arguments;A(function(){y._gaq.push(["_addPromo",e,t,n,i]),c._log("_addPromo",arguments)}),P(function(){c._enhancedEcommerceEnabled()&&(y.ga("ec:addPromo",{id:e,name:t,creative:n,position:i}),c._log("ec:addPromo",a))})},this._getActionFieldObject=function(e,t,n,i,c,a,o,r,s){var u={};return e&&(u.id=e),t&&(u.affiliation=t),n&&(u.revenue=n),i&&(u.tax=i),c&&(u.shipping=c),a&&(u.coupon=a),o&&(u.list=o),r&&(u.step=r),s&&(u.option=s),u},this._setAction=function(e,t){var n=this,i=arguments;A(function(){y._gaq.push(["_setAction",e,t]),n._log("__setAction",i)}),P(function(){n._enhancedEcommerceEnabled()&&(y.ga("ec:setAction",e,t),n._log("ec:setAction",i))})},this._trackTransaction=function(e,t,n,i,c,a,o,r,s){this._setAction("purchase",this._getActionFieldObject(e,t,n,i,c,a,o,r,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(e){var t=this;P(function(){y.ga("send",e),t._log("send",e)})},this._pageView=function(){this._send("pageview")},this._set=function(e,t){var n=this;P(function(){y.ga("set",e,t),n._log("set",e,t)})},u?q():b(),r&&w.$on(d,function(){C._trackPage()}),{_logs:C._logs,cookieConfig:l,displayFeatures:t,ecommerce:g,enhancedEcommerce:m,enhancedLinkAttribution:_,getUrl:T,experimentId:c,ignoreFirstPageLoad:h,ecommerceEnabled:function(){return C._ecommerceEnabled()},enhancedEcommerceEnabled:function(){return C._enhancedEcommerceEnabled()},trackPage:function(e,t){C._trackPage(e,t)},trackEvent:function(e,t,n,i,c){C._trackEvent(e,t,n,i,c)},addTrans:function(e,t,n,i,c,a,o,r,s){C._addTrans(e,t,n,i,c,a,o,r,s)},addItem:function(e,t,n,i,c,a){C._addItem(e,t,n,i,c,a)},trackTrans:function(){C._trackTrans()},clearTrans:function(){C._clearTrans()},addProduct:function(e,t,n,i,c,a,o,r,s){C._addProduct(e,t,n,i,c,a,o,r,s)},addPromo:function(e,t,n,i){C._addPromo(e,t,n,i)},addImpression:function(e,t,n,i,c,a,o,r){C._addImpression(e,t,n,i,c,a,o,r)},productClick:function(e){C._productClick(e)},promoClick:function(e){C._promoClick(e)},trackDetail:function(){C._setAction("detail"),C._pageView()},trackCart:function(e){C._trackCart(e)},trackCheckout:function(e,t){C._trackCheckOut(e,t)},trackTransaction:function(e,t,n,i,c,a,o,r,s){C._trackTransaction(e,t,n,i,c,a,o,r,s)},setAction:function(e,t){C._setAction(e,t)},send:function(e){C._send(e)},pageView:function(){C._pageView()},set:function(e,t){C._set(e,t)}}}]}); \ No newline at end of file diff --git a/package.json b/package.json index 179dd03..b8cccbd 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "angular-google-analytics", "description": "Angular Google Analytics - Easy tracking for your AngularJS application", - "version": "0.0.8", + "version": "0.0.9", "homepage": "http://github.com/revolunet/angular-google-analytics", "author": "Julien Bouquillon ", "main": "./src/angular-google-analytics.js", @@ -17,7 +17,7 @@ "type": "MIT", "url": "http://revolunet.mit-license.org" } - ], + ], "dependencies": {}, "devDependencies": { "bower": "*", @@ -35,4 +35,4 @@ "test": "karma start test/karma.conf.js --single-run --browsers Chrome", "test-server": "karma start test/karma.conf.js --browsers Chrome" } -} \ No newline at end of file +} diff --git a/test/unit/angular-google-analytics.js b/test/unit/angular-google-analytics.js index b04fb98..d69bae0 100644 --- a/test/unit/angular-google-analytics.js +++ b/test/unit/angular-google-analytics.js @@ -434,7 +434,7 @@ describe('angular-google-analytics', function () { position: '1' }); }); - }) + }); }); it('should add promo data', function () { @@ -643,4 +643,4 @@ describe('angular-google-analytics', function () { }); }); }); -}); \ No newline at end of file +});