Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trikot analytics revamp #206

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
public class com/mirego/trikot/analytics/SimpleTrackableViewModelAction : com/mirego/trikot/viewmodels/properties/ViewModelAction {
public fun <init> (Lcom/mirego/trikot/analytics/AnalyticsEvent;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)V
public synthetic fun <init> (Lcom/mirego/trikot/analytics/AnalyticsEvent;Ljava/util/Map;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun execute ()V
public fun execute (Ljava/lang/Object;)V
}

public class com/mirego/trikot/analytics/TrackableViewModelAction : com/mirego/trikot/viewmodels/properties/ViewModelAction {
public fun <init> (Lcom/mirego/trikot/analytics/AnalyticsEvent;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)V
public fun <init> (Lcom/mirego/trikot/analytics/AnalyticsEvent;Lorg/reactivestreams/Publisher;Lkotlin/jvm/functions/Function1;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
public class com/mirego/trikot/analytics/SimpleTrackableViewModelAction : com/mirego/trikot/viewmodels/properties/ViewModelAction {
public fun <init> (Lcom/mirego/trikot/analytics/AnalyticsEvent;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)V
public synthetic fun <init> (Lcom/mirego/trikot/analytics/AnalyticsEvent;Ljava/util/Map;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun execute ()V
public fun execute (Ljava/lang/Object;)V
}

public class com/mirego/trikot/analytics/TrackableViewModelAction : com/mirego/trikot/viewmodels/properties/ViewModelAction {
public fun <init> (Lcom/mirego/trikot/analytics/AnalyticsEvent;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)V
public fun <init> (Lcom/mirego/trikot/analytics/AnalyticsEvent;Lorg/reactivestreams/Publisher;Lkotlin/jvm/functions/Function1;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mirego.trikot.analytics

import com.mirego.trikot.viewmodels.properties.ViewModelAction
import com.mirego.trikot.viewmodels.properties.ViewModelActionBlock

open class SimpleTrackableViewModelAction(private val event: AnalyticsEvent, private val properties: AnalyticsPropertiesType = mapOf(), actionBlock: ViewModelActionBlock) :
ViewModelAction(actionBlock) {

override fun execute() {
super.execute()
AnalyticsConfiguration.analyticsManager.trackEvent(event, properties)
}

override fun execute(actionContext: Any?) {
super.execute(actionContext)
AnalyticsConfiguration.analyticsManager.trackEvent(event, properties)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import com.mirego.trikot.viewmodels.properties.ViewModelAction
import com.mirego.trikot.viewmodels.properties.ViewModelActionBlock
import org.reactivestreams.Publisher

@Deprecated(
message = "Deprecated. Please use AnalyticsEvent",
replaceWith = ReplaceWith(
expression = "AnalyticsEvent",
imports = ["com.mirego.trikot.analytics.AnalyticsEvent"]
)
)
open class TrackableViewModelAction(private val event: AnalyticsEvent, private val properties: Publisher<AnalyticsPropertiesType> = mapOf<String, Any>().just(), actionBlock: ViewModelActionBlock) :
ViewModelAction(actionBlock) {
constructor(event: AnalyticsEvent, properties: AnalyticsPropertiesType, actionBlock: ViewModelActionBlock) : this(event, properties.just(), actionBlock)
Expand Down
6 changes: 6 additions & 0 deletions trikot-analytics/analytics/api/android/analytics.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public abstract interface class com/mirego/trikot/analytics/AnalyticsEvent {
public abstract interface class com/mirego/trikot/analytics/AnalyticsService {
public static final field Companion Lcom/mirego/trikot/analytics/AnalyticsService$Companion;
public abstract fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public abstract fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getName ()Ljava/lang/String;
public abstract fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public abstract fun incrementUserProperties (Ljava/util/Map;)V
Expand All @@ -26,6 +27,7 @@ public abstract interface class com/mirego/trikot/analytics/AnalyticsService {

public final class com/mirego/trikot/analytics/AnalyticsService$Companion : com/mirego/trikot/analytics/AnalyticsService {
public fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getName ()Ljava/lang/String;
public fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public fun incrementUserProperties (Ljava/util/Map;)V
Expand All @@ -40,15 +42,18 @@ public final class com/mirego/trikot/analytics/AnalyticsService$Companion : com/
}

public final class com/mirego/trikot/analytics/AnalyticsServiceKt {
public static final fun identifyUser (Lcom/mirego/trikot/analytics/AnalyticsService;Ljava/lang/String;Ljava/util/Map;)V
public static final fun identifyUser (Lcom/mirego/trikot/analytics/AnalyticsService;Ljava/lang/String;Lorg/reactivestreams/Publisher;)V
public static final fun logout (Lcom/mirego/trikot/analytics/AnalyticsService;Z)V
public static synthetic fun logout$default (Lcom/mirego/trikot/analytics/AnalyticsService;ZILjava/lang/Object;)V
public static final fun trackEvent (Lcom/mirego/trikot/analytics/AnalyticsService;Lcom/mirego/trikot/analytics/AnalyticsEvent;Ljava/util/Map;)V
public static final fun trackEvent (Lcom/mirego/trikot/analytics/AnalyticsService;Lcom/mirego/trikot/analytics/AnalyticsEvent;Lorg/reactivestreams/Publisher;)V
}

public final class com/mirego/trikot/analytics/AnalyticsServiceLogger : com/mirego/trikot/analytics/AnalyticsService {
public fun <init> (Lcom/mirego/trikot/analytics/AnalyticsService;)V
public fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getName ()Ljava/lang/String;
public fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public fun incrementUserProperties (Ljava/util/Map;)V
Expand All @@ -65,6 +70,7 @@ public final class com/mirego/trikot/analytics/AnalyticsServiceLogger : com/mire
public final class com/mirego/trikot/analytics/EmptyAnalyticsService : com/mirego/trikot/analytics/AnalyticsService {
public fun <init> ()V
public fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getName ()Ljava/lang/String;
public fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public fun incrementUserProperties (Ljava/util/Map;)V
Expand Down
6 changes: 6 additions & 0 deletions trikot-analytics/analytics/api/jvm/analytics.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public abstract interface class com/mirego/trikot/analytics/AnalyticsEvent {
public abstract interface class com/mirego/trikot/analytics/AnalyticsService {
public static final field Companion Lcom/mirego/trikot/analytics/AnalyticsService$Companion;
public abstract fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public abstract fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getName ()Ljava/lang/String;
public abstract fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public abstract fun incrementUserProperties (Ljava/util/Map;)V
Expand All @@ -26,6 +27,7 @@ public abstract interface class com/mirego/trikot/analytics/AnalyticsService {

public final class com/mirego/trikot/analytics/AnalyticsService$Companion : com/mirego/trikot/analytics/AnalyticsService {
public fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getName ()Ljava/lang/String;
public fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public fun incrementUserProperties (Ljava/util/Map;)V
Expand All @@ -40,15 +42,18 @@ public final class com/mirego/trikot/analytics/AnalyticsService$Companion : com/
}

public final class com/mirego/trikot/analytics/AnalyticsServiceKt {
public static final fun identifyUser (Lcom/mirego/trikot/analytics/AnalyticsService;Ljava/lang/String;Ljava/util/Map;)V
public static final fun identifyUser (Lcom/mirego/trikot/analytics/AnalyticsService;Ljava/lang/String;Lorg/reactivestreams/Publisher;)V
public static final fun logout (Lcom/mirego/trikot/analytics/AnalyticsService;Z)V
public static synthetic fun logout$default (Lcom/mirego/trikot/analytics/AnalyticsService;ZILjava/lang/Object;)V
public static final fun trackEvent (Lcom/mirego/trikot/analytics/AnalyticsService;Lcom/mirego/trikot/analytics/AnalyticsEvent;Ljava/util/Map;)V
public static final fun trackEvent (Lcom/mirego/trikot/analytics/AnalyticsService;Lcom/mirego/trikot/analytics/AnalyticsEvent;Lorg/reactivestreams/Publisher;)V
}

public final class com/mirego/trikot/analytics/AnalyticsServiceLogger : com/mirego/trikot/analytics/AnalyticsService {
public fun <init> (Lcom/mirego/trikot/analytics/AnalyticsService;)V
public fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getName ()Ljava/lang/String;
public fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public fun incrementUserProperties (Ljava/util/Map;)V
Expand All @@ -65,6 +70,7 @@ public final class com/mirego/trikot/analytics/AnalyticsServiceLogger : com/mire
public final class com/mirego/trikot/analytics/EmptyAnalyticsService : com/mirego/trikot/analytics/AnalyticsService {
public fun <init> ()V
public fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getName ()Ljava/lang/String;
public fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public fun incrementUserProperties (Ljava/util/Map;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,20 @@ interface AnalyticsService {
/*
The distinctAppId associated with the current device
*/
@Deprecated(
message = "Deprecated. Please use distinctDeviceId()",
replaceWith = ReplaceWith(
expression = "distinctDeviceId()",
imports = ["com.mirego.trikot.analytics.distinctDeviceId"]
)
)
fun distinctAppId(): Promise<String>

/*
The distinctDeviceId associated with the current device
*/
suspend fun distinctDeviceId(): String

/*
userId: Id of the logged in user
properties: Properties associated with the user
Expand Down Expand Up @@ -76,8 +88,17 @@ interface AnalyticsService {
currentAnalyticsService().isEnabled = value
}

@Deprecated(
message = "Deprecated. Please use distinctDeviceId()",
replaceWith = ReplaceWith(
expression = "distinctDeviceId()",
imports = ["com.mirego.trikot.analytics.distinctDeviceId"]
)
)
override fun distinctAppId() = currentAnalyticsService().distinctAppId()

override suspend fun distinctDeviceId() = currentAnalyticsService().distinctDeviceId()

override fun identifyUser(userId: String, properties: AnalyticsPropertiesType) {
currentAnalyticsService().identifyUser(userId, properties)
}
Expand Down Expand Up @@ -116,6 +137,13 @@ interface AnalyticsService {
event: Event to track, will be store using toString() method
properties: Publisher of property to store with the event. Publisher MUST return a value.
*/
@Deprecated(
message = "Deprecated. Please use the one without a Publisher",
replaceWith = ReplaceWith(
expression = "trackEvent(event, propertiesMap)",
imports = ["com.mirego.trikot.analytics.trackEvent"]
)
)
fun AnalyticsService.trackEvent(
event: AnalyticsEvent,
properties: Publisher<AnalyticsPropertiesType>
Expand All @@ -125,16 +153,42 @@ fun AnalyticsService.trackEvent(
}
}

/*
event: Event to track, will be store using toString() method
properties: Property to store with the event.
*/
fun AnalyticsService.trackEvent(
event: AnalyticsEvent,
propertiesMap: AnalyticsPropertiesType
) {
trackEvent(event, propertiesMap)
}

/*
userId: Id of the logged in user
properties: Publisher of property to store with the event. Publisher MUST return a value.
*/
@Deprecated(
message = "Deprecated. Please use the one without a Publisher",
replaceWith = ReplaceWith(
expression = "trackEvent(event, propertiesMap)",
imports = ["com.mirego.trikot.analytics.identifyUser"]
)
)
fun AnalyticsService.identifyUser(userId: String, properties: Publisher<AnalyticsPropertiesType>) {
properties.first().subscribe(CancellableManager()) {
identifyUser(userId, it)
}
}

/*
userId: Id of the logged in user
properties: Property to store with the event.
*/
fun AnalyticsService.identifyUser(userId: String, propertiesMap: AnalyticsPropertiesType) {
identifyUser(userId, propertiesMap)
}

/*
UnIdentify the current logged in user
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ class AnalyticsServiceLogger(private val analyticsService: AnalyticsService) : A

private val superProperties = AtomicReference<AnalyticsPropertiesType>(mapOf())

@Deprecated(
message = "Deprecated. Please use distinctDeviceId()",
replaceWith = ReplaceWith(
expression = "distinctDeviceId()",
imports = ["com.mirego.trikot.analytics.distinctDeviceId"]
)
)
override fun distinctAppId() = analyticsService.distinctAppId()

override suspend fun distinctDeviceId() = analyticsService.distinctDeviceId()

override fun identifyUser(userId: String, properties: AnalyticsPropertiesType) {
println(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ class EmptyAnalyticsService : AnalyticsService {

override var isEnabled = false

@Deprecated(
message = "Deprecated. Please use distinctDeviceId()",
replaceWith = ReplaceWith(
expression = "distinctDeviceId()",
imports = ["com.mirego.trikot.analytics.distinctDeviceId"]
)
)
override fun distinctAppId() = Promise.resolve("ANALYTICS_SERVICE_NOT_CONFIGURED_DISTINCT_ID")

override suspend fun distinctDeviceId() = "ANALYTICS_SERVICE_NOT_CONFIGURED_DISTINCT_ID"

override fun identifyUser(userId: String, properties: AnalyticsPropertiesType) {
}

Expand Down
1 change: 1 addition & 0 deletions trikot-analytics/firebase-ktx/api/firebase-ktx.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ public final class com/mirego/trikot/analytics/FirebaseAnalyticsService : com/mi
public fun <init> (Landroid/content/Context;Z)V
public synthetic fun <init> (Landroid/content/Context;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getName ()Ljava/lang/String;
public fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public fun incrementUserProperties (Ljava/util/Map;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.Context
import android.os.Bundle
import com.google.firebase.analytics.FirebaseAnalytics
import com.mirego.trikot.streams.reactive.promise.Promise
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

class FirebaseAnalyticsService(context: Context, analyticsEnabled: Boolean = true) :
AnalyticsService {
Expand All @@ -23,6 +25,13 @@ class FirebaseAnalyticsService(context: Context, analyticsEnabled: Boolean = tru

private var superProperties = emptyMap<String, Any>()

@Deprecated(
message = "Deprecated. Please use distinctDeviceId()",
replaceWith = ReplaceWith(
expression = "distinctDeviceId()",
imports = ["com.mirego.trikot.analytics.distinctDeviceId"]
)
)
override fun distinctAppId() = Promise.create<String> { resolve, reject ->
firebaseAnalytics.appInstanceId
.addOnCompleteListener { task ->
Expand All @@ -40,6 +49,23 @@ class FirebaseAnalyticsService(context: Context, analyticsEnabled: Boolean = tru
}
}

override suspend fun distinctDeviceId() = suspendCancellableCoroutine { continuation ->
firebaseAnalytics.appInstanceId
.addOnCompleteListener { task ->
val result = task.result
if (task.isSuccessful && result != null) {
continuation.resume(result)
} else if (!task.isSuccessful) {
continuation.cancel(Throwable(task.exception))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typed exceptions might help catching specific errors here.

also, why not resumeWithException

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for .resumeWithException 🙂

} else {
continuation.cancel(Throwable("Empty result"))
}
}
.addOnCanceledListener {
continuation.cancel(Throwable("Canceled"))
}
}

override fun identifyUser(userId: String, properties: AnalyticsPropertiesType) {
firebaseAnalytics.setUserId(userId)
properties.forEach {
Expand Down
1 change: 1 addition & 0 deletions trikot-analytics/mixpanel-ktx/api/mixpanel-ktx.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ public final class com/mirego/trikot/analytics/MixpanelAnalyticsService : com/mi
public fun <init> (Landroid/content/Context;Ljava/lang/String;Z)V
public synthetic fun <init> (Landroid/content/Context;Ljava/lang/String;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun distinctAppId ()Lcom/mirego/trikot/streams/reactive/promise/Promise;
public fun distinctDeviceId (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getName ()Ljava/lang/String;
public fun identifyUser (Ljava/lang/String;Ljava/util/Map;)V
public fun incrementUserProperties (Ljava/util/Map;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ class MixpanelAnalyticsService(

override val name: String = "MixpanelAnalytics"

@Deprecated(
message = "Deprecated. Please use distinctDeviceId()",
replaceWith = ReplaceWith(
expression = "distinctDeviceId()",
imports = ["com.mirego.trikot.analytics.distinctDeviceId"]
)
)
override fun distinctAppId() = Promise.resolve(mixpanelAnalytics.distinctId)

override suspend fun distinctDeviceId(): String = mixpanelAnalytics.distinctId

override fun identifyUser(userId: String, properties: AnalyticsPropertiesType) {
mixpanelAnalytics.identify(userId)
mixpanelAnalytics.people.identify(userId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class FirebaseAnalyticsService: AnalyticsService {
PromiseCompanion().resolve(value: Analytics.appInstanceID) as! Promise<NSString>
}

public func distinctDeviceId() -> NSString {
Analytics.appInstanceID() as! NSString
}

public func identifyUser(userId: String, properties: [String: Any]) {
Analytics.setUserID(userId)
properties.forEach { Analytics.setUserProperty(anyToString($0.value), forName: $0.key) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class MixpanelAnalyticsService: AnalyticsService {
PromiseCompanion().resolve(value: Mixpanel.mainInstance().distinctId) as! Promise<NSString>
}

public func distinctDeviceId() -> NSString {
Mixpanel.mainInstance().distinctId as NSString
}

public func identifyUser(userId: String, properties: [String: Any]) {
Mixpanel.mainInstance().identify(distinctId: userId)
Mixpanel.mainInstance().people.set(properties: properties.asMixpanelProperties)
Expand Down