From 8dd1ab6eaf2542913cf15fe645197e296116f6c8 Mon Sep 17 00:00:00 2001 From: Alyssa Yu Date: Tue, 31 May 2022 20:32:38 -0700 Subject: [PATCH 1/2] feat: enable to pass the event options for all track call (#36) --- .../main/java/com/amplitude/core/Amplitude.kt | 6 ++++- .../com/amplitude/core/AmplitudeTest.kt | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/amplitude/core/Amplitude.kt b/core/src/main/java/com/amplitude/core/Amplitude.kt index 2005c169..99514d2e 100644 --- a/core/src/main/java/com/amplitude/core/Amplitude.kt +++ b/core/src/main/java/com/amplitude/core/Amplitude.kt @@ -86,10 +86,14 @@ open class Amplitude internal constructor( * * @param event the event * @param callback the optional event callback + * @param options optional event options * @return the Amplitude instance */ @JvmOverloads - fun track(event: BaseEvent, callback: EventCallBack? = null): Amplitude { + fun track(event: BaseEvent, options: EventOptions? = null, callback: EventCallBack? = null): Amplitude { + options ?. let { + event.mergeEventOptions(it) + } callback ?. let { event.callback = it } diff --git a/core/src/test/kotlin/com/amplitude/core/AmplitudeTest.kt b/core/src/test/kotlin/com/amplitude/core/AmplitudeTest.kt index c5626c71..abeb38fe 100644 --- a/core/src/test/kotlin/com/amplitude/core/AmplitudeTest.kt +++ b/core/src/test/kotlin/com/amplitude/core/AmplitudeTest.kt @@ -64,6 +64,30 @@ internal class AmplitudeTest { assertEquals("test", it.plan?.source) } } + + @Test + fun `test track with event object and event options`() { + val mockPlugin = spyk(StubPlugin()) + amplitude.add(mockPlugin) + amplitude.setUserId("user_id") + amplitude.setDeviceId("device_id") + val eventOptions = EventOptions() + eventOptions.city = "SF" + val event = BaseEvent() + event.eventType = "test event" + event.region = "CA" + amplitude.track(event, eventOptions) + val track = slot() + verify { mockPlugin.track(capture(track)) } + track.captured.let { + assertEquals("user_id", it.userId) + assertEquals("device_id", it.deviceId) + assertEquals("${Constants.SDK_LIBRARY}/${Constants.SDK_VERSION}", it.library) + assertEquals("CA", it.region) + assertEquals("SF", it.city) + assertEquals("test", it.plan?.source) + } + } } @Nested From a4e0801e5d83eb04c0772ab5d37e6c15495786f6 Mon Sep 17 00:00:00 2001 From: qingzhuozhen <84748495+qingzhuozhen@users.noreply.github.com> Date: Wed, 1 Jun 2022 09:50:14 -0700 Subject: [PATCH 2/2] feat: add configuration java support (#35) --- .../com/amplitude/android/Configuration.kt | 32 +++++++++---------- .../com/amplitude/android/events/Export.kt | 2 +- .../java/com/amplitude/core/Configuration.kt | 28 ++++++++-------- .../android/sample/MainApplication.java | 12 +++++-- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/android/src/main/java/com/amplitude/android/Configuration.kt b/android/src/main/java/com/amplitude/android/Configuration.kt index db085750..e4188e2b 100644 --- a/android/src/main/java/com/amplitude/android/Configuration.kt +++ b/android/src/main/java/com/amplitude/android/Configuration.kt @@ -1,7 +1,6 @@ package com.amplitude.android import android.content.Context -import com.amplitude.android.events.Plan import com.amplitude.android.utilities.AndroidLoggerProvider import com.amplitude.android.utilities.AndroidStorageProvider import com.amplitude.core.Configuration @@ -9,24 +8,25 @@ import com.amplitude.core.EventCallBack import com.amplitude.core.LoggerProvider import com.amplitude.core.ServerZone import com.amplitude.core.StorageProvider +import com.amplitude.core.events.Plan class Configuration @JvmOverloads constructor( apiKey: String, val context: Context, - flushQueueSize: Int = FLUSH_QUEUE_SIZE, - flushIntervalMillis: Int = FLUSH_INTERVAL_MILLIS, - instanceName: String = DEFAULT_INSTANCE, - optOut: Boolean = false, - storageProvider: StorageProvider = AndroidStorageProvider(), - loggerProvider: LoggerProvider = AndroidLoggerProvider(), - minIdLength: Int? = null, - partnerId: String? = null, - callback: EventCallBack? = null, - flushMaxRetries: Int = FLUSH_MAX_RETRIES, - useBatch: Boolean = false, - serverZone: ServerZone = ServerZone.US, - serverUrl: String? = null, - plan: Plan? = null, + override var flushQueueSize: Int = FLUSH_QUEUE_SIZE, + override var flushIntervalMillis: Int = FLUSH_INTERVAL_MILLIS, + override var instanceName: String = DEFAULT_INSTANCE, + override var optOut: Boolean = false, + override val storageProvider: StorageProvider = AndroidStorageProvider(), + override val loggerProvider: LoggerProvider = AndroidLoggerProvider(), + override var minIdLength: Int? = null, + override var partnerId: String? = null, + override var callback: EventCallBack? = null, + override var flushMaxRetries: Int = FLUSH_MAX_RETRIES, + override var useBatch: Boolean = false, + override var serverZone: ServerZone = ServerZone.US, + override var serverUrl: String? = null, + override var plan: Plan? = null, val useAdvertisingIdForDeviceId: Boolean = false, val useAppSetIdForDeviceId: Boolean = false, val newDeviceIdPerInstall: Boolean = false, @@ -38,6 +38,6 @@ class Configuration @JvmOverloads constructor( val trackingSessionEvents: Boolean = true ) : Configuration(apiKey, flushQueueSize, flushIntervalMillis, instanceName, optOut, storageProvider, loggerProvider, minIdLength, partnerId, callback, flushMaxRetries, useBatch, serverZone, serverUrl, plan) { companion object { - const val MIN_TIME_BETWEEN_SESSIONS_MILLIS: Long = 5 * 60 * 1000 + const val MIN_TIME_BETWEEN_SESSIONS_MILLIS: Long = 300000 } } diff --git a/android/src/main/java/com/amplitude/android/events/Export.kt b/android/src/main/java/com/amplitude/android/events/Export.kt index 6d6d34e0..8e953592 100644 --- a/android/src/main/java/com/amplitude/android/events/Export.kt +++ b/android/src/main/java/com/amplitude/android/events/Export.kt @@ -16,7 +16,7 @@ open class EventOptions : EventOptions() open class Identify : Identify() open class Revenue : Revenue() open class RevenueEvent : RevenueEvent() -open class Plan( +open class Plan @JvmOverloads constructor( branch: String? = null, source: String? = null, version: String? = null, diff --git a/core/src/main/java/com/amplitude/core/Configuration.kt b/core/src/main/java/com/amplitude/core/Configuration.kt index 4356ad7a..1761dea9 100644 --- a/core/src/main/java/com/amplitude/core/Configuration.kt +++ b/core/src/main/java/com/amplitude/core/Configuration.kt @@ -9,20 +9,20 @@ typealias EventCallBack = (BaseEvent, status: Int, message: String) -> Unit open class Configuration @JvmOverloads constructor( val apiKey: String, - var flushQueueSize: Int = FLUSH_QUEUE_SIZE, - var flushIntervalMillis: Int = FLUSH_INTERVAL_MILLIS, - var instanceName: String = DEFAULT_INSTANCE, - var optOut: Boolean = false, - val storageProvider: StorageProvider = InMemoryStorageProvider(), - val loggerProvider: LoggerProvider = ConsoleLoggerProvider(), - var minIdLength: Int? = null, - var partnerId: String? = null, - val callback: EventCallBack? = null, - val flushMaxRetries: Int = FLUSH_MAX_RETRIES, - var useBatch: Boolean = false, - var serverZone: ServerZone = ServerZone.US, - var serverUrl: String? = null, - val plan: Plan? = null + open var flushQueueSize: Int = FLUSH_QUEUE_SIZE, + open var flushIntervalMillis: Int = FLUSH_INTERVAL_MILLIS, + open var instanceName: String = DEFAULT_INSTANCE, + open var optOut: Boolean = false, + open val storageProvider: StorageProvider = InMemoryStorageProvider(), + open val loggerProvider: LoggerProvider = ConsoleLoggerProvider(), + open var minIdLength: Int? = null, + open var partnerId: String? = null, + open var callback: EventCallBack? = null, + open var flushMaxRetries: Int = FLUSH_MAX_RETRIES, + open var useBatch: Boolean = false, + open var serverZone: ServerZone = ServerZone.US, + open var serverUrl: String? = null, + open var plan: Plan? = null ) { companion object { diff --git a/samples/java-android-app/src/main/java/com/amplitude/android/sample/MainApplication.java b/samples/java-android-app/src/main/java/com/amplitude/android/sample/MainApplication.java index 7aed5bdd..b42f0f5d 100644 --- a/samples/java-android-app/src/main/java/com/amplitude/android/sample/MainApplication.java +++ b/samples/java-android-app/src/main/java/com/amplitude/android/sample/MainApplication.java @@ -6,12 +6,15 @@ import androidx.annotation.Nullable; import com.amplitude.android.Amplitude; -import com.amplitude.android.Configuration; +import com.amplitude.android.AmplitudeKt; +import com.amplitude.android.events.Plan; import com.amplitude.core.events.BaseEvent; import com.amplitude.core.platform.Plugin; import java.util.HashMap; +import kotlin.Unit; + public class MainApplication extends Application { private static Amplitude amplitude; @@ -19,8 +22,13 @@ public class MainApplication extends Application { public void onCreate() { super.onCreate(); + Plan plan = new Plan("test branch", "test source", "test version", "test version id"); + // init instance - amplitude = new Amplitude(new Configuration(BuildConfig.AMPLITUDE_API_KEY, getApplicationContext())); + amplitude = AmplitudeKt.Amplitude(BuildConfig.AMPLITUDE_API_KEY, getApplicationContext(), configuration -> { + configuration.setPlan(plan); + return Unit.INSTANCE; + }); // add sample plugin amplitude.add(new SamplePlugin());