diff --git a/src/angular-google-analytics.js b/src/angular-google-analytics.js index b546148..950d421 100644 --- a/src/angular-google-analytics.js +++ b/src/angular-google-analytics.js @@ -167,6 +167,18 @@ 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 _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'); @@ -181,7 +193,30 @@ 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; + var options; + if (_checkOption('crossDomainLinker', trackerObj)) { + trackerObj.allowLinker = trackerObj.crossDomainLinker; + } + 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); + } + if (options && 'allowLinker' in options && options.allowLinker) { + $window.ga(_generateCommandName('require', trackerObj), 'linker'); + if (_checkOption('crossLinkDomains', trackerObj)) { + $window.ga(_generateCommandName('linker:autoLink', trackerObj), trackerObj.crossLinkDomains); + } + } }); } else if (crossDomainLinker) { $window.ga('create', accountId, cookieConfig, linkerConfig); @@ -274,7 +309,7 @@ angular.module('angular-google-analytics', []) _analyticsJs(function () { if (angular.isArray(accountId)) { accountId.forEach(function (trackerObj) { - $window.ga(trackerObj.name + '.send', 'pageview', { + $window.ga(_generateCommandName('send', trackerObj), 'pageview', { 'page': trackPrefix + url, 'title': title }); @@ -307,7 +342,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 (_checkOption('trackEvent', trackerObj)) { + $window.ga(_generateCommandName('send', trackerObj), 'event', category, action, label, value); + } + }); + } else { + $window.ga('send', 'event', category, action, label, value); + } that._log('event', args); }); }; diff --git a/test/unit/angular-google-analytics.js b/test/unit/angular-google-analytics.js index d69bae0..134d874 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,9 +640,80 @@ 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' }); }); }); }); }); }); + + 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'] }, + { 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'); + }); + }); + }); + }); });