Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 419df20

Browse files
committed
Add analytics.ready(name, fn) function
See segmentio/analytics.js#409 This adds the ability to set a ready callback per integration. todo: * document the function behaviour (need help here on what syntax to use) * test for already readied integration
1 parent 3b48c44 commit 419df20

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

lib/analytics.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o
106106

107107
this._options(options);
108108
this._readied = false;
109+
this._readiedIntegrations = {};
109110

110111
// clean unknown integrations from settings
111112
var self = this;
@@ -146,8 +147,14 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o
146147
integration.page = after(2, integration.page);
147148
}
148149

150+
var integrationReady = function() {
151+
self._readiedIntegrations[integration.name] = true;
152+
self.emit(integration.name + '-ready');
153+
ready();
154+
};
155+
149156
integration.analytics = self;
150-
integration.once('ready', ready);
157+
integration.once('ready', integrationReady);
151158
integration.initialize();
152159
}, integrations);
153160

@@ -496,14 +503,56 @@ Analytics.prototype.alias = function(to, from, options, fn) {
496503
return this;
497504
};
498505

506+
/**
507+
* Register a `fn` to be fired when integrations are ready.
508+
* If the first parameter is a function `fn`, `fn` is firsted when all
509+
* integrations are ready.
510+
* If the first parameter is a name and the second parameter is a function `fn`,
511+
* `fn` is fired when the given integration is ready.
512+
*
513+
* It is recommended that you use the latter, as the global ready callback may
514+
* not be fired if a single integration fails to load.
515+
* See https://github.com/segmentio/analytics.js/issues/409.
516+
*
517+
* @param {(Function|String)} a
518+
* @param {Function} b
519+
* @return {Analytics}
520+
*/
521+
522+
Analytics.prototype.ready = function(a, b) {
523+
if (is.fn(a)) {
524+
return this._ready(a);
525+
}
526+
return this._integrationReady(a, b);
527+
};
528+
529+
/**
530+
* Register a `fn` to be fired when the given analytics service is ready.
531+
*
532+
* @param {Function} fn
533+
* @return {Analytics}
534+
*/
535+
536+
Analytics.prototype._integrationReady = function(name, fn) {
537+
if (is.fn(fn)) {
538+
var integrations = this._readiedIntegrations || {};
539+
if (integrations[name]) {
540+
nextTick(fn);
541+
} else {
542+
this.once(name + '-ready', fn);
543+
}
544+
}
545+
return this;
546+
};
547+
499548
/**
500549
* Register a `fn` to be fired when all the analytics services are ready.
501550
*
502551
* @param {Function} fn
503552
* @return {Analytics}
504553
*/
505554

506-
Analytics.prototype.ready = function(fn) {
555+
Analytics.prototype._ready = function(fn) {
507556
if (is.fn(fn)) {
508557
if (this._readied) {
509558
nextTick(fn);

test/analytics.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ describe('Analytics', function() {
159159
analytics.initialize({ Unknown: { key: 'key' } });
160160
});
161161

162+
it('should listen on integration ready events for integration', function(done) {
163+
Test.readyOnInitialize();
164+
analytics.addIntegration(Test);
165+
analytics.ready('Test', done);
166+
analytics.initialize(settings);
167+
});
168+
162169
it('should set analytics._readied to true', function(done) {
163170
analytics.ready(function() {
164171
assert(analytics._readied);

0 commit comments

Comments
 (0)