Skip to content

Commit 618e30f

Browse files
fix: fixed native module to get advertisingID (#1097)
Changes are done to fix the github issue raised here Background: In December 2024, RN released v0.75, which had major architectural changes. One of the biggest changes was how RN 0.75+ pipe data between the RN Javascript layer, vs. native layer. Problem Statement: Our native module started failing after 0.76 RN version Fix: Right now we had made changes to provide the quick fix by updating the native module. But this is short term solution, in long run, it's better we convert our legacy bridge-based native module to Turbo Module
1 parent 7123479 commit 618e30f

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed
Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11
package com.analyticsreactnativepluginadvertisingid
22

3-
import com.facebook.react.bridge.ReactApplicationContext
4-
import com.facebook.react.bridge.ReactContextBaseJavaModule
5-
import com.facebook.react.bridge.ReactMethod
6-
import com.facebook.react.bridge.Promise
7-
import com.facebook.react.ReactApplication
8-
import com.google.android.gms.ads.identifier.AdvertisingIdClient
9-
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
3+
import com.facebook.react.bridge.*
104
import com.facebook.react.module.annotations.ReactModule
11-
import android.util.Log
12-
import java.io.IOException;
13-
14-
15-
@ReactModule(name="AnalyticsReactNativePluginAdvertisingId")
16-
class AnalyticsReactNativePluginAdvertisingIdModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
17-
override fun getName(): String {
18-
return "AnalyticsReactNativePluginAdvertisingId"
19-
}
20-
21-
@ReactMethod
22-
fun getAdvertisingId(promise: Promise) {
23-
getAdvertisingIdInfo(promise) { advertisingInfo ->
24-
val id = advertisingInfo.id
25-
promise.resolve(id.toString())
26-
}
27-
}
5+
import com.google.android.gms.ads.identifier.AdvertisingIdClient
6+
import kotlinx.coroutines.*
287

29-
@ReactMethod
30-
fun getIsLimitAdTrackingEnableStatus(promise: Promise) {
31-
getAdvertisingIdInfo(promise) { advertisingInfo ->
32-
val isLimitAdTrackingEnabled = advertisingInfo.isLimitAdTrackingEnabled
33-
promise.resolve(isLimitAdTrackingEnabled)
34-
}
35-
}
8+
@ReactModule(name = "AnalyticsReactNativePluginAdvertisingId")
369

37-
private fun getAdvertisingIdInfo(promise: Promise, callback: (AdvertisingIdClient.Info) -> Unit) {
38-
if (currentActivity?.application == null) {
39-
promise.resolve(null)
40-
return
41-
}
10+
class AnalyticsReactNativePluginAdvertisingIdModule(
11+
private val reactContext: ReactApplicationContext
12+
) : ReactContextBaseJavaModule(reactContext) {
4213

43-
val reactContext = (currentActivity?.application as ReactApplication)
44-
?.reactNativeHost
45-
?.reactInstanceManager
46-
?.currentReactContext
14+
override fun getName(): String {
15+
return "AnalyticsReactNativePluginAdvertisingId"
16+
}
4717

48-
if (reactContext == null) {
49-
promise.resolve(null)
50-
return
51-
}
18+
/**
19+
* Return only the advertising ID string
20+
*/
21+
@ReactMethod
22+
fun getAdvertisingId(promise: Promise) {
23+
Thread {
24+
try {
25+
val info = AdvertisingIdClient.getAdvertisingIdInfo(reactContext)
26+
promise.resolve(info.id ?: "")
27+
} catch (e: Exception) {
28+
promise.reject("ERROR", e)
29+
}
30+
}.start()
31+
}
32+
/**
33+
* Return only the "is limit ad tracking enabled" status
34+
*/
35+
@ReactMethod
36+
fun getIsLimitAdTrackingEnableStatus(promise: Promise) {
37+
Thread {
38+
try {
39+
val info = AdvertisingIdClient.getAdvertisingIdInfo(reactContext)
40+
promise.resolve(info.isLimitAdTrackingEnabled ?: false)
41+
} catch (e: Exception) {
42+
promise.reject("ERROR", e)
43+
}
44+
}.start()
45+
}
5246

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-
}
47+
/**
48+
* Return both values together
49+
*/
50+
@ReactMethod
51+
fun getAdvertisingInfo(promise: Promise) {
52+
Thread {
53+
try {
54+
val info = AdvertisingIdClient.getAdvertisingIdInfo(reactContext)
55+
val result = Arguments.createMap()
56+
result.putString("advertisingId", info.id ?: "")
57+
result.putBoolean("isLimitAdTrackingEnabled", info.isLimitAdTrackingEnabled ?: false)
58+
promise.resolve(result)
59+
} catch (e: Exception) {
60+
promise.reject("ERROR", e)
61+
}
62+
}.start()
6363
}
6464
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,26 @@ export class AdvertisingIdPlugin extends Plugin {
9191
const limitAdTrackingStatusPromise = this.fetchLimitAdTrackingStatus();
9292

9393
try {
94-
// Await both promises to resolve simultaneously
95-
const [id, status] = await Promise.all([
94+
// eslint-disable-next-line prefer-const
95+
let [id, status] = await Promise.all([
9696
advertisingIdPromise,
9797
limitAdTrackingStatusPromise,
9898
]);
99+
// Handle null status (e.g., native failure) by assuming enabled if id null
100+
if (id === null && status === null) {
101+
status = true; // Assume enabled on failure
102+
}
99103

100-
// Handle advertisingID
101104
if (id === null) {
102-
void this.analytics?.track(
103-
'LimitAdTrackingEnabled (Google Play Services) is enabled'
104-
);
105-
this.advertisingId = undefined; // Set to undefined if id is null
105+
this.advertisingId = null; // CHANGED: Use null for unavailable, not undefined
106106
} else {
107107
this.advertisingId = id;
108108
}
109109

110-
// Set context after both values are available
111-
await this.setContext(id as string, status);
110+
// NEW: Set isLimitAdTracking here to ensure initialization
111+
this.isLimitAdTracking = status ?? true; // Default to true on null
112+
113+
await this.setContext(id ?? undefined, status ?? true); // CHANGED: Handle nulls safely
112114
} catch (error) {
113115
this.handleError(error);
114116
}

0 commit comments

Comments
 (0)