66 ErrorType ,
77 SegmentError ,
88 SegmentEvent ,
9+ EventType ,
10+ TrackEventType ,
911} from '@segment/analytics-react-native' ;
1012
1113import { Platform , NativeModule } from 'react-native' ;
@@ -18,48 +20,91 @@ type AdvertisingIDNativeModule = NativeModule & {
1820export class AdvertisingIdPlugin extends Plugin {
1921 type = PluginType . enrichment ;
2022 queuedEvents : SegmentEvent [ ] = [ ] ;
21- advertisingId ? : string = undefined ;
22- isLimitAdTracking ?: boolean = false ;
23+ advertisingId : string | undefined | null = undefined ;
24+ isLimitAdTracking ?: boolean = undefined ;
2325
2426 configure ( analytics : SegmentClient ) : void {
27+ console . log ( 'configure' ) ;
2528 if ( Platform . OS !== 'android' ) {
2629 return ;
2730 }
2831
2932 this . analytics = analytics ;
30- // Create an array of promises for fetching advertising ID and limit ad tracking status
31- const advertisingIdPromise = this . fetchAdvertisingId ( ) ;
32- const limitAdTrackingStatusPromise = this . fetchLimitAdTrackingStatus ( ) ;
33- // Wait for both promises to resolve
34- Promise . all ( [ advertisingIdPromise , limitAdTrackingStatusPromise ] )
35- . then ( ( [ id , status ] ) => {
36- //handle advertisingID
37- if ( id === null ) {
38- // Need to check this condition
39- void analytics . track (
40- 'LimitAdTrackingEnabled (Google Play Services) is enabled'
41- ) ;
42- } else {
43- this . advertisingId = id ;
44- }
45- //handle isLimitAdTrackingEnableStatus
46- this . isLimitAdTracking = status ;
47-
48- // Call setContext after both values are available
49- void this . setContext ( this . advertisingId , status ) ;
33+ this . fetchAdvertisingInfo ( )
34+ . then ( ( ) => {
35+ // Additional logic after the advertising info is fetched
36+ this . sendQueued ( ) ;
5037 } )
51- . catch ( ( error ) => this . handleError ( error ) ) ;
38+ . catch ( ( error ) => {
39+ this . handleError ( error ) ;
40+ } ) ;
5241 }
5342
54- execute ( event : SegmentEvent ) {
43+ async execute ( event : SegmentEvent ) {
44+ // If advertisingId is not set, queue the event
5545 if ( this . advertisingId === undefined ) {
5646 this . queuedEvents . push ( event ) ;
5747 } else {
48+ // Send event if advertisingId is available
49+ const currentLimitAdTrackingStatus =
50+ await this . fetchLimitAdTrackingStatus ( ) ;
51+ if ( this . isLimitAdTracking === undefined ) {
52+ this . isLimitAdTracking = currentLimitAdTrackingStatus ;
53+ } else if ( this . isLimitAdTracking !== currentLimitAdTrackingStatus ) {
54+ //Fetch the fresh advertising id
55+ await this . fetchAdvertisingInfo ( )
56+ . then ( ( ) => {
57+ console . log (
58+ 'Advertising info fetched successfully when adTrackingStatus Changed.'
59+ ) ;
60+ // Additional logic after the advertising info is fetched
61+ } )
62+ . catch ( ( error ) => {
63+ this . handleError ( error ) ;
64+ } ) ;
65+ this . queuedEvents . push ( event ) ;
66+ this . isLimitAdTracking = currentLimitAdTrackingStatus ;
67+ this . sendQueued ( ) ;
68+ return ;
69+ }
5870 return event ;
5971 }
72+
6073 return ;
6174 }
6275
76+ isTrackEvent ( event : SegmentEvent ) : event is TrackEventType {
77+ return event . type === EventType . TrackEvent ;
78+ }
79+
80+ private async fetchAdvertisingInfo ( ) : Promise < void > {
81+ const advertisingIdPromise = this . fetchAdvertisingId ( ) ;
82+ const limitAdTrackingStatusPromise = this . fetchLimitAdTrackingStatus ( ) ;
83+
84+ try {
85+ // Await both promises to resolve simultaneously
86+ const [ id , status ] = await Promise . all ( [
87+ advertisingIdPromise ,
88+ limitAdTrackingStatusPromise ,
89+ ] ) ;
90+
91+ // Handle advertisingID
92+ if ( id === null ) {
93+ void this . analytics ?. track (
94+ 'LimitAdTrackingEnabled (Google Play Services) is enabled'
95+ ) ;
96+ this . advertisingId = undefined ; // Set to undefined if id is null
97+ } else {
98+ this . advertisingId = id ;
99+ }
100+
101+ // Set context after both values are available
102+ await this . setContext ( id as string , status ) ;
103+ } catch ( error ) {
104+ this . handleError ( error ) ;
105+ }
106+ }
107+
63108 async setContext (
64109 id : string | undefined ,
65110 isLimitAdTrackingEnableStatus : boolean
@@ -71,7 +116,6 @@ export class AdvertisingIdPlugin extends Plugin {
71116 adTrackingEnabled : ! isLimitAdTrackingEnableStatus ,
72117 } ,
73118 } ) ;
74- this . sendQueued ( ) ;
75119 } catch ( error ) {
76120 const message = 'AdvertisingID failed to set context' ;
77121 this . analytics ?. reportInternalError (
@@ -82,6 +126,7 @@ export class AdvertisingIdPlugin extends Plugin {
82126 }
83127
84128 sendQueued ( ) {
129+ console . log ( 'Sending queued events:' , this . queuedEvents ) ;
85130 this . queuedEvents . forEach ( ( event ) => {
86131 void this . analytics ?. process ( event ) ;
87132 } ) ;
0 commit comments