Skip to content

Commit

Permalink
Merge branch 'main' of github.com:amplitude/Amplitude-Kotlin into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhao900914 committed Jun 1, 2022
2 parents 522ecc5 + a4e0801 commit 1fda1c7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 34 deletions.
32 changes: 16 additions & 16 deletions android/src/main/java/com/amplitude/android/Configuration.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
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
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,
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/com/amplitude/core/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 14 additions & 14 deletions core/src/main/java/com/amplitude/core/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions core/src/test/kotlin/com/amplitude/core/AmplitudeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseEvent>()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
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;

@Override
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());
Expand Down

0 comments on commit 1fda1c7

Please sign in to comment.