Skip to content

Commit

Permalink
Merge pull request #50 from plaidev/flutter
Browse files Browse the repository at this point in the history
[android] support appid condition for experiment targetting and send meta data to collect server
  • Loading branch information
RyosukeCla committed Sep 12, 2024
2 parents 6db79dd + e7be4a8 commit 739df00
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
34 changes: 32 additions & 2 deletions android/nativebrik/src/main/java/com/nativebrik/sdk/data/track.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.nativebrik.sdk.data

import android.os.Build
import com.nativebrik.sdk.Config
import com.nativebrik.sdk.VERSION
import com.nativebrik.sdk.data.user.NativebrikUser
import com.nativebrik.sdk.data.user.formatISO8601
import com.nativebrik.sdk.data.user.getCurrentDate
Expand Down Expand Up @@ -97,10 +99,29 @@ internal sealed class TrackEvent {
}
}

internal data class TrackEventMeta(
val appId: String?,
val appVersion: String?,
val osName: String?,
val osVersion: String?,
val sdkVersion: String?
) {
fun encode(): JsonObject {
return JsonObject(mapOf(
"appId" to JsonPrimitive(this.appId),
"appVersion" to JsonPrimitive(this.appVersion),
"osName" to JsonPrimitive(this.osName),
"osVersion" to JsonPrimitive(this.osVersion),
"sdkVersion" to JsonPrimitive(this.sdkVersion),
))
}
}

internal data class TrackRequest(
val projectId: String,
val userId: String,
val events: List<TrackEvent>,
val meta: TrackEventMeta,
val timestamp: ZonedDateTime = getCurrentDate(),
) {
fun encode(): JsonObject {
Expand All @@ -109,7 +130,8 @@ internal data class TrackRequest(
"projectId" to JsonPrimitive(projectId),
"userId" to JsonPrimitive(userId),
"timestamp" to JsonPrimitive(formatISO8601(timestamp)),
"events" to JsonArray(events)
"events" to JsonArray(events),
"meta" to meta.encode(),
))
}
}
Expand Down Expand Up @@ -175,10 +197,18 @@ internal class TrackRepositoryImpl: TrackRepository {
val tempBuffer = this.buffer
if (tempBuffer.isEmpty()) return
this.buffer = mutableListOf()
val meta = TrackEventMeta(
appId = this.user.packageName,
appVersion = this.user.appVersion,
osVersion = Build.VERSION.SDK_INT.toString(),
osName = "Android",
sdkVersion = VERSION
)
val request = TrackRequest(
projectId = config.projectId,
userId = user.id,
events = tempBuffer
events = tempBuffer,
meta = meta,
)
val body = Json.encodeToString(request.encode())
this.timer?.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class NativebrikUser {
private var properties: MutableMap<String, String> = mutableMapOf()
internal var preferences: SharedPreferences? = null
private var lastBootTime: ZonedDateTime = getCurrentDate()
internal var packageName: String? = null
internal var appVersion: String? = null

val id: String
get() {
Expand Down Expand Up @@ -99,8 +101,11 @@ class NativebrikUser {

try {
val packageName = context.packageName
this.packageName = packageName
this.properties[BuiltinUserProperty.appId.toString()] = packageName
val appVersion = context.packageManager.getPackageInfo(packageName, 0).versionName
this.properties[BuiltinUserProperty.appVersion.toString()] = appVersion
this.appVersion = appVersion
} catch (_: Exception) {
this.properties[BuiltinUserProperty.appVersion.toString()] = "0.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ internal enum class BuiltinUserProperty {
sdkVersion,
osVersion,
osName,
appId,
appVersion,
cfBundleVersion,
localYear,
Expand Down Expand Up @@ -374,6 +375,7 @@ internal enum class BuiltinUserProperty {
"sdkVersion" -> sdkVersion
"osVersion" -> osVersion
"osName" -> osName
"appId" -> appId
"appVersion" -> appVersion
"cfBundleVersion" -> cfBundleVersion
"localYear" -> localYear
Expand Down

0 comments on commit 739df00

Please sign in to comment.