Skip to content

Commit 4dfd78a

Browse files
authored
fix: events not being sent when adTrackingEnabled is false (#1024)
* 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. --------- Co-authored-by: Sunita Prajapati <>
1 parent fe0ab8f commit 4dfd78a

File tree

2 files changed

+93
-61
lines changed

2 files changed

+93
-61
lines changed

packages/plugins/plugin-advertising-id/android/src/main/java/com/reactnativeanalyticsreactnativepluginadvertisingid/AnalyticsReactNativePluginAdvertisingIdModule.kt

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,47 @@ class AnalyticsReactNativePluginAdvertisingIdModule(reactContext: ReactApplicati
1818
return "AnalyticsReactNativePluginAdvertisingId"
1919
}
2020

21-
@ReactMethod
21+
@ReactMethod
2222
fun getAdvertisingId(promise: Promise) {
23-
if (currentActivity?.application == null) {
24-
promise.resolve(null)
25-
return
26-
}
27-
28-
val reactContext = (currentActivity?.application as ReactApplication)
29-
?.reactNativeHost
30-
?.reactInstanceManager
31-
?.currentReactContext
32-
33-
if (reactContext == null) {
34-
promise.resolve(null)
35-
return
36-
}
37-
38-
try {
39-
val advertisingInfo = AdvertisingIdClient.getAdvertisingIdInfo(reactContext)
40-
val isLimitAdTrackingEnabled = advertisingInfo.isLimitAdTrackingEnabled
41-
42-
if (isLimitAdTrackingEnabled) {
43-
promise.resolve(null)
44-
}
45-
46-
val id = advertisingInfo.id
47-
val advertisingId = id.toString()
48-
promise.resolve(advertisingId)
49-
}
50-
catch (e: GooglePlayServicesNotAvailableException) {
51-
Log.d(name, e.toString())
52-
promise.resolve(null)
53-
}
54-
catch ( e: IOException) {
55-
Log.d(name, e.toString())
56-
promise.resolve(null)
57-
}
23+
getAdvertisingIdInfo(promise) { advertisingInfo ->
24+
val id = advertisingInfo.id
25+
promise.resolve(id.toString())
26+
}
27+
}
28+
29+
@ReactMethod
30+
fun getIsLimitAdTrackingEnableStatus(promise: Promise) {
31+
getAdvertisingIdInfo(promise) { advertisingInfo ->
32+
val isLimitAdTrackingEnabled = advertisingInfo.isLimitAdTrackingEnabled
33+
promise.resolve(isLimitAdTrackingEnabled)
34+
}
5835
}
36+
37+
private fun getAdvertisingIdInfo(promise: Promise, callback: (AdvertisingIdClient.Info) -> Unit) {
38+
if (currentActivity?.application == null) {
39+
promise.resolve(null)
40+
return
41+
}
42+
43+
val reactContext = (currentActivity?.application as ReactApplication)
44+
?.reactNativeHost
45+
?.reactInstanceManager
46+
?.currentReactContext
47+
48+
if (reactContext == null) {
49+
promise.resolve(null)
50+
return
51+
}
52+
53+
try {
54+
val advertisingInfo = AdvertisingIdClient.getAdvertisingIdInfo(reactContext)
55+
callback(advertisingInfo)
56+
} catch (e: GooglePlayServicesNotAvailableException) {
57+
Log.d(name, e.toString())
58+
promise.resolve(null)
59+
} catch (e: IOException) {
60+
Log.d(name, e.toString())
61+
promise.resolve(null)
62+
}
63+
}
5964
}

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

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,70 @@ import {
55
getNativeModule,
66
ErrorType,
77
SegmentError,
8-
SegmentEvent
8+
SegmentEvent,
99
} from '@segment/analytics-react-native';
1010

1111
import { Platform, NativeModule } from 'react-native';
1212

1313
type AdvertisingIDNativeModule = NativeModule & {
1414
getAdvertisingId: () => Promise<string>;
15+
getIsLimitAdTrackingEnableStatus: () => Promise<boolean>;
1516
};
1617

1718
export class AdvertisingIdPlugin extends Plugin {
1819
type = PluginType.enrichment;
1920
queuedEvents: SegmentEvent[] = [];
2021
advertisingId?: string = undefined;
22+
isLimitAdTracking?: boolean = false;
2123

2224
configure(analytics: SegmentClient): void {
2325
if (Platform.OS !== 'android') {
2426
return;
2527
}
2628

2729
this.analytics = analytics;
28-
(
29-
getNativeModule(
30-
'AnalyticsReactNativePluginAdvertisingId'
31-
) as AdvertisingIDNativeModule
32-
)
33-
?.getAdvertisingId()
34-
.then((id: string) => {
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
3537
if (id === null) {
38+
// Need to check this condition
3639
void analytics.track(
3740
'LimitAdTrackingEnabled (Google Play Services) is enabled'
3841
);
3942
} else {
40-
this.advertisingId = id
41-
void this.setContext(id);
43+
this.advertisingId = id;
4244
}
45+
//handle isLimitAdTrackingEnableStatus
46+
this.isLimitAdTracking = status;
47+
48+
// Call setContext after both values are available
49+
void this.setContext(this.advertisingId, status);
4350
})
44-
.catch((error) => {
45-
this.analytics?.reportInternalError(
46-
new SegmentError(
47-
ErrorType.PluginError,
48-
'Error retrieving AdvertisingID',
49-
error
50-
)
51-
);
52-
});
51+
.catch((error) => this.handleError(error));
5352
}
5453

55-
execute(event: SegmentEvent){
56-
54+
execute(event: SegmentEvent) {
5755
if (this.advertisingId === undefined) {
5856
this.queuedEvents.push(event);
59-
}else{
57+
} else {
6058
return event;
6159
}
6260
return;
6361
}
6462

65-
async setContext(id: string): Promise<void> {
63+
async setContext(
64+
id: string | undefined,
65+
isLimitAdTrackingEnableStatus: boolean
66+
): Promise<void> {
6667
try {
6768
await this.analytics?.context.set({
6869
device: {
6970
advertisingId: id,
70-
adTrackingEnabled: true,
71+
adTrackingEnabled: !isLimitAdTrackingEnableStatus,
7172
},
7273
});
7374
this.sendQueued();
@@ -81,9 +82,35 @@ export class AdvertisingIdPlugin extends Plugin {
8182
}
8283

8384
sendQueued() {
84-
this.queuedEvents.forEach(event => {
85+
this.queuedEvents.forEach((event) => {
8586
void this.analytics?.process(event);
8687
});
8788
this.queuedEvents = [];
8889
}
90+
91+
private fetchAdvertisingId(): Promise<string | null> {
92+
return (
93+
getNativeModule(
94+
'AnalyticsReactNativePluginAdvertisingId'
95+
) as AdvertisingIDNativeModule
96+
)?.getAdvertisingId();
97+
}
98+
99+
private fetchLimitAdTrackingStatus(): Promise<boolean> {
100+
return (
101+
getNativeModule(
102+
'AnalyticsReactNativePluginAdvertisingId'
103+
) as AdvertisingIDNativeModule
104+
)?.getIsLimitAdTrackingEnableStatus();
105+
}
106+
107+
private handleError(error: unknown): void {
108+
this.analytics?.reportInternalError(
109+
new SegmentError(
110+
ErrorType.PluginError,
111+
'Error retrieving AdvertisingID',
112+
error
113+
)
114+
);
115+
}
89116
}

0 commit comments

Comments
 (0)