Skip to content

Commit

Permalink
adding ability to update the user properties (identity) via the vecto…
Browse files Browse the repository at this point in the history
…r analytics
  • Loading branch information
ouchadam committed Jan 20, 2022
1 parent e6603dd commit 9754afe
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package im.vector.app.features.analytics

import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
import im.vector.app.features.analytics.plan.Identity
import kotlinx.coroutines.flow.Flow

interface VectorAnalytics {
Expand Down Expand Up @@ -70,4 +71,9 @@ interface VectorAnalytics {
* Track a displayed screen
*/
fun screen(screen: VectorAnalyticsScreen)

/**
* Update user specific properties
*/
fun updateUserProperties(identity: Identity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package im.vector.app.features.analytics.impl

import android.content.Context
import com.posthog.android.Options
import com.posthog.android.PostHog
import com.posthog.android.Properties
import im.vector.app.BuildConfig
Expand All @@ -25,6 +26,7 @@ import im.vector.app.features.analytics.VectorAnalytics
import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
import im.vector.app.features.analytics.log.analyticsTag
import im.vector.app.features.analytics.plan.Identity
import im.vector.app.features.analytics.store.AnalyticsStore
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.Flow
Expand All @@ -34,6 +36,9 @@ import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

private val REUSE_EXISTING_ID: String? = null
private val IGNORED_OPTIONS: Options? = null

@Singleton
class DefaultVectorAnalytics @Inject constructor(
private val context: Context,
Expand Down Expand Up @@ -170,6 +175,10 @@ class DefaultVectorAnalytics @Inject constructor(
?.screen(screen.getName(), screen.getProperties()?.toPostHogProperties())
}

override fun updateUserProperties(identity: Identity) {
posthog?.identify(REUSE_EXISTING_ID, identity.getProperties().toPostHogProperties(), IGNORED_OPTIONS)
}

private fun Map<String, Any>?.toPostHogProperties(): Properties? {
if (this == null) return null

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.app.features.analytics.plan

import im.vector.app.features.analytics.itf.VectorAnalyticsEvent

// GENERATED FILE, DO NOT EDIT. FOR MORE INFORMATION VISIT
// https://github.com/matrix-org/matrix-analytics-events/

/**
* The user properties to apply when identifying
*/
data class Identity(
/**
* The selected messaging use case during the onboarding flow.
*/
val ftueUseCaseSelection: FtueUseCaseSelection? = null,
) : VectorAnalyticsEvent {

enum class FtueUseCaseSelection {
/**
* The third option, Communities.
*/
CommunityMessaging,

/**
* The first option, Friends and family.
*/
PersonalMessaging,

/**
* The footer option to skip the question.
*/
Skip,

/**
* The second option, Teams.
*/
WorkMessaging,
}

override fun getName() = "Identity"

override fun getProperties(): Map<String, Any>? {
return mutableMapOf<String, Any>().apply {
ftueUseCaseSelection?.let { put("ftueUseCaseSelection", it.name) }
}.takeIf { it.isNotEmpty() }
}
}

0 comments on commit 9754afe

Please sign in to comment.