16
16
17
17
package im.vector.app.features.analytics.impl
18
18
19
- import com.posthog.android.Options
20
- import com.posthog.android.PostHog
21
- import com.posthog.android.Properties
19
+ import com.posthog.PostHogInterface
22
20
import im.vector.app.core.di.NamedGlobalScope
23
21
import im.vector.app.features.analytics.AnalyticsConfig
24
22
import im.vector.app.features.analytics.VectorAnalytics
@@ -36,9 +34,6 @@ import timber.log.Timber
36
34
import javax.inject.Inject
37
35
import javax.inject.Singleton
38
36
39
- private val REUSE_EXISTING_ID : String? = null
40
- private val IGNORED_OPTIONS : Options ? = null
41
-
42
37
@Singleton
43
38
class DefaultVectorAnalytics @Inject constructor(
44
39
private val postHogFactory : PostHogFactory ,
@@ -49,9 +44,9 @@ class DefaultVectorAnalytics @Inject constructor(
49
44
@NamedGlobalScope private val globalScope : CoroutineScope
50
45
) : VectorAnalytics {
51
46
52
- private var posthog: PostHog ? = null
47
+ private var posthog: PostHogInterface ? = null
53
48
54
- private fun createPosthog (): PostHog ? {
49
+ private fun createPosthog (): PostHogInterface ? {
55
50
return when {
56
51
analyticsConfig.isEnabled -> postHogFactory.createPosthog()
57
52
else -> {
@@ -126,7 +121,7 @@ class DefaultVectorAnalytics @Inject constructor(
126
121
posthog?.reset()
127
122
} else {
128
123
Timber .tag(analyticsTag.value).d(" identify" )
129
- posthog?.identify(id, lateInitUserPropertiesFactory.createUserProperties()?.getProperties()?.toPostHogUserProperties(), IGNORED_OPTIONS )
124
+ posthog?.identify(id, lateInitUserPropertiesFactory.createUserProperties()?.getProperties()?.toPostHogUserProperties())
130
125
}
131
126
}
132
127
@@ -155,16 +150,16 @@ class DefaultVectorAnalytics @Inject constructor(
155
150
when (_userConsent ) {
156
151
true -> {
157
152
posthog = createPosthog()
158
- posthog?.optOut( false )
153
+ posthog?.optIn( )
159
154
identifyPostHog()
160
155
pendingUserProperties?.let { doUpdateUserProperties(it) }
161
156
pendingUserProperties = null
162
157
}
163
158
false -> {
164
159
// When opting out, ensure that the queue is flushed first, or it will be flushed later (after user has revoked consent)
165
160
posthog?.flush()
166
- posthog?.optOut(true )
167
- posthog?.shutdown ()
161
+ posthog?.optOut()
162
+ posthog?.close ()
168
163
posthog = null
169
164
}
170
165
}
@@ -177,6 +172,7 @@ class DefaultVectorAnalytics @Inject constructor(
177
172
?.takeIf { userConsent == true }
178
173
?.capture(
179
174
event.getName(),
175
+ analyticsId,
180
176
event.getProperties()?.toPostHogProperties()
181
177
)
182
178
}
@@ -197,27 +193,37 @@ class DefaultVectorAnalytics @Inject constructor(
197
193
}
198
194
199
195
private fun doUpdateUserProperties (userProperties : UserProperties ) {
196
+ // we need a distinct id to set user properties
197
+ val distinctId = analyticsId ? : return
200
198
posthog
201
199
?.takeIf { userConsent == true }
202
- ?.identify(REUSE_EXISTING_ID , userProperties.getProperties()?.toPostHogUserProperties(), IGNORED_OPTIONS )
200
+ ?.identify(distinctId , userProperties.getProperties())
203
201
}
204
202
205
- private fun Map <String , Any ?>?.toPostHogProperties (): Properties ? {
203
+ private fun Map <String , Any ?>?.toPostHogProperties (): Map < String , Any > ? {
206
204
if (this == null ) return null
207
205
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
+ }
210
211
}
212
+ return nonNulls
211
213
}
212
214
213
215
/* *
214
216
* We avoid sending nulls as part of the UserProperties as this will reset the values across all devices.
215
217
* The UserProperties event has nullable properties to allow for clients to opt in.
216
218
*/
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
+ }
220
225
}
226
+ return nonNulls
221
227
}
222
228
223
229
override fun trackError (throwable : Throwable ) {
0 commit comments