vvo / analytics.js-loader Goto Github PK
View Code? Open in Web Editor NEWAsynchronously load segment.com analytics.js with an npm module
License: ISC License
Asynchronously load segment.com analytics.js with an npm module
License: ISC License
In the single page application I am working on we call analytics.page()
whenever we render a new page. The issue is, on initial page load, this will be fired twice since it is also called at the end of index.js.
Could an option be passed to omit this call in case a consumer of this library wants to manually call it later?
The analytics object returned by the loader should have the reset method.
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.
Here is the module I created to fix this issue:
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;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.