Skip to content

Commit

Permalink
Merge pull request #53 from adam187/master
Browse files Browse the repository at this point in the history
add more options for multiple tracking objects
  • Loading branch information
Julien Bouquillon committed Jan 22, 2015
2 parents 38070a6 + 65f9ee1 commit 818c601
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 4 deletions.
49 changes: 46 additions & 3 deletions src/angular-google-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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);
});
};
Expand Down
75 changes: 74 additions & 1 deletion test/unit/angular-google-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
});
});
});
Expand All @@ -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');
});
});
});
});
});

0 comments on commit 818c601

Please sign in to comment.