Skip to content

Commit

Permalink
fix: do not fetch advertising Id if adid tracking is disabled (#424)
Browse files Browse the repository at this point in the history
* fix: do not fetch advertising Id if adid tracking is disabled

* fix: ios compilation
falconandy authored Jun 13, 2023

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 5cfe645 commit 52f7b08
Showing 5 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -13,25 +13,31 @@ const val MODULE_NAME = "AmplitudeReactNative"
@ReactModule(name = MODULE_NAME)
class AmplitudeReactNativeModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

private val androidContextProvider = AndroidContextProvider(reactContext.applicationContext, false)
private var androidContextProvider: AndroidContextProvider? = null

override fun getName(): String {
return MODULE_NAME
}

@ReactMethod
private fun getApplicationContext(promise: Promise) {
private fun getApplicationContext(shouldTrackAdid: Boolean, promise: Promise) {
if (androidContextProvider == null) {
androidContextProvider = AndroidContextProvider(reactContext.applicationContext, false, shouldTrackAdid)
}

promise.resolve(WritableNativeMap().apply {
putString("version", androidContextProvider.versionName)
putString("platform", androidContextProvider.platform)
putString("language", androidContextProvider.language)
putString("osName", androidContextProvider.osName)
putString("osVersion", androidContextProvider.osVersion)
putString("deviceBrand", androidContextProvider.brand)
putString("deviceManufacturer", androidContextProvider.manufacturer)
putString("deviceModel", androidContextProvider.model)
putString("carrier", androidContextProvider.carrier)
putString("adid", androidContextProvider.advertisingId)
putString("version", androidContextProvider!!.versionName)
putString("platform", androidContextProvider!!.platform)
putString("language", androidContextProvider!!.language)
putString("osName", androidContextProvider!!.osName)
putString("osVersion", androidContextProvider!!.osVersion)
putString("deviceBrand", androidContextProvider!!.brand)
putString("deviceManufacturer", androidContextProvider!!.manufacturer)
putString("deviceModel", androidContextProvider!!.model)
putString("carrier", androidContextProvider!!.carrier)
if (androidContextProvider!!.advertisingId != null) {
putString("adid", androidContextProvider!!.advertisingId)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -20,8 +20,9 @@ import java.util.Locale
import java.util.UUID
import kotlin.collections.ArrayList

class AndroidContextProvider(private val context: Context, locationListening: Boolean) {
class AndroidContextProvider(private val context: Context, locationListening: Boolean, shouldTrackAdid: Boolean) {
var isLocationListening = true
var shouldTrackAdid = true
private var cachedInfo: CachedInfo? = null
private get() {
if (field == null) {
@@ -34,7 +35,7 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
* Internal class serves as a cache
*/
inner class CachedInfo {
var advertisingId: String
var advertisingId: String?
val country: String?
val versionName: String?
val osName: String
@@ -201,7 +202,11 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
return locale.language
}

private fun fetchAdvertisingId(): String {
private fun fetchAdvertisingId(): String? {
if (!shouldTrackAdid) {
return null
}

// This should not be called on the main thread.
return if ("Amazon" == fetchManufacturer()) {
fetchAndCacheAmazonAdvertisingId
@@ -237,14 +242,14 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
return appSetId
}

private val fetchAndCacheAmazonAdvertisingId: String
private val fetchAndCacheAmazonAdvertisingId: String?
private get() {
val cr = context.contentResolver
limitAdTrackingEnabled = Secure.getInt(cr, SETTING_LIMIT_AD_TRACKING, 0) == 1
advertisingId = Secure.getString(cr, SETTING_ADVERTISING_ID)
return advertisingId
}
private val fetchAndCacheGoogleAdvertisingId: String
private val fetchAndCacheGoogleAdvertisingId: String?
private get() {
try {
val AdvertisingIdClient = Class
@@ -340,7 +345,7 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
get() = cachedInfo!!.country
val language: String
get() = cachedInfo!!.language
val advertisingId: String
val advertisingId: String?
get() = cachedInfo!!.advertisingId
val appSetId: String
get() = cachedInfo!!.appSetId // other causes// failed to get providers list
@@ -416,5 +421,6 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo

init {
isLocationListening = locationListening
this.shouldTrackAdid = shouldTrackAdid
}
}
2 changes: 1 addition & 1 deletion packages/analytics-react-native/ios/AmplitudeReactNative.m
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@

@interface RCT_EXTERN_MODULE(AmplitudeReactNative, NSObject)

RCT_EXTERN_METHOD(getApplicationContext: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(getApplicationContext: (BOOL)shouldTrackAdid resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

@end
Original file line number Diff line number Diff line change
@@ -13,7 +13,8 @@ class ReactNative: NSObject {

@objc
func getApplicationContext(
_ resolve: RCTPromiseResolveBlock,
_ shouldTrackAdid: Bool,
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock
) -> Void {
let applicationContext: [String: String?] = [
4 changes: 2 additions & 2 deletions packages/analytics-react-native/src/plugins/context.ts
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ type NativeContext = {
};

export interface AmplitudeReactNative {
getApplicationContext(): Promise<NativeContext>;
getApplicationContext(shouldTrackAdid: boolean): Promise<NativeContext>;
}

export class Context implements BeforePlugin {
@@ -55,7 +55,7 @@ export class Context implements BeforePlugin {

async execute(context: Event): Promise<Event> {
const time = new Date().getTime();
const nativeContext = await this.nativeModule?.getApplicationContext();
const nativeContext = await this.nativeModule?.getApplicationContext(this.config.trackingOptions.adid ?? false);
const appVersion = nativeContext?.version || this.config.appVersion;
const platform = nativeContext?.platform || BROWSER_PLATFORM;
const osName = nativeContext?.osName || this.uaResult.browser.name;

0 comments on commit 52f7b08

Please sign in to comment.