Skip to content

Commit 443db6c

Browse files
committed
Bump posthog version to 3.2.0
1 parent 305372c commit 443db6c

File tree

5 files changed

+43
-49
lines changed

5 files changed

+43
-49
lines changed

dependencies_groups.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ ext.groups = [
120120
'com.parse.bolts',
121121
'com.pinterest',
122122
'com.pinterest.ktlint',
123-
'com.posthog.android',
123+
'com.posthog',
124124
'com.squareup',
125125
'com.squareup.curtains',
126126
'com.squareup.duktape',

vector/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,7 @@ dependencies {
231231
kapt libs.dagger.hiltCompiler
232232

233233
// Analytics
234-
implementation('com.posthog.android:posthog:2.0.3') {
235-
exclude group: 'com.android.support', module: 'support-annotations'
236-
}
234+
implementation 'com.posthog:posthog-android:3.2.0'
237235
implementation libs.sentry.sentryAndroid
238236

239237
// UnifiedPush

vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
package im.vector.app.features.analytics.impl
1818

19-
import com.posthog.android.Options
20-
import com.posthog.android.PostHog
21-
import com.posthog.android.Properties
19+
import com.posthog.PostHogInterface
2220
import im.vector.app.core.di.NamedGlobalScope
2321
import im.vector.app.features.analytics.AnalyticsConfig
2422
import im.vector.app.features.analytics.VectorAnalytics
@@ -36,9 +34,6 @@ import timber.log.Timber
3634
import javax.inject.Inject
3735
import javax.inject.Singleton
3836

39-
private val REUSE_EXISTING_ID: String? = null
40-
private val IGNORED_OPTIONS: Options? = null
41-
4237
@Singleton
4338
class DefaultVectorAnalytics @Inject constructor(
4439
private val postHogFactory: PostHogFactory,
@@ -49,9 +44,9 @@ class DefaultVectorAnalytics @Inject constructor(
4944
@NamedGlobalScope private val globalScope: CoroutineScope
5045
) : VectorAnalytics {
5146

52-
private var posthog: PostHog? = null
47+
private var posthog: PostHogInterface? = null
5348

54-
private fun createPosthog(): PostHog? {
49+
private fun createPosthog(): PostHogInterface? {
5550
return when {
5651
analyticsConfig.isEnabled -> postHogFactory.createPosthog()
5752
else -> {
@@ -126,7 +121,7 @@ class DefaultVectorAnalytics @Inject constructor(
126121
posthog?.reset()
127122
} else {
128123
Timber.tag(analyticsTag.value).d("identify")
129-
posthog?.identify(id, lateInitUserPropertiesFactory.createUserProperties()?.getProperties()?.toPostHogUserProperties(), IGNORED_OPTIONS)
124+
posthog?.identify(id, lateInitUserPropertiesFactory.createUserProperties()?.getProperties()?.toPostHogUserProperties())
130125
}
131126
}
132127

@@ -155,16 +150,16 @@ class DefaultVectorAnalytics @Inject constructor(
155150
when (_userConsent) {
156151
true -> {
157152
posthog = createPosthog()
158-
posthog?.optOut(false)
153+
posthog?.optIn()
159154
identifyPostHog()
160155
pendingUserProperties?.let { doUpdateUserProperties(it) }
161156
pendingUserProperties = null
162157
}
163158
false -> {
164159
// When opting out, ensure that the queue is flushed first, or it will be flushed later (after user has revoked consent)
165160
posthog?.flush()
166-
posthog?.optOut(true)
167-
posthog?.shutdown()
161+
posthog?.optOut()
162+
posthog?.close()
168163
posthog = null
169164
}
170165
}
@@ -177,6 +172,7 @@ class DefaultVectorAnalytics @Inject constructor(
177172
?.takeIf { userConsent == true }
178173
?.capture(
179174
event.getName(),
175+
analyticsId,
180176
event.getProperties()?.toPostHogProperties()
181177
)
182178
}
@@ -197,27 +193,37 @@ class DefaultVectorAnalytics @Inject constructor(
197193
}
198194

199195
private fun doUpdateUserProperties(userProperties: UserProperties) {
196+
// we need a distinct id to set user properties
197+
val distinctId = analyticsId ?: return
200198
posthog
201199
?.takeIf { userConsent == true }
202-
?.identify(REUSE_EXISTING_ID, userProperties.getProperties()?.toPostHogUserProperties(), IGNORED_OPTIONS)
200+
?.identify(distinctId, userProperties.getProperties())
203201
}
204202

205-
private fun Map<String, Any?>?.toPostHogProperties(): Properties? {
203+
private fun Map<String, Any?>?.toPostHogProperties(): Map<String, Any>? {
206204
if (this == null) return null
207205

208-
return Properties().apply {
209-
putAll(this@toPostHogProperties)
206+
val nonNulls = HashMap<String, Any>()
207+
this.forEach { (key, value) ->
208+
if (value != null) {
209+
nonNulls[key] = value
210+
}
210211
}
212+
return nonNulls
211213
}
212214

213215
/**
214216
* We avoid sending nulls as part of the UserProperties as this will reset the values across all devices.
215217
* The UserProperties event has nullable properties to allow for clients to opt in.
216218
*/
217-
private fun Map<String, Any?>.toPostHogUserProperties(): Properties {
218-
return Properties().apply {
219-
putAll(this@toPostHogUserProperties.filter { it.value != null })
219+
private fun Map<String, Any?>.toPostHogUserProperties(): Map<String, Any> {
220+
val nonNulls = HashMap<String, Any>()
221+
this.forEach { (key, value) ->
222+
if (value != null) {
223+
nonNulls[key] = value
224+
}
220225
}
226+
return nonNulls
221227
}
222228

223229
override fun trackError(throwable: Throwable) {

vector/src/main/java/im/vector/app/features/analytics/impl/PostHogFactory.kt

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package im.vector.app.features.analytics.impl
1818

1919
import android.content.Context
20-
import com.posthog.android.PostHog
20+
import com.posthog.PostHogInterface
21+
import com.posthog.android.PostHogAndroid
22+
import com.posthog.android.PostHogAndroidConfig
2123
import im.vector.app.core.resources.BuildMeta
2224
import im.vector.app.features.analytics.AnalyticsConfig
2325
import javax.inject.Inject
@@ -28,29 +30,17 @@ class PostHogFactory @Inject constructor(
2830
private val buildMeta: BuildMeta,
2931
) {
3032

31-
fun createPosthog(): PostHog {
32-
return PostHog.Builder(context, analyticsConfig.postHogApiKey, analyticsConfig.postHogHost)
33-
// Record certain application events automatically! (off/false by default)
34-
// .captureApplicationLifecycleEvents()
35-
// Record screen views automatically! (off/false by default)
36-
// .recordScreenViews()
37-
// Capture deep links as part of the screen call. (off by default)
38-
// .captureDeepLinks()
39-
// Maximum number of events to keep in queue before flushing (default 20)
40-
// .flushQueueSize(20)
41-
// Max delay before flushing the queue (30 seconds)
42-
// .flushInterval(30, TimeUnit.SECONDS)
43-
// Enable or disable collection of ANDROID_ID (true)
44-
.collectDeviceId(false)
45-
.logLevel(getLogLevel())
46-
.build()
47-
}
48-
49-
private fun getLogLevel(): PostHog.LogLevel {
50-
return if (buildMeta.isDebug) {
51-
PostHog.LogLevel.DEBUG
52-
} else {
53-
PostHog.LogLevel.INFO
33+
fun createPosthog(): PostHogInterface {
34+
val config = PostHogAndroidConfig(
35+
apiKey = analyticsConfig.postHogApiKey,
36+
host = analyticsConfig.postHogHost,
37+
// we do that manually
38+
captureScreenViews = false,
39+
).also {
40+
if (buildMeta.isDebug) {
41+
it.debug = true
42+
}
5443
}
44+
return PostHogAndroid.with(context, config)
5545
}
5646
}

vector/src/test/java/im/vector/app/test/fakes/FakePostHogFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package im.vector.app.test.fakes
1818

19-
import com.posthog.android.PostHog
19+
import com.posthog.PostHogInterface
2020
import im.vector.app.features.analytics.impl.PostHogFactory
2121
import io.mockk.every
2222
import io.mockk.mockk
2323

24-
class FakePostHogFactory(postHog: PostHog) {
24+
class FakePostHogFactory(postHog: PostHogInterface) {
2525
val instance = mockk<PostHogFactory>().also {
2626
every { it.createPosthog() } returns postHog
2727
}

0 commit comments

Comments
 (0)