I think this library doesn't actually do the right thing. Real issue being the goofy way Segment has done their JS. When the analytics script is loaded, it doesn't actually fix the initial window.analytics
, it just replaces it, and the fact that this module exports a reference to the initial object, means that after the script is loaded, events aren't actually sent to Segment and tracked.
I could be wrong, but that is what I was observing.
import loadAnalytics from 'analytics.js-loader';
/*
* 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.
*/
const initialAnalytics = loadAnalytics({
writeKey: 'KEY',
skipPageCall: true
});
window.analytics = initialAnalytics;
const analytics = {};
_.each(initialAnalytics.methods, method => {
analytics[method] = function() {
return window.analytics[method].apply(window.analytics, arguments);
};
});
export default analytics;