Skip to content

Commit

Permalink
Merge pull request #49 from justinsa/master
Browse files Browse the repository at this point in the history
Changed console to $log usage
  • Loading branch information
Julien Bouquillon committed Nov 11, 2014
2 parents 69e00ee + ebe5d28 commit 3055c1a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 29 deletions.
54 changes: 33 additions & 21 deletions src/angular-google-analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global angular, console */
/* global angular */

'use strict';

Expand Down Expand Up @@ -108,7 +108,9 @@ angular.module('angular-google-analytics', [])
/**
* Public Service
*/
this.$get = ['$document', '$rootScope', '$location', '$window', function ($document, $rootScope, $location, $window) {
this.$get = ['$document', '$location', '$log', '$rootScope', '$window', function ($document, $location, $log, $rootScope, $window) {
var me = this;

var getUrl = function () {
var url = $location.path();
if (removeRegExp) {
Expand All @@ -123,10 +125,12 @@ angular.module('angular-google-analytics', [])

function _createScriptTag() //noinspection JSValidateTypes
{
// inject the google analytics tag
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]);
Expand All @@ -141,12 +145,12 @@ angular.module('angular-google-analytics', [])
}
}
var gaSrc;
if(displayFeatures) {
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() {
(function () {
var document = $document[0];
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = gaSrc;
Expand All @@ -157,9 +161,11 @@ angular.module('angular-google-analytics', [])

function _createAnalyticsScriptTag() {
if (!accountId) {
return console.warn('No account id set for Analytics.js');
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);
Expand All @@ -179,7 +185,7 @@ angular.module('angular-google-analytics', [])
$window.ga('create', accountId, cookieConfig);
}

if(displayFeatures) {
if (displayFeatures) {
$window.ga('require', 'displayfeatures');
}

Expand Down Expand Up @@ -208,17 +214,21 @@ angular.module('angular-google-analytics', [])
}

this._log = function () {
// for testing
//console.info('analytics log:', arguments);
if (arguments.length === 0) {
return;
}
if (arguments.length > 1 && arguments[0] === 'warn') {
$log.warn(Array.prototype.slice.call(arguments, 1));
}
this._logs.push(arguments);
};

this._ecommerceEnabled = function () {
if (!ecommerce) {
console.warn('ecommerce not set. Use AnalyticsProvider.setECommerce(true, false);');
this._log('warn', 'ecommerce not set. Use AnalyticsProvider.setECommerce(true, false);');
return false;
} else if (enhancedEcommerce) {
console.warn('Enhanced ecommerce plugin is enabled. Only one plugin(ecommerce/ec) can be used at a time. ' +
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;
}
Expand All @@ -227,10 +237,10 @@ angular.module('angular-google-analytics', [])

this._enhancedEcommerceEnabled = function () {
if (!ecommerce) {
console.warn('ecommerce not set. Use AnalyticsProvider.setECommerce(true, true);');
this._log('warn', 'ecommerce not set. Use AnalyticsProvider.setECommerce(true, true);');
return false;
} else if (!enhancedEcommerce) {
console.warn('Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true, true);');
this._log('warn', 'Enhanced ecommerce plugin is disabled. Use AnalyticsProvider.setECommerce(true, true);');
return false;
}
return true;
Expand Down Expand Up @@ -631,19 +641,15 @@ angular.module('angular-google-analytics', [])
_createScriptTag();
}

var me = this;

// activates page tracking
if (trackRoutes) {
$rootScope.$on(pageEvent, function() {
$rootScope.$on(pageEvent, function () {
me._trackPage();
});
}

return {
_logs: me._logs,
_ecommerceEnabled: me._ecommerceEnabled,
_enhancedEcommerceEnabled: me._enhancedEcommerceEnabled,
cookieConfig: cookieConfig,
displayFeatures: displayFeatures,
ecommerce: ecommerce,
Expand All @@ -652,6 +658,12 @@ angular.module('angular-google-analytics', [])
getUrl: getUrl,
experimentId: experimentId,
ignoreFirstPageLoad: ignoreFirstPageLoad,
ecommerceEnabled: function () {
return me._ecommerceEnabled();
},
enhancedEcommerceEnabled: function () {
return me._enhancedEcommerceEnabled();
},
trackPage: function (url, title) {
// add a page event
me._trackPage(url, title);
Expand Down Expand Up @@ -695,13 +707,13 @@ angular.module('angular-google-analytics', [])
me._trackCart(action);
},
trackCheckout: function (step, option) {
me._trackCheckOut(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);
me._trackTransaction(transactionId, affiliation, revenue, tax, shipping, coupon, list, step, option);
},
setAction: function (action, obj) {
me._setAction(action,obj);
me._setAction(action, obj);
},
send: function (obj) {
me._send(obj);
Expand Down
68 changes: 60 additions & 8 deletions test/unit/angular-google-analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global module, angular, console, describe, expect, it, before, beforeEach, inject, spyOn, AnalyticsProvider */
/* global angular, before, beforeEach, describe, expect, inject, it, module, spyOn */

'use strict';

Expand All @@ -8,6 +8,58 @@ describe('angular-google-analytics', function () {
AnalyticsProvider.setAccount('UA-XXXXXX-xx');
}));

describe('required settings missing', function () {
describe('for default ga script injection', function () {
beforeEach(module(function (AnalyticsProvider) {
AnalyticsProvider.setAccount(false);
AnalyticsProvider.useAnalytics(false);
}));

it('should not inject a script tag', function () {
inject(function (Analytics) {
expect(document.querySelectorAll("script[src='http://www.google-analytics.com/ga.js']").length).toBe(0);
});
});

it('should issue a warning to the log', function () {
inject(function ($log) {
spyOn($log, 'warn');
inject(function (Analytics) {
expect(Analytics._logs.length).toBe(1);
expect(Analytics._logs[0][0]).toBe('warn');
expect(Analytics._logs[0][1]).toBe('No account id set to create script tag');
expect($log.warn).toHaveBeenCalledWith(['No account id set to create script tag']);
});
});
});
});

describe('for analytics script injection', function () {
beforeEach(module(function (AnalyticsProvider) {
AnalyticsProvider.setAccount(false);
AnalyticsProvider.useAnalytics(true);
}));

it('should not inject a script tag', function () {
inject(function (Analytics) {
expect(document.querySelectorAll("script[src='//www.google-analytics.com/analytics.js']").length).toBe(0);
});
});

it('should issue a warning to the log', function () {
inject(function ($log) {
spyOn($log, 'warn');
inject(function (Analytics) {
expect(Analytics._logs.length).toBe(1);
expect(Analytics._logs[0][0]).toBe('warn');
expect(Analytics._logs[0][1]).toBe('No account id set to create analytics script tag');
expect($log.warn).toHaveBeenCalledWith(['No account id set to create analytics script tag']);
});
});
});
});
});

describe('automatic trackPages with ga.js', function () {
it('should inject the GA script', function () {
inject(function (Analytics) {
Expand Down Expand Up @@ -93,12 +145,12 @@ describe('angular-google-analytics', function () {
});
});

describe('Supports ignoreFirstPageLoad', function () {
describe('supports ignoreFirstPageLoad', function () {
beforeEach(module(function (AnalyticsProvider) {
AnalyticsProvider.ignoreFirstPageLoad(true);
}));

it('Supports ignoreFirstPageLoad config', function () {
it('supports ignoreFirstPageLoad config', function () {
inject(function (Analytics, $rootScope) {
expect(Analytics.ignoreFirstPageLoad).toBe(true);
});
Expand Down Expand Up @@ -127,7 +179,7 @@ describe('angular-google-analytics', function () {
});
});

it('should support displayfeatures config', function () {
it('should support displayFeatures config', function () {
inject(function (Analytics) {
expect(Analytics.displayFeatures).toBe(true);
});
Expand Down Expand Up @@ -205,13 +257,13 @@ describe('angular-google-analytics', function () {

it('should have ecommerce enabled', function () {
inject(function (Analytics) {
expect(Analytics._ecommerceEnabled()).toBe(true);
expect(Analytics.ecommerceEnabled()).toBe(true);
});
});

it('should have enhanced ecommerce disabled', function () {
inject(function (Analytics) {
expect(Analytics._enhancedEcommerceEnabled()).toBe(false);
expect(Analytics.enhancedEcommerceEnabled()).toBe(false);
});
});

Expand Down Expand Up @@ -251,13 +303,13 @@ describe('angular-google-analytics', function () {

it('should have ecommerce disabled', function () {
inject(function (Analytics) {
expect(Analytics._ecommerceEnabled()).toBe(false);
expect(Analytics.ecommerceEnabled()).toBe(false);
});
});

it('should have enhanced ecommerce enabled', function () {
inject(function (Analytics) {
expect(Analytics._enhancedEcommerceEnabled()).toBe(true);
expect(Analytics.enhancedEcommerceEnabled()).toBe(true);
});
});

Expand Down

0 comments on commit 3055c1a

Please sign in to comment.