diff --git a/android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt b/android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt index 32fd9f4b..24470443 100644 --- a/android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt +++ b/android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt @@ -21,14 +21,15 @@ class AndroidStorage( private val store: Store, writeKey: String, private val ioDispatcher: CoroutineDispatcher, - directory: String? = null + directory: String? = null, + subject: String? = null ) : Subscriber, Storage { private val sharedPreferences: SharedPreferences = context.getSharedPreferences("analytics-android-$writeKey", Context.MODE_PRIVATE) override val storageDirectory: File = context.getDir(directory ?: "segment-disk-queue", Context.MODE_PRIVATE) internal val eventsFile = - EventsFileManager(storageDirectory, writeKey, AndroidKVS(sharedPreferences)) + EventsFileManager(storageDirectory, writeKey, AndroidKVS(sharedPreferences), subject) override suspend fun subscribeToStore() { store.subscribe( diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt index f70d87b4..5d6b4b7a 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt @@ -34,7 +34,8 @@ import java.io.FileOutputStream class EventsFileManager( val directory: File, private val writeKey: String, - private val kvs: KVS + private val kvs: KVS, + subject: String? = null ) { init { @@ -42,7 +43,7 @@ class EventsFileManager( registerShutdownHook() } - private val fileIndexKey = "segment.events.file.index.$writeKey" + private val fileIndexKey = if(subject == null) "segment.events.file.index.$writeKey" else "segment.events.file.index.$writeKey.$subject" private var os: FileOutputStream? = null diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt index dc093ca0..a1be4287 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt @@ -20,14 +20,15 @@ class StorageImpl( private val store: Store, writeKey: String, private val ioDispatcher: CoroutineDispatcher, - directory: String? = null + directory: String? = null, + subject: String? = null ) : Subscriber, Storage { override val storageDirectory = File(directory ?: "/tmp/analytics-kotlin/$writeKey") private val storageDirectoryEvents = File(storageDirectory, "events") internal val propertiesFile = PropertiesFile(storageDirectory, writeKey) - internal val eventsFile = EventsFileManager(storageDirectoryEvents, writeKey, propertiesFile) + internal val eventsFile = EventsFileManager(storageDirectoryEvents, writeKey, propertiesFile, subject) init { propertiesFile.load() diff --git a/core/src/test/kotlin/com/segment/analytics/kotlin/core/utilities/EventsFileManagerTest.kt b/core/src/test/kotlin/com/segment/analytics/kotlin/core/utilities/EventsFileManagerTest.kt index 7b5baec5..5d3b7c50 100644 --- a/core/src/test/kotlin/com/segment/analytics/kotlin/core/utilities/EventsFileManagerTest.kt +++ b/core/src/test/kotlin/com/segment/analytics/kotlin/core/utilities/EventsFileManagerTest.kt @@ -57,6 +57,26 @@ internal class EventsFileManagerTest{ assertEquals(expectedContents, actualContents) } + @Test + fun `check if filename includes subject`() = runTest { + val file = EventsFileManager(directory, "123", kvStore, "test") + val trackEvent = TrackEvent( + event = "clicked", + properties = buildJsonObject { put("behaviour", "good") }) + .apply { + messageId = "qwerty-1234" + anonymousId = "anonId" + integrations = emptyJsonObject + context = emptyJsonObject + timestamp = epochTimestamp + } + val eventString = EncodeDefaultsJson.encodeToString(trackEvent) + file.storeEvent(eventString) + file.rollover() + + assertEquals(1, kvStore.getInt("segment.events.file.index.123.test", -1)) + } + @Test fun `storeEvent stores in existing file if available`() = runTest { val file = EventsFileManager(directory, "123", kvStore) diff --git a/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/MainApplication.kt b/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/MainApplication.kt index cf9be8a0..031aed56 100644 --- a/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/MainApplication.kt +++ b/samples/kotlin-android-app/src/main/java/com/segment/analytics/next/MainApplication.kt @@ -13,6 +13,7 @@ import com.segment.analytics.kotlin.core.platform.Plugin import com.segment.analytics.kotlin.core.platform.policies.CountBasedFlushPolicy import com.segment.analytics.kotlin.core.platform.policies.FrequencyFlushPolicy import com.segment.analytics.kotlin.core.utilities.* +import java.net.HttpURLConnection class MainApplication : Application() { companion object { @@ -22,7 +23,7 @@ class MainApplication : Application() { override fun onCreate() { super.onCreate() - analytics = Analytics(BuildConfig.SEGMENT_WRITE_KEY, applicationContext) { + analytics = Analytics("tteOFND0bb5ugJfALOJWpF0wu1tcxYgr", applicationContext) { this.collectDeviceId = true this.trackApplicationLifecycleEvents = true this.trackDeepLinks = true @@ -32,6 +33,15 @@ class MainApplication : Application() { UnmeteredFlushPolicy(applicationContext) // Flush if network is not metered ) this.flushPolicies = listOf(UnmeteredFlushPolicy(applicationContext)) + this.requestFactory = object : RequestFactory() { + override fun upload(apiHost: String): HttpURLConnection { + val connection: HttpURLConnection = openConnection("https://$apiHost/b") + connection.setRequestProperty("Content-Type", "text/plain") + connection.doOutput = true + connection.setChunkedStreamingMode(0) + return connection + } + } } analytics.add(AndroidRecordScreenPlugin()) analytics.add(object : Plugin {