Skip to content

Commit 53ee59d

Browse files
authored
fix: Android Advertising ID does not update unless app is relaunched (#1033)
* fix: to show 'Queue restoration timeout' only once * fix: advertising id being sent for Android application installed events * chore: add privacy manifest file to core package * fix: events not being sent when adTrackingEnabled is false * fix: type mismatch inferred type is String? but String was expected * Revert "fix: type mismatch inferred type is String? but String was expected" This reverts commit 2b1e1fc. * chore: upgrade AnalyticsReactNativeExample to RN 0.76 * chore: upgrade AnalyticsReactNativeExample RN version to 0.76 * chore: removed actual write key with placeholder after testing * fix: Android Advertising ID does not update unless app is relaunched --------- Co-authored-by: Sunita Prajapati <>
1 parent bde0ebb commit 53ee59d

File tree

1 file changed

+70
-25
lines changed

1 file changed

+70
-25
lines changed

packages/plugins/plugin-advertising-id/src/AdvertisingIdPlugin.ts

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
ErrorType,
77
SegmentError,
88
SegmentEvent,
9+
EventType,
10+
TrackEventType,
911
} from '@segment/analytics-react-native';
1012

1113
import { Platform, NativeModule } from 'react-native';
@@ -18,48 +20,91 @@ type AdvertisingIDNativeModule = NativeModule & {
1820
export 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

Comments
 (0)