Skip to content

Commit

Permalink
feat: opt out of optional metadata fields (#243)
Browse files Browse the repository at this point in the history
* feat: opt out of optional metadata fields

* fix: lints

* fix: remove unused import

* fix: lint
  • Loading branch information
tore-statsig authored Jul 11, 2024
1 parent af66cb1 commit a679c90
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 22 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/statsig/androidsdk/StatsigClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,11 @@ class StatsigClient() : LifecycleEventListener {
if (!this::statsigNetwork.isInitialized) {
statsigNetwork = StatsigNetwork(application, sdkKey, errorBoundary, getSharedPrefs(), options)
}
statsigMetadata = StatsigMetadata()
statsigMetadata = if (options.optOutNonSdkMetadata) {
createCoreStatsigMetadata()
} else {
createStatsigMetadata()
}
errorBoundary.setMetadata(statsigMetadata)
errorBoundary.setDiagnostics(diagnostics)

Expand Down Expand Up @@ -1010,7 +1014,7 @@ class StatsigClient() : LifecycleEventListener {
private fun populateStatsigMetadata() {
statsigMetadata.overrideStableID(options.overrideStableID)
try {
if (application.packageManager != null) {
if (application.packageManager != null && !options.optOutNonSdkMetadata) {
val pInfo: PackageInfo =
application.packageManager.getPackageInfo(application.packageName, 0)
statsigMetadata.appVersion = pInfo.versionName
Expand Down
65 changes: 49 additions & 16 deletions src/main/java/com/statsig/androidsdk/StatsigMetadata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,62 @@ import java.util.*

internal data class StatsigMetadata(
@SerializedName("stableID") var stableID: String? = null,
@SerializedName("appIdentifier") var appIdentifier: String? = null,
@SerializedName("appVersion") var appVersion: String? = null,
@SerializedName("deviceModel") var deviceModel: String? = Build.MODEL,
@SerializedName("deviceOS") var deviceOS: String = "Android",
@SerializedName("locale") var locale: String? = Locale.getDefault().toString(),
@SerializedName("language")
var language: String? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// API 21+
Locale.getDefault().toLanguageTag()
} else {
Locale.getDefault().let { locale ->
"${locale.language}-${locale.country}"
}
},
@SerializedName("sdkType") var sdkType: String? = "android-client",
@SerializedName("sdkVersion") var sdkVersion: String? = BuildConfig.VERSION_NAME,
@SerializedName("sessionID") var sessionID: String = UUID.randomUUID().toString(),
@SerializedName("systemVersion") var systemVersion: String = Build.VERSION.SDK_INT.toString(),
@SerializedName("systemName") var systemName: String = "Android",
@SerializedName("appIdentifier") var appIdentifier: String? = null,
@SerializedName("appVersion") var appVersion: String? = null,
@SerializedName("deviceModel") var deviceModel: String? = null,
@SerializedName("deviceOS") var deviceOS: String? = null,
@SerializedName("locale") var locale: String? = null,
@SerializedName("language") var language: String? = null,
@SerializedName("systemVersion") var systemVersion: String? = null,
@SerializedName("systemName") var systemName: String? = null,
) {
internal fun overrideStableID(overrideStableID: String?) {
if (overrideStableID != null && overrideStableID != stableID) {
stableID = overrideStableID
}
}
}

internal fun createStatsigMetadata(): StatsigMetadata {
return StatsigMetadata(
stableID = null,
sdkType = "android-client",
sdkVersion = BuildConfig.VERSION_NAME,
sessionID = UUID.randomUUID().toString(),
appIdentifier = null,
appVersion = null,
deviceModel = Build.MODEL,
deviceOS = "Android",
locale = Locale.getDefault().toString(),
language = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// API 21+
Locale.getDefault().toLanguageTag()
} else {
Locale.getDefault().let { locale ->
"${locale.language}-${locale.country}"
}
},
systemVersion = Build.VERSION.SDK_INT.toString(),
systemName = "Android",
)
}

internal fun createCoreStatsigMetadata(): StatsigMetadata {
return StatsigMetadata(
stableID = null,
sdkType = "android-client",
sdkVersion = BuildConfig.VERSION_NAME,
sessionID = UUID.randomUUID().toString(),
appIdentifier = null,
appVersion = null,
deviceModel = null,
deviceOS = null,
locale = null,
language = null,
systemVersion = null,
systemName = null,
)
}
2 changes: 2 additions & 0 deletions src/main/java/com/statsig/androidsdk/StatsigOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class StatsigOptions(
var customCacheKey: ((String, StatsigUser) -> String) = { sdkKey, user -> "${user.getCacheKey()}:$sdkKey" },

var disableLogEventRetries: Boolean = false,

var optOutNonSdkMetadata: Boolean = false,
) {

private var environment: MutableMap<String, String>? = null
Expand Down
36 changes: 32 additions & 4 deletions src/test/java/com/statsig/androidsdk/StatsigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ class StatsigTest {
assertEquals(15, parsedLogs.events.count())
// first 2 are exposures pre initialize() completion
assertEquals("custom_stable_id", parsedLogs.statsigMetadata.stableID)
assertEquals("Android", parsedLogs.statsigMetadata.systemName)
assertEquals("Android", parsedLogs.statsigMetadata.deviceOS)
assert(parsedLogs.statsigMetadata.locale.toString().startsWith("en"))
assert(parsedLogs.statsigMetadata.language.toString().startsWith("en"))
assertEquals("Android", (parsedLogs.statsigMetadata as StatsigMetadata).systemName)
assertEquals("Android", (parsedLogs.statsigMetadata as StatsigMetadata).deviceOS)
assert((parsedLogs.statsigMetadata as StatsigMetadata).locale.toString().startsWith("en"))
assert((parsedLogs.statsigMetadata as StatsigMetadata).language.toString().startsWith("en"))

// validate diagnostics
assertEquals(parsedLogs.events[0].eventName, "statsig::diagnostics")
Expand Down Expand Up @@ -229,6 +229,34 @@ class StatsigTest {
assertEquals(parsedLogs.events[12].metadata!!["isManualExposure"], "true")
}

@Test
fun testInitializeWithOptOutNonSdkMetadata() {
val user = StatsigUser("123")
user.customIDs = mapOf("random_id" to "abcde")

// Initialize Statsig with optOutNonSdkMetadata set to true
TestUtil.startStatsigAndWait(
app,
user,
StatsigOptions(optOutNonSdkMetadata = true, overrideStableID = "custom_stable_id"),
network = network,
)
client = Statsig.client
assertTrue(client.checkGate("always_on"))

client.shutdown()

var parsedLogs = Gson().fromJson(flushedLogs, LogEventData::class.java)
assertNull(parsedLogs.statsigMetadata.appIdentifier)
assertNull(parsedLogs.statsigMetadata.appVersion)
assertNull(parsedLogs.statsigMetadata.deviceModel)
assertNull(parsedLogs.statsigMetadata.locale)
assertNull(parsedLogs.statsigMetadata.language)
assertNull(parsedLogs.statsigMetadata.deviceOS)
assertNull(parsedLogs.statsigMetadata.systemVersion)
assertNull(parsedLogs.statsigMetadata.systemName)
}

@Test
fun testReinitialize() = runBlocking {
var countdown = CountDownLatch(1)
Expand Down

0 comments on commit a679c90

Please sign in to comment.