From e7be4a853bd145d0cc67b7e30d3b52cff34b12bb Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 12 Sep 2024 17:21:22 +0900 Subject: [PATCH] [android] send meta data to collect server --- .../java/com/nativebrik/sdk/data/track.kt | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/android/nativebrik/src/main/java/com/nativebrik/sdk/data/track.kt b/android/nativebrik/src/main/java/com/nativebrik/sdk/data/track.kt index ca61ad7..34beb20 100644 --- a/android/nativebrik/src/main/java/com/nativebrik/sdk/data/track.kt +++ b/android/nativebrik/src/main/java/com/nativebrik/sdk/data/track.kt @@ -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 @@ -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, + val meta: TrackEventMeta, val timestamp: ZonedDateTime = getCurrentDate(), ) { fun encode(): JsonObject { @@ -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(), )) } } @@ -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()