Skip to content

Commit

Permalink
Merge pull request googleanalytics#24 from googleanalytics/provide-order
Browse files Browse the repository at this point in the history
Allow sourcing autotrack.js before the snippet
  • Loading branch information
philipwalton committed Feb 26, 2016
2 parents 060ae12 + acb035e commit bd6a112
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 7 deletions.
2 changes: 1 addition & 1 deletion autotrack.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion autotrack.js.map

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions lib/provide.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ var constants = require('./constants');
* @param {Function} pluginConstructor The plugin constructor function.
*/
module.exports = function providePlugin(pluginName, pluginConstructor) {
var ga = window[window.GoogleAnalyticsObject || 'ga'];
if (typeof ga == 'function') {
ga('provide', pluginName, pluginConstructor);
}
var w = window;
var g = w.GoogleAnalyticsObject || 'ga';

// Creates the global command queue if it's not defined.
w[g] = w[g] || function(){(w[g].q=w[g].q||[]).push(arguments)};
w[g].l = w[g].l || +new Date;

w[g]('provide', pluginName, pluginConstructor);
};
29 changes: 29 additions & 0 deletions test/autotrack-rename-ga.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!doctype html>
<html>
<body>

<script>
(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)
})(window,document,'script','//www.google-analytics.com/analytics.js','_ga');

_ga('create', 'UA-XXXXX-Y', 'auto');
_ga('require', 'autotrack');

// Note(philipwalton):
// Selenium on Windows 10 Edge doesn't handle arrays well, so we fake it.
var hitData = {count: 0};
_ga('set', 'sendHitTask', function(model) {
hitData[hitData.count] = {
hitType: model.get('hitType')
};
hitData.count++;
});
</script>

<script async src='//www.google-analytics.com/analytics.js'></script>
<script async src="/autotrack.js"></script>

</body>
</html>
27 changes: 27 additions & 0 deletions test/autotrack-source-order.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<html>
<body>

<script src="/autotrack.js"></script>

<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;

ga('create', 'UA-XXXXX-Y', 'auto');
ga('require', 'autotrack');

// Note(philipwalton):
// Selenium on Windows 10 Edge doesn't handle arrays well, so we fake it.
var hitData = {count: 0};
ga('set', 'sendHitTask', function(model) {
hitData[hitData.count] = {
hitType: model.get('hitType')
};
hitData.count++;
});
</script>

<script async src='//www.google-analytics.com/analytics.js'></script>

</body>
</html>
50 changes: 49 additions & 1 deletion test/autotrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,54 @@ describe('autotrack', function() {
assert(gaplugins.UrlChangeTracker);
});


it('should provide plugins even if sourced before the tracking snippet',
function *() {

var gaplugins = (yield browser
.url('/test/autotrack-source-order.html')
.execute(getGaPlugins))
.value;

assert(gaplugins.Autotrack);
assert(gaplugins.EventTracker);
assert(gaplugins.MediaQueryTracker);
assert(gaplugins.OutboundFormTracker);
assert(gaplugins.OutboundLinkTracker);
assert(gaplugins.SocialTracker);
assert(gaplugins.UrlChangeTracker);

var hitData = (yield browser
.execute(sendPageview)
.execute(getHitData)).value;

assert(hitData.count === 1);
});


it('should work with renaming the global object', function *() {

var gaplugins = (yield browser
.url('/test/autotrack-rename-ga.html')
.execute(getGaPlugins))
.value;

assert(gaplugins.Autotrack);
assert(gaplugins.EventTracker);
assert(gaplugins.MediaQueryTracker);
assert(gaplugins.OutboundFormTracker);
assert(gaplugins.OutboundLinkTracker);
assert(gaplugins.SocialTracker);
assert(gaplugins.UrlChangeTracker);

var hitData = (yield browser
.execute(sendPageview)
.execute(getHitData)).value;

assert(hitData.count === 1);
});


it('should include the &did param with all hits', function() {

return browser
Expand All @@ -50,7 +98,7 @@ describe('autotrack', function() {


function sendPageview() {
ga('send', 'pageview');
window[window.GoogleAnalyticsObject || 'ga']('send', 'pageview');
}


Expand Down

0 comments on commit bd6a112

Please sign in to comment.