@@ -15,6 +15,7 @@ import RudderElementBuilder from "./utils/RudderElementBuilder";
15
15
import Storage from "./utils/storage" ;
16
16
import { EventRepository } from "./utils/EventRepository" ;
17
17
import logger from "./utils/logUtil" ;
18
+ import { addDomEventHandlers } from "./utils/autotrack.js" ;
18
19
19
20
//https://unpkg.com/[email protected] /dist/browser.js
20
21
@@ -40,8 +41,11 @@ class Analytics {
40
41
* @memberof Analytics
41
42
*/
42
43
constructor ( ) {
44
+ this . autoTrackHandlersRegistered = false ;
45
+ this . autoTrackFeatureEnabled = false ;
43
46
this . initialized = false ;
44
47
this . ready = false ;
48
+ this . trackValues = [ ] ;
45
49
this . eventsBuffer = [ ] ;
46
50
this . clientIntegrations = [ ] ;
47
51
this . configArray = [ ] ;
@@ -73,25 +77,42 @@ class Analytics {
73
77
* @memberof Analytics
74
78
*/
75
79
processResponse ( status , response ) {
76
- logger . debug ( "===in process response=== " + status ) ;
77
- response = JSON . parse ( response ) ;
78
- response . source . destinations . forEach ( function ( destination , index ) {
80
+ try {
81
+ logger . debug ( "===in process response=== " + status ) ;
82
+ response = JSON . parse ( response ) ;
83
+ if ( response . source . useAutoTracking ) {
84
+ this . autoTrackFeatureEnabled = true ;
85
+ addDomEventHandlers ( this ) ;
86
+ this . autoTrackHandlersRegistered = true ;
87
+ }
88
+ response . source . destinations . forEach ( function ( destination , index ) {
89
+ logger . debug (
90
+ "Destination " +
91
+ index +
92
+ " Enabled? " +
93
+ destination . enabled +
94
+ " Type: " +
95
+ destination . destinationDefinition . name +
96
+ " Use Native SDK? " +
97
+ destination . config . useNativeSDK
98
+ ) ;
99
+ if ( destination . enabled && destination . config . useNativeSDK ) {
100
+ this . clientIntegrations . push ( destination . destinationDefinition . name ) ;
101
+ this . configArray . push ( destination . config ) ;
102
+ }
103
+ } , this ) ;
104
+ this . init ( this . clientIntegrations , this . configArray ) ;
105
+ } catch ( error ) {
106
+ handleError ( error ) ;
107
+ logger . debug ( "===handling config BE response processing error===" ) ;
79
108
logger . debug (
80
- "Destination " +
81
- index +
82
- " Enabled? " +
83
- destination . enabled +
84
- " Type: " +
85
- destination . destinationDefinition . name +
86
- " Use Native SDK? " +
87
- destination . config . useNativeSDK
109
+ "autoTrackHandlersRegistered" ,
110
+ this . autoTrackHandlersRegistered
88
111
) ;
89
- if ( destination . enabled ) {
90
- this . clientIntegrations . push ( destination . destinationDefinition . name ) ;
91
- this . configArray . push ( destination . config ) ;
112
+ if ( this . autoTrackFeatureEnabled && ! this . autoTrackHandlersRegistered ) {
113
+ addDomEventHandlers ( this ) ;
92
114
}
93
- } , this ) ;
94
- this . init ( this . clientIntegrations , this . configArray ) ;
115
+ }
95
116
}
96
117
97
118
/**
@@ -144,7 +165,12 @@ class Analytics {
144
165
integrationOptions [ "All" ] )
145
166
) {
146
167
try {
147
- object . clientIntegrationObjects [ i ] [ methodName ] ( ...event ) ;
168
+ if (
169
+ ! object . clientIntegrationObjects [ i ] [ "isFailed" ] ||
170
+ ! object . clientIntegrationObjects [ i ] [ "isFailed" ] ( )
171
+ ) {
172
+ object . clientIntegrationObjects [ i ] [ methodName ] ( ...event ) ;
173
+ }
148
174
} catch ( error ) {
149
175
handleError ( error ) ;
150
176
}
@@ -243,6 +269,32 @@ class Analytics {
243
269
this . processIdentify ( userId , traits , options , callback ) ;
244
270
}
245
271
272
+ /**
273
+ *
274
+ * @param {* } to
275
+ * @param {* } from
276
+ * @param {* } options
277
+ * @param {* } callback
278
+ */
279
+ alias ( to , from , options , callback ) {
280
+ if ( typeof options == "function" ) ( callback = options ) , ( options = null ) ;
281
+ if ( typeof from == "function" )
282
+ ( callback = from ) , ( options = null ) , ( from = null ) ;
283
+ if ( typeof from == "object" ) ( options = from ) , ( from = null ) ;
284
+
285
+ let rudderElement = new RudderElementBuilder ( ) . setType ( "alias" ) . build ( ) ;
286
+ rudderElement . message . previousId =
287
+ from || this . userId ? this . userId : this . getAnonymousId ( ) ;
288
+ rudderElement . message . userId = to ;
289
+
290
+ this . processAndSendDataToDestinations (
291
+ "alias" ,
292
+ rudderElement ,
293
+ options ,
294
+ callback
295
+ ) ;
296
+ }
297
+
246
298
/**
247
299
* Send page call to Rudder BE and to initialized integrations
248
300
*
@@ -409,7 +461,9 @@ class Analytics {
409
461
) ;
410
462
logger . debug ( "anonymousId: " , this . anonymousId ) ;
411
463
rudderElement [ "message" ] [ "anonymousId" ] = this . anonymousId ;
412
- rudderElement [ "message" ] [ "userId" ] = this . userId ;
464
+ rudderElement [ "message" ] [ "userId" ] = rudderElement [ "message" ] [ "userId" ]
465
+ ? rudderElement [ "message" ] [ "userId" ]
466
+ : this . userId ;
413
467
414
468
if ( options ) {
415
469
this . processOptionsParam ( rudderElement , options ) ;
@@ -426,7 +480,9 @@ class Analytics {
426
480
integrations [ obj . name ] ||
427
481
( integrations [ obj . name ] == undefined && integrations [ "All" ] )
428
482
) {
429
- obj [ type ] ( rudderElement ) ;
483
+ if ( ! obj [ "isFailed" ] || ! obj [ "isFailed" ] ( ) ) {
484
+ obj [ type ] ( rudderElement ) ;
485
+ }
430
486
}
431
487
} ) ;
432
488
}
@@ -498,15 +554,15 @@ class Analytics {
498
554
this . storage . clear ( ) ;
499
555
}
500
556
501
- getAnonymousId ( ) {
557
+ getAnonymousId ( ) {
502
558
this . anonymousId = this . storage . getAnonymousId ( ) ;
503
- if ( ! this . anonymousId ) {
559
+ if ( ! this . anonymousId ) {
504
560
this . setAnonymousId ( ) ;
505
561
}
506
562
return this . anonymousId ;
507
563
}
508
564
509
- setAnonymousId ( anonymousId ) {
565
+ setAnonymousId ( anonymousId ) {
510
566
this . anonymousId = anonymousId ? anonymousId : generateUUID ( ) ;
511
567
this . storage . setAnonymousId ( this . anonymousId ) ;
512
568
}
@@ -527,12 +583,26 @@ class Analytics {
527
583
if ( options && options . logLevel ) {
528
584
logger . setLogLevel ( options . logLevel ) ;
529
585
}
586
+ if (
587
+ options &&
588
+ options . valTrackingList &&
589
+ options . valTrackingList . push == Array . prototype . push
590
+ ) {
591
+ this . trackValues = options . valTrackingList ;
592
+ }
530
593
logger . debug ( "inside load " ) ;
531
594
this . eventRepository . writeKey = writeKey ;
532
595
if ( serverUrl ) {
533
596
this . eventRepository . url = serverUrl ;
534
597
}
535
- getJSONTrimmed ( this , CONFIG_URL , writeKey , this . processResponse ) ;
598
+ try {
599
+ getJSONTrimmed ( this , CONFIG_URL , writeKey , this . processResponse ) ;
600
+ } catch ( error ) {
601
+ handleError ( error ) ;
602
+ if ( this . autoTrackFeatureEnabled && ! this . autoTrackHandlersRegistered ) {
603
+ addDomEventHandlers ( instance ) ;
604
+ }
605
+ }
536
606
}
537
607
}
538
608
@@ -578,10 +648,21 @@ if (process.browser) {
578
648
let identify = instance . identify . bind ( instance ) ;
579
649
let page = instance . page . bind ( instance ) ;
580
650
let track = instance . track . bind ( instance ) ;
651
+ let alias = instance . alias . bind ( instance ) ;
581
652
let reset = instance . reset . bind ( instance ) ;
582
653
let load = instance . load . bind ( instance ) ;
583
654
let initialized = ( instance . initialized = true ) ;
584
655
let getAnonymousId = instance . getAnonymousId . bind ( instance ) ;
585
656
let setAnonymousId = instance . setAnonymousId . bind ( instance ) ;
586
657
587
- export { initialized , page , track , load , identify , reset , getAnonymousId , setAnonymousId } ;
658
+ export {
659
+ initialized ,
660
+ page ,
661
+ track ,
662
+ load ,
663
+ identify ,
664
+ reset ,
665
+ alias ,
666
+ getAnonymousId ,
667
+ setAnonymousId
668
+ } ;
0 commit comments