Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
package com.analyticsreactnativepluginadvertisingid

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.ReactApplication
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.facebook.react.bridge.*
import com.facebook.react.module.annotations.ReactModule
import android.util.Log
import java.io.IOException;


@ReactModule(name="AnalyticsReactNativePluginAdvertisingId")
class AnalyticsReactNativePluginAdvertisingIdModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
override fun getName(): String {
return "AnalyticsReactNativePluginAdvertisingId"
}

@ReactMethod
fun getAdvertisingId(promise: Promise) {
getAdvertisingIdInfo(promise) { advertisingInfo ->
val id = advertisingInfo.id
promise.resolve(id.toString())
}
}
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import kotlinx.coroutines.*

@ReactMethod
fun getIsLimitAdTrackingEnableStatus(promise: Promise) {
getAdvertisingIdInfo(promise) { advertisingInfo ->
val isLimitAdTrackingEnabled = advertisingInfo.isLimitAdTrackingEnabled
promise.resolve(isLimitAdTrackingEnabled)
}
}
@ReactModule(name = "AnalyticsReactNativePluginAdvertisingId")

private fun getAdvertisingIdInfo(promise: Promise, callback: (AdvertisingIdClient.Info) -> Unit) {
if (currentActivity?.application == null) {
promise.resolve(null)
return
}
class AnalyticsReactNativePluginAdvertisingIdModule(
private val reactContext: ReactApplicationContext
) : ReactContextBaseJavaModule(reactContext) {

val reactContext = (currentActivity?.application as ReactApplication)
?.reactNativeHost
?.reactInstanceManager
?.currentReactContext
override fun getName(): String {
return "AnalyticsReactNativePluginAdvertisingId"
}

if (reactContext == null) {
promise.resolve(null)
return
}
/**
* Return only the advertising ID string
*/
@ReactMethod
fun getAdvertisingId(promise: Promise) {
Thread {
try {
val info = AdvertisingIdClient.getAdvertisingIdInfo(reactContext)
promise.resolve(info.id ?: "")
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}.start()
}
/**
* Return only the "is limit ad tracking enabled" status
*/
@ReactMethod
fun getIsLimitAdTrackingEnableStatus(promise: Promise) {
Thread {
try {
val info = AdvertisingIdClient.getAdvertisingIdInfo(reactContext)
promise.resolve(info.isLimitAdTrackingEnabled ?: false)
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}.start()
}

try {
val advertisingInfo = AdvertisingIdClient.getAdvertisingIdInfo(reactContext)
callback(advertisingInfo)
} catch (e: GooglePlayServicesNotAvailableException) {
Log.d(name, e.toString())
promise.resolve(null)
} catch (e: IOException) {
Log.d(name, e.toString())
promise.resolve(null)
}
/**
* Return both values together
*/
@ReactMethod
fun getAdvertisingInfo(promise: Promise) {
Thread {
try {
val info = AdvertisingIdClient.getAdvertisingIdInfo(reactContext)
val result = Arguments.createMap()
result.putString("advertisingId", info.id ?: "")
result.putBoolean("isLimitAdTrackingEnabled", info.isLimitAdTrackingEnabled ?: false)
promise.resolve(result)
} catch (e: Exception) {
promise.reject("ERROR", e)
}
}.start()
}
}
20 changes: 11 additions & 9 deletions packages/plugins/plugin-advertising-id/src/AdvertisingIdPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,26 @@ export class AdvertisingIdPlugin extends Plugin {
const limitAdTrackingStatusPromise = this.fetchLimitAdTrackingStatus();

try {
// Await both promises to resolve simultaneously
const [id, status] = await Promise.all([
// eslint-disable-next-line prefer-const
let [id, status] = await Promise.all([
advertisingIdPromise,
limitAdTrackingStatusPromise,
]);
// Handle null status (e.g., native failure) by assuming enabled if id null
if (id === null && status === null) {
status = true; // Assume enabled on failure
}

// Handle advertisingID
if (id === null) {
void this.analytics?.track(
'LimitAdTrackingEnabled (Google Play Services) is enabled'
);
this.advertisingId = undefined; // Set to undefined if id is null
this.advertisingId = null; // CHANGED: Use null for unavailable, not undefined
} else {
this.advertisingId = id;
}

// Set context after both values are available
await this.setContext(id as string, status);
// NEW: Set isLimitAdTracking here to ensure initialization
this.isLimitAdTracking = status ?? true; // Default to true on null

await this.setContext(id ?? undefined, status ?? true); // CHANGED: Handle nulls safely
} catch (error) {
this.handleError(error);
}
Expand Down
Loading