Skip to content

Commit

Permalink
Modify load function to compensate for queueing (Fixes #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Reade Harris committed Feb 1, 2016
1 parent 0991932 commit 5871aa7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.1.2 (2016-01-31)

* fix loader to compensate for segment queue object

# 2.1.1 (2016-01-01)

* use https: protocol by default
Expand Down
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,29 @@ function load(opts) {
analytics.page();
}

return analytics;
/*
* The analytics.js-loader module returns the "initial" analytics
* object, which is basically just a queue to hold events until the
* "real" one is loaded with a global script. Problem is, the "real"
* one simply replaces `window.analytics`, and doesn't change over
* this initial "queue" object, meaning that nothing gets registered
* after the "real" one is loaded - things just keep adding to the queue.
*
* This is why we save off `initialAnalytics`, and then always run
* methods through `window.analytics`, which might be the queue, and
* might be the real one.
*/

var initialAnalytics = analytics;
var actualAnalytics = {};

window.analytics = initialAnalytics;

initialAnalytics.methods.forEach(function(method) {
actualAnalytics[method] = function() {
window.analytics[method].apply(window.analytics, arguments);
}
});

return actualAnalytics;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "analytics.js-loader",
"version": "2.1.1",
"version": "2.1.2",
"description": "Asynchronously load segment.com analytics.js with an npm module",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 5871aa7

Please sign in to comment.