Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Lanning committed Oct 14, 2014
2 parents ca67179 + e61861a commit 450df5b
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 27 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module.exports = function(grunt) {
eqnull: true,
expr: true,
node: true,
'-W018': true,
globals: {
exports: true,
angular: false,
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Proudly brought to you by [@revolunet](http://twitter.com/revolunet) and [@delta
- e-commerce tracking
- multiple-domains
- ga.js and and analytics.js support
- cross-domain support
- multiple tracking objects

## install

Expand All @@ -26,20 +28,32 @@ var app = angular.module('app', ['angular-google-analytics'])
.config(function(AnalyticsProvider) {
// initial configuration
AnalyticsProvider.setAccount('UA-XXXXX-xx');
// using multiple tracking objects (analytics.js only)
// AnalyticsProvider.setAccount([
// { tracker: 'UA-12345-12', name: "tracker1" },
// { tracker: 'UA-12345-34', name: "tracker2" }
// ]);

// track all routes (or not)
AnalyticsProvider.trackPages(true);

//Optional set domain (Use 'none' for testing on localhost)
//AnalyticsProvider.setDomainName('XXX');

//Use display features plugin
AnalyticsProvider.useDisplayFeatures(true);

// url prefix (default is empty)
// - for example: when an app doesn't run in the root directory
AnalyticsProvider.trackPrefix('my-application');

// Use analytics.js instead of ga.js
AnalyticsProvider.useAnalytics(true);

// Use cross domain linking
AnalyticsProvider.useCrossDomainLinker(true);
AnalyticsProvider.setCrossLinkDomains(['domain-1.com', 'domain-2.com']);

// Ignore first page view... helpful when using hashes and whenever your bounce rate looks obscenely low.
AnalyticsProvider.ignoreFirstPageLoad(true);

Expand Down Expand Up @@ -102,6 +116,8 @@ AnalyticsProvider.setAccount('UA-XXXXX-xx');
AnalyticsProvider.trackPages(false);
//Optional set domain (Use 'none' for testing on localhost)
AnalyticsProvider.setDomainName('XXX');
//Use display features plugin
AnalyticsProvider.useDisplayFeatures(true);
//Use analytics.js instead of ga.js
AnalyticsProvider.useAnalytics(true);
// Ignore first page view.
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "revolunet",
"name": "angular-google-analytics",
"description": "Easy Analytics for your AngularJS application",
"version": "0.0.3",
"version": "0.0.6",
"homepage": "https://github.com/revolunet/angular-google-analytics",
"repository": {
"type": "git",
Expand Down
95 changes: 84 additions & 11 deletions dist/angular-google-analytics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Angular Google Analytics - Easy tracking for your AngularJS application
* @version v0.0.3 - 2014-06-05
* @link http://revolunet.com.github.com/angular-google-analytics
* @version v0.0.6 - 2014-10-14
* @link http://github.com/revolunet/angular-google-analytics
* @author Julien Bouquillon <[email protected]>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
Expand All @@ -14,6 +14,7 @@ angular.module('angular-google-analytics', [])
var created = false,
trackRoutes = true,
accountId,
displayFeatures,
trackPrefix = '',
domainName,
analyticsJS = false,
Expand All @@ -23,7 +24,10 @@ angular.module('angular-google-analytics', [])
enhancedLinkAttribution = false,
removeRegExp,
experimentId,
ignoreFirstPageLoad = false;
ignoreFirstPageLoad = false,
crossDomainLinker = false,
crossLinkDomains,
linkerConfig = {'allowLinker': true};

this._logs = [];

Expand All @@ -47,6 +51,11 @@ angular.module('angular-google-analytics', [])
return true;
};

this.useDisplayFeatures = function(val) {
displayFeatures = !!val;
return true;
};

this.useAnalytics = function(val) {
analyticsJS = !!val;
return true;
Expand All @@ -57,6 +66,16 @@ angular.module('angular-google-analytics', [])
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;
Expand Down Expand Up @@ -117,14 +136,21 @@ angular.module('angular-google-analytics', [])
$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 = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
ga.src = gaSrc;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
})(gaSrc);
created = true;
}

function _createAnalyticsScriptTag() {
if (!accountId) {
return console.warn('No account id set for Analytics.js');
Expand All @@ -135,7 +161,23 @@ angular.module('angular-google-analytics', [])
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');

$window.ga('create', accountId, cookieConfig);
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());
Expand All @@ -154,15 +196,16 @@ angular.module('angular-google-analytics', [])
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) {
Expand All @@ -171,13 +214,23 @@ angular.module('angular-google-analytics', [])
$window._gaq.push(['_trackPageview', trackPrefix + url]);
this._log('_trackPageview', arguments);
} else if (trackRoutes && analyticsJS && $window.ga) {
$window.ga('send', 'pageview', {
'page': trackPrefix + url,
'title': title
});
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]);
Expand Down Expand Up @@ -299,6 +352,22 @@ angular.module('angular-google-analytics', [])
}
};

/**
* 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
Expand All @@ -319,6 +388,7 @@ angular.module('angular-google-analytics', [])
return {
_logs: me._logs,
cookieConfig: cookieConfig,
displayFeatures: displayFeatures,
ecommerce: ecommerce,
enhancedLinkAttribution: enhancedLinkAttribution,
getUrl: getUrl,
Expand Down Expand Up @@ -346,6 +416,9 @@ angular.module('angular-google-analytics', [])
},
send: function (obj) {
me._send(obj);
},
set: function (name, value) {
me._set(name, value);
}
};
}];
Expand Down
9 changes: 8 additions & 1 deletion dist/angular-google-analytics.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "angular-google-analytics",
"description": "Angular Google Analytics - Easy tracking for your AngularJS application",
"version": "0.0.3",
"homepage": "http://revolunet.com.github.com/angular-google-analytics",
"version": "0.0.6",
"homepage": "http://github.com/revolunet/angular-google-analytics",
"author": "Julien Bouquillon <[email protected]>",
"main": "./src/angular-google-analytics.js",
"repository": {
"type": "git",
"url": "git://github.com/revolunet/angular-google-analytics.git"
Expand Down
Loading

0 comments on commit 450df5b

Please sign in to comment.