Skip to content

Commit

Permalink
[android] send meta data to collect server
Browse files Browse the repository at this point in the history
  • Loading branch information
RyosukeCla committed Sep 12, 2024
1 parent 863304f commit e7be4a8
Showing 1 changed file with 32 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

0 comments on commit e7be4a8

Please sign in to comment.