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/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/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/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 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());