@@ -45,6 +45,7 @@ function Analytics() {
45
45
this . Integrations = { } ;
46
46
this . _integrations = { } ;
47
47
this . _readied = false ;
48
+ this . _readiedIntegrations = { } ;
48
49
this . _timeout = 300 ;
49
50
// XXX: BACKWARDS COMPATIBILITY
50
51
this . _user = user ;
@@ -106,6 +107,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o
106
107
107
108
this . _options ( options ) ;
108
109
this . _readied = false ;
110
+ this . _readiedIntegrations = { } ;
109
111
110
112
// clean unknown integrations from settings
111
113
var self = this ;
@@ -146,8 +148,14 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o
146
148
integration . page = after ( 2 , integration . page ) ;
147
149
}
148
150
151
+ var integrationReady = function ( ) {
152
+ self . _readiedIntegrations [ integration . name ] = true ;
153
+ self . emit ( integration . name + '-ready' ) ;
154
+ ready ( ) ;
155
+ } ;
156
+
149
157
integration . analytics = self ;
150
- integration . once ( 'ready' , ready ) ;
158
+ integration . once ( 'ready' , integrationReady ) ;
151
159
integration . initialize ( ) ;
152
160
} , integrations ) ;
153
161
@@ -496,14 +504,55 @@ Analytics.prototype.alias = function(to, from, options, fn) {
496
504
return this ;
497
505
} ;
498
506
507
+ /**
508
+ * Register a `fn` to be fired when integrations are ready.
509
+ * If the first parameter is a function `fn`, `fn` is firsted when all
510
+ * integrations are ready.
511
+ * If the first parameter is a name and the second parameter is a function `fn`,
512
+ * `fn` is fired when the given integration is ready.
513
+ *
514
+ * It is recommended that you use the latter, as the global ready callback may
515
+ * not be fired if a single integration fails to load.
516
+ * See https://github.com/segmentio/analytics.js/issues/409.
517
+ *
518
+ * @param {(Function|String) } a
519
+ * @param {Function } b
520
+ * @return {Analytics }
521
+ */
522
+
523
+ Analytics . prototype . ready = function ( a , b ) {
524
+ if ( is . fn ( a ) ) {
525
+ return this . _ready ( a ) ;
526
+ }
527
+ return this . _integrationReady ( a , b ) ;
528
+ } ;
529
+
530
+ /**
531
+ * Register a `fn` to be fired when the given analytics service is ready.
532
+ *
533
+ * @param {Function } fn
534
+ * @return {Analytics }
535
+ */
536
+
537
+ Analytics . prototype . _integrationReady = function ( name , fn ) {
538
+ if ( is . fn ( fn ) ) {
539
+ if ( this . _readiedIntegrations [ name ] ) {
540
+ nextTick ( fn ) ;
541
+ } else {
542
+ this . once ( name + '-ready' , fn ) ;
543
+ }
544
+ }
545
+ return this ;
546
+ } ;
547
+
499
548
/**
500
549
* Register a `fn` to be fired when all the analytics services are ready.
501
550
*
502
551
* @param {Function } fn
503
552
* @return {Analytics }
504
553
*/
505
554
506
- Analytics . prototype . ready = function ( fn ) {
555
+ Analytics . prototype . _ready = function ( fn ) {
507
556
if ( is . fn ( fn ) ) {
508
557
if ( this . _readied ) {
509
558
nextTick ( fn ) ;
0 commit comments