From 44baf964b536dad8be6fa53ca945283cb7084dc4 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 11:38:45 +0100 Subject: [PATCH 01/15] allow to setup separate cookieConfig for each account --- src/angular-google-analytics.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index b546148..078312a 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -181,7 +181,8 @@ angular.module('angular-google-analytics', []) if (angular.isArray(accountId)) { accountId.forEach(function (trackerObj) { - $window.ga('create', trackerObj.tracker, cookieConfig, { name: trackerObj.name }); + var _cookieConfig = 'cookieConfig' in trackerObj ? trackerObj.cookieConfig : cookieConfig; + $window.ga('create', trackerObj.tracker, _cookieConfig, { name: trackerObj.name }); }); } else if (crossDomainLinker) { $window.ga('create', accountId, cookieConfig, linkerConfig); From a40f8a5162090888cd3ee4e01db4002a755ecaef Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 11:52:07 +0100 Subject: [PATCH 02/15] more advanced configuration options for multiple accounts --- src/angular-google-analytics.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index 078312a..305a556 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -182,7 +182,20 @@ angular.module('angular-google-analytics', []) if (angular.isArray(accountId)) { accountId.forEach(function (trackerObj) { var _cookieConfig = 'cookieConfig' in trackerObj ? trackerObj.cookieConfig : cookieConfig; - $window.ga('create', trackerObj.tracker, _cookieConfig, { name: trackerObj.name }); + var options = undefined; + angular.forEach(['name', 'allowLinker'], function(key) { + if (key in trackerObj) { + if (angular.isUndefined(options)) { + options = {}; + } + options[key] = trackerObj[key]; + } + }); + if (angular.isUndefined(options)) { + $window.ga('create', trackerObj.tracker, _cookieConfig); + } else { + $window.ga('create', trackerObj.tracker, _cookieConfig, options); + } }); } else if (crossDomainLinker) { $window.ga('create', accountId, cookieConfig, linkerConfig); From 49f55972ee393ce1f228a4c14e447a3a19bbfd6f Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 11:52:32 +0100 Subject: [PATCH 03/15] allow undefined name for multiple accounts --- src/angular-google-analytics.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index 305a556..41ed8d0 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -288,7 +288,8 @@ angular.module('angular-google-analytics', []) _analyticsJs(function () { if (angular.isArray(accountId)) { accountId.forEach(function (trackerObj) { - $window.ga(trackerObj.name + '.send', 'pageview', { + var sendCmd = 'name' in trackerObj ? trackerObj.name + '.send' : 'send'; + $window.ga(sendCmd, 'pageview', { 'page': trackPrefix + url, 'title': title }); From 86548c8a018291b837ffd0c445c9a30815629c05 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 12:15:42 +0100 Subject: [PATCH 04/15] add private method to generate command name --- src/angular-google-analytics.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index 41ed8d0..8dc775f 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -167,6 +167,14 @@ angular.module('angular-google-analytics', []) created = true; } + function _generateCommandName(commandName, config) { + if (!angular.isUndefined(config) && 'name' in config && config.name) { + return config.name+'.'+commandName; + } else { + return commandName; + } + } + function _createAnalyticsScriptTag() { if (!accountId) { me._log('warn', 'No account id set to create analytics script tag'); From 40be10c1f543a3972f70aeb67f0b9a231e525afe Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 12:16:10 +0100 Subject: [PATCH 05/15] use new name generator --- src/angular-google-analytics.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index 8dc775f..e1e264b 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -296,8 +296,7 @@ angular.module('angular-google-analytics', []) _analyticsJs(function () { if (angular.isArray(accountId)) { accountId.forEach(function (trackerObj) { - var sendCmd = 'name' in trackerObj ? trackerObj.name + '.send' : 'send'; - $window.ga(sendCmd, 'pageview', { + $window.ga(_generateCommandName('send', trackerObj), 'pageview', { 'page': trackPrefix + url, 'title': title }); From 521ff1cf7c362dfa25ef091b4a0a9ce1bde13644 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 12:19:08 +0100 Subject: [PATCH 06/15] add support for event tracking for multiple accounts --- src/angular-google-analytics.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index e1e264b..3b48b4d 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -329,7 +329,15 @@ angular.module('angular-google-analytics', []) that._log('trackEvent', args); }); _analyticsJs(function () { - $window.ga('send', 'event', category, action, label, value); + if (angular.isArray(accountId)) { + accountId.forEach(function (trackerObj) { + if ('trackEvent' in trackerObj && trackerObj.trackEvent) { + $window.ga(_generateCommandName('send', trackerObj), 'event', category, action, label, value); + } + }); + } else { + $window.ga('send', 'event', category, action, label, value); + } that._log('event', args); }); }; From 373b3c7cb22c688cb0534ee4eeb88d3652823ad2 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 12:19:40 +0100 Subject: [PATCH 07/15] add support for auto linking for multiple accounts --- src/angular-google-analytics.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index 3b48b4d..7b9a50c 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -204,6 +204,12 @@ angular.module('angular-google-analytics', []) } else { $window.ga('create', trackerObj.tracker, _cookieConfig, options); } + if (options && 'allowLinker' in options && options.allowLinker) { + $window.ga(_generateCommandName('require', trackerObj), 'linker'); + if ('crossLinkDomains' in trackerObj) { + $window.ga(_generateCommandName('linker:autoLink', trackerObj), trackerObj.crossLinkDomains); + } + } }); } else if (crossDomainLinker) { $window.ga('create', accountId, cookieConfig, linkerConfig); From f408ba473712d31d742a6d787d0d1a67369ec5e9 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 15:15:45 +0100 Subject: [PATCH 08/15] cs fix --- src/angular-google-analytics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index 7b9a50c..60050ab 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -190,7 +190,7 @@ angular.module('angular-google-analytics', []) if (angular.isArray(accountId)) { accountId.forEach(function (trackerObj) { var _cookieConfig = 'cookieConfig' in trackerObj ? trackerObj.cookieConfig : cookieConfig; - var options = undefined; + var options; angular.forEach(['name', 'allowLinker'], function(key) { if (key in trackerObj) { if (angular.isUndefined(options)) { From 4d0cbedfe54637c4116fd1369052c2cd18358579 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 15:21:46 +0100 Subject: [PATCH 09/15] add test for empty tracker name name --- test/unit/angular-google-analytics.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/unit/angular-google-analytics.js b/test/unit/angular-google-analytics.js index d69bae0..eba00f1 100644 --- a/test/unit/angular-google-analytics.js +++ b/test/unit/angular-google-analytics.js @@ -612,7 +612,8 @@ describe('angular-google-analytics', function () { describe('supports multiple tracking objects', function () { var trackers = [ { tracker: 'UA-12345-12', name: "tracker1" }, - { tracker: 'UA-12345-34', name: "tracker2" } + { tracker: 'UA-12345-34', name: "tracker2" }, + { tracker: 'UA-12345-45' } ]; beforeEach(module(function (AnalyticsProvider) { @@ -626,6 +627,7 @@ describe('angular-google-analytics', function () { inject(function (Analytics) { expect($window.ga).toHaveBeenCalledWith('create', trackers[0].tracker, 'auto', { name: trackers[0].name }); expect($window.ga).toHaveBeenCalledWith('create', trackers[1].tracker, 'auto', { name: trackers[1].name }); + expect($window.ga).toHaveBeenCalledWith('create', trackers[2].tracker, 'auto'); }); }); }); @@ -638,6 +640,7 @@ describe('angular-google-analytics', function () { Analytics.trackPage('/mypage', 'My Page'); expect($window.ga).toHaveBeenCalledWith(trackers[0].name + '.send', 'pageview', { page: '/mypage', title: 'My Page' }); expect($window.ga).toHaveBeenCalledWith(trackers[1].name + '.send', 'pageview', { page: '/mypage', title: 'My Page' }); + expect($window.ga).toHaveBeenCalledWith('send', 'pageview', { page: '/mypage', title: 'My Page' }); }); }); }); From 3a8ce2593b68ac47f950cbe189958c187d17ea55 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 23:57:32 +0100 Subject: [PATCH 10/15] add crossDomainLinker option to be consistent with public api --- src/angular-google-analytics.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index 60050ab..8830635 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -191,6 +191,9 @@ angular.module('angular-google-analytics', []) accountId.forEach(function (trackerObj) { var _cookieConfig = 'cookieConfig' in trackerObj ? trackerObj.cookieConfig : cookieConfig; var options; + if ('crossDomainLinker' in trackerObj && trackerObj.crossDomainLinker) { + trackerObj.allowLinker = trackerObj.crossDomainLinker; + } angular.forEach(['name', 'allowLinker'], function(key) { if (key in trackerObj) { if (angular.isUndefined(options)) { From c31b741abd28e2046a4bce01fb13ff17cf389eb8 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 23:58:03 +0100 Subject: [PATCH 11/15] check if crossLinkDomains option is not empty --- src/angular-google-analytics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index 8830635..988bd93 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -209,7 +209,7 @@ angular.module('angular-google-analytics', []) } if (options && 'allowLinker' in options && options.allowLinker) { $window.ga(_generateCommandName('require', trackerObj), 'linker'); - if ('crossLinkDomains' in trackerObj) { + if ('crossLinkDomains' in trackerObj && trackerObj.crossLinkDomains) { $window.ga(_generateCommandName('linker:autoLink', trackerObj), trackerObj.crossLinkDomains); } } From f179b18fb20d7ab0da244d620d3909f0e65d583e Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Tue, 23 Dec 2014 23:58:37 +0100 Subject: [PATCH 12/15] add tests to cover new options --- test/unit/angular-google-analytics.js | 70 +++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/unit/angular-google-analytics.js b/test/unit/angular-google-analytics.js index eba00f1..0003051 100644 --- a/test/unit/angular-google-analytics.js +++ b/test/unit/angular-google-analytics.js @@ -646,4 +646,74 @@ describe('angular-google-analytics', function () { }); }); }); + + describe('supports advanced options for multiple traking objects', function () { + var trackers = [ + { tracker: 'UA-12345-12', name: "tracker1", crossDomainLinker: true }, + { tracker: 'UA-12345-34', name: "tracker2", crossDomainLinker: true, crossLinkDomains: ['domain-1.com'] }, + { tracker: 'UA-12345-45', crossDomainLinker: true, crossLinkDomains: ['domain-2.com'] }, + { tracker: 'UA-12345-67', cookieConfig: 'yourdomain.org' } + ]; + + beforeEach(module(function (AnalyticsProvider) { + AnalyticsProvider.setAccount(trackers); + AnalyticsProvider.useAnalytics(true); + })); + + it('should call ga require for each tracker', function () { + inject(function ($window) { + spyOn($window, 'ga'); + inject(function (Analytics) { + expect($window.ga).toHaveBeenCalledWith('tracker1.require', 'linker'); + expect($window.ga).toHaveBeenCalledWith('tracker2.require', 'linker'); + expect($window.ga).toHaveBeenCalledWith('require', 'linker'); + }); + }); + }); + + it('should call ga linker autoLink for configured tracking objects only', function () { + inject(function ($window) { + spyOn($window, 'ga'); + inject(function (Analytics) { + expect($window.ga).not.toHaveBeenCalledWith('tracker1.linker:autoLink'); + expect($window.ga).toHaveBeenCalledWith('tracker2.linker:autoLink', ['domain-1.com']); + expect($window.ga).toHaveBeenCalledWith('linker:autoLink', ['domain-2.com']); + }); + }); + }); + + it ('should call ga create with custom cookie config', function() { + inject(function ($window) { + spyOn($window, 'ga'); + inject(function (Analytics) { + expect($window.ga).toHaveBeenCalledWith('create', 'UA-12345-67', 'yourdomain.org'); + }); + }); + }); + }); + + describe('supports advanced tracking for multiple tracking objects', function () { + var trackers = [ + { tracker: 'UA-12345-12', name: "tracker1", trackEvent: true }, + { tracker: 'UA-12345-34', name: "tracker2" }, + { tracker: 'UA-12345-45', trackEvent: true } + ]; + + beforeEach(module(function (AnalyticsProvider) { + AnalyticsProvider.setAccount(trackers); + AnalyticsProvider.useAnalytics(true); + })); + + it('should track events for configured tracking objects only', function () { + inject(function ($window) { + spyOn($window, 'ga'); + inject(function (Analytics) { + Analytics.trackEvent('category', 'action', 'label', 'value'); + expect($window.ga).toHaveBeenCalledWith('tracker1.send', 'event', 'category', 'action', 'label', 'value'); + expect($window.ga).not.toHaveBeenCalledWith('tracker2.send', 'event', 'category', 'action', 'label', 'value'); + expect($window.ga).toHaveBeenCalledWith('send', 'event', 'category', 'action', 'label', 'value'); + }); + }); + }); + }); }); From 647524e27e0ff2bc1e3656609a9bdbe91c061161 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Wed, 24 Dec 2014 00:07:08 +0100 Subject: [PATCH 13/15] add helper function for option checking --- src/angular-google-analytics.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index 988bd93..a0b9052 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -175,6 +175,10 @@ angular.module('angular-google-analytics', []) } } + function _checkOption(key, config) { + return key in config && config[key]; + } + function _createAnalyticsScriptTag() { if (!accountId) { me._log('warn', 'No account id set to create analytics script tag'); @@ -191,7 +195,7 @@ angular.module('angular-google-analytics', []) accountId.forEach(function (trackerObj) { var _cookieConfig = 'cookieConfig' in trackerObj ? trackerObj.cookieConfig : cookieConfig; var options; - if ('crossDomainLinker' in trackerObj && trackerObj.crossDomainLinker) { + if (_checkOption('crossDomainLinker', trackerObj)) { trackerObj.allowLinker = trackerObj.crossDomainLinker; } angular.forEach(['name', 'allowLinker'], function(key) { @@ -209,7 +213,7 @@ angular.module('angular-google-analytics', []) } if (options && 'allowLinker' in options && options.allowLinker) { $window.ga(_generateCommandName('require', trackerObj), 'linker'); - if ('crossLinkDomains' in trackerObj && trackerObj.crossLinkDomains) { + if (_checkOption('crossLinkDomains', trackerObj)) { $window.ga(_generateCommandName('linker:autoLink', trackerObj), trackerObj.crossLinkDomains); } } @@ -340,7 +344,7 @@ angular.module('angular-google-analytics', []) _analyticsJs(function () { if (angular.isArray(accountId)) { accountId.forEach(function (trackerObj) { - if ('trackEvent' in trackerObj && trackerObj.trackEvent) { + if (_checkOption('trackEvent', trackerObj)) { $window.ga(_generateCommandName('send', trackerObj), 'event', category, action, label, value); } }); From 5653d73b21986a858664e7d532f6355aaee3548a Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Wed, 21 Jan 2015 20:59:33 +0100 Subject: [PATCH 14/15] fix typo: "traking" => "tracking" --- test/unit/angular-google-analytics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/angular-google-analytics.js b/test/unit/angular-google-analytics.js index 0003051..134d874 100644 --- a/test/unit/angular-google-analytics.js +++ b/test/unit/angular-google-analytics.js @@ -647,7 +647,7 @@ describe('angular-google-analytics', function () { }); }); - describe('supports advanced options for multiple traking objects', function () { + describe('supports advanced options for multiple tracking objects', function () { var trackers = [ { tracker: 'UA-12345-12', name: "tracker1", crossDomainLinker: true }, { tracker: 'UA-12345-34', name: "tracker2", crossDomainLinker: true, crossLinkDomains: ['domain-1.com'] }, From 65f9ee1bed0b6401c6f54c0ebb1d42c1e3c8a743 Mon Sep 17 00:00:00 2001 From: Adam Misiorny Date: Wed, 21 Jan 2015 21:01:01 +0100 Subject: [PATCH 15/15] cs fix, add space to concatenation --- src/angular-google-analytics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index a0b9052..950d421 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -169,7 +169,7 @@ angular.module('angular-google-analytics', []) function _generateCommandName(commandName, config) { if (!angular.isUndefined(config) && 'name' in config && config.name) { - return config.name+'.'+commandName; + return config.name + '.' + commandName; } else { return commandName; }