1
- /* eslint-disable camelcase, max-depth, sonarjs/cognitive-complexity */
1
+ /* eslint-disable camelcase, max-depth, sonarjs/cognitive-complexity, promise/no-nesting */
2
2
import posthog , { JsonRecord , JsonType } from 'posthog-js' ;
3
3
import dayjs from 'dayjs' ;
4
4
import { Wallet } from '@lace/cardano' ;
@@ -22,7 +22,7 @@ import {
22
22
import { BackgroundService , UserIdService } from '@lib/scripts/types' ;
23
23
import { experiments , getDefaultFeatureFlags } from '@providers/ExperimentsProvider/config' ;
24
24
import { ExperimentName } from '@providers/ExperimentsProvider/types' ;
25
- import { BehaviorSubject , Subscription } from 'rxjs' ;
25
+ import { BehaviorSubject , distinctUntilChanged , Observable , Subscription } from 'rxjs' ;
26
26
import { PostHogAction , PostHogProperties } from '@lace/common' ;
27
27
28
28
type FeatureFlag = 'create-paper-wallet' | 'restore-paper-wallet' | 'shared-wallets' ;
@@ -46,6 +46,8 @@ export class PostHogClient<Action extends string = string> {
46
46
private hasPostHogInitialized$ : BehaviorSubject < boolean > ;
47
47
private subscription : Subscription ;
48
48
private initSuccess : Promise < boolean > ;
49
+ private readonly optedInBeta$ : BehaviorSubject < boolean > ;
50
+ private updatePersonaProperties = false ;
49
51
featureFlags : GroupedFeatureFlags ;
50
52
constructor (
51
53
private chain : Wallet . Cardano . ChainId ,
@@ -58,45 +60,52 @@ export class PostHogClient<Action extends string = string> {
58
60
const token = this . getApiToken ( ) ;
59
61
if ( ! token ) throw new Error ( `posthog token has not been provided for chain: ${ this . chain . networkId } ` ) ;
60
62
this . hasPostHogInitialized$ = new BehaviorSubject ( false ) ;
61
-
62
- this . initSuccess = this . userIdService
63
- . getUserId ( chain . networkMagic )
64
- . then ( ( id ) => {
65
- posthog . init ( token , {
66
- request_batching : false ,
67
- api_host : this . postHogHost ,
68
- autocapture : false ,
69
- opt_out_useragent_filter : true ,
70
- disable_compression : true ,
71
- disable_session_recording : true ,
72
- capture_pageview : false ,
73
- capture_pageleave : false ,
74
- // Disables PostHog user ID persistence - we manage ID ourselves with userIdService
75
- disable_persistence : true ,
76
- disable_cookie : true ,
77
- persistence : 'memory' ,
78
- bootstrap : {
79
- distinctID : id ,
80
- isIdentifiedID : true
81
- } ,
82
- property_blacklist : [
83
- '$autocapture_disabled_server_side' ,
84
- '$console_log_recording_enabled_server_side' ,
85
- '$device_id' ,
86
- '$session_recording_recorder_version_server_side' ,
87
- '$time'
88
- ]
89
- } ) ;
63
+ this . optedInBeta$ = new BehaviorSubject ( false ) ;
64
+
65
+ this . backgroundServiceUtils
66
+ . getBackgroundStorage ( )
67
+ . then ( ( storage ) => {
68
+ this . optedInBeta$ . next ( storage ?. optedInBeta ?? false ) ;
69
+
70
+ this . initSuccess = this . userIdService
71
+ . getUserId ( chain . networkMagic )
72
+ . then ( ( id ) => {
73
+ posthog . init ( token , {
74
+ request_batching : false ,
75
+ api_host : this . postHogHost ,
76
+ autocapture : false ,
77
+ opt_out_useragent_filter : true ,
78
+ disable_compression : true ,
79
+ disable_session_recording : true ,
80
+ capture_pageview : false ,
81
+ capture_pageleave : false ,
82
+ // Disables PostHog user ID persistence - we manage ID ourselves with userIdService
83
+ disable_persistence : true ,
84
+ disable_cookie : true ,
85
+ persistence : 'memory' ,
86
+ bootstrap : {
87
+ distinctID : id ,
88
+ isIdentifiedID : true
89
+ } ,
90
+ property_blacklist : [
91
+ '$autocapture_disabled_server_side' ,
92
+ '$console_log_recording_enabled_server_side' ,
93
+ '$device_id' ,
94
+ '$session_recording_recorder_version_server_side' ,
95
+ '$time'
96
+ ]
97
+ } ) ;
98
+ } )
99
+ . then ( ( ) => true ) ;
100
+
101
+ this . subscribeToDistinctIdUpdate ( ) ;
102
+ this . loadExperiments ( ) ;
90
103
} )
91
- . then ( ( ) => true )
92
104
. catch ( ( ) => {
93
105
console . warn ( 'Analytics failed' ) ;
94
106
return false ;
95
107
} ) ;
96
108
97
- this . subscribeToDistinctIdUpdate ( ) ;
98
- this . loadExperiments ( ) ;
99
-
100
109
this . featureFlags = {
101
110
[ Wallet . Cardano . NetworkMagics . Mainnet ] : getDefaultFeatureFlags ( ) ,
102
111
[ Wallet . Cardano . NetworkMagics . Preprod ] : getDefaultFeatureFlags ( ) ,
@@ -203,6 +212,21 @@ export class PostHogClient<Action extends string = string> {
203
212
} ) ;
204
213
}
205
214
215
+ hasOptedInBeta ( ) : Observable < boolean > {
216
+ return this . optedInBeta$ . pipe ( distinctUntilChanged ( ) ) ;
217
+ }
218
+
219
+ async setOptedInBeta ( optedInBeta : boolean ) : Promise < void > {
220
+ await this . backgroundServiceUtils . setBackgroundStorage ( {
221
+ optedInBeta
222
+ } ) ;
223
+
224
+ this . updatePersonaProperties = true ;
225
+ this . optedInBeta$ . next ( optedInBeta ) ;
226
+
227
+ console . debug ( '[ANALYTICS] Changing Opted In Beta' , optedInBeta ) ;
228
+ }
229
+
206
230
isFeatureFlagEnabled ( feature : string ) : boolean {
207
231
return this . featureFlags [ this . chain . networkMagic ] ?. [ feature as FeatureFlag ] || false ;
208
232
}
@@ -281,6 +305,8 @@ export class PostHogClient<Action extends string = string> {
281
305
backgroundStorage ?. featureFlags [ this . chain . networkMagic ] || getDefaultFeatureFlags ( )
282
306
) ;
283
307
}
308
+
309
+ this . optedInBeta$ . next ( backgroundStorage ?. optedInBeta ) ;
284
310
} ) ;
285
311
this . hasPostHogInitialized$ . next ( true ) ;
286
312
}
@@ -307,15 +333,17 @@ export class PostHogClient<Action extends string = string> {
307
333
protected async getPersonProperties ( ) : Promise < PostHogPersonProperties | undefined > {
308
334
if ( ! this . userTrackingType ) {
309
335
this . userTrackingType = this . currentUserTrackingType ;
310
- // set user_tracking_type in the first event
311
- return { $set : { user_tracking_type : this . userTrackingType } } ;
336
+ // set user_tracking_type and opted_in_beta in the first event
337
+ return { $set : { user_tracking_type : this . userTrackingType , opted_in_beta : this . optedInBeta$ . value } } ;
312
338
}
313
339
314
340
// eslint-disable-next-line consistent-return
315
- if ( this . currentUserTrackingType === this . userTrackingType ) return ;
341
+ if ( this . currentUserTrackingType === this . userTrackingType && ! this . updatePersonaProperties ) return ;
342
+
343
+ this . updatePersonaProperties = false ;
316
344
this . userTrackingType = this . currentUserTrackingType ;
317
- // update user_tracking_type if tracking type has changed
318
- return { $set : { user_tracking_type : this . userTrackingType } } ;
345
+
346
+ return { $set : { user_tracking_type : this . userTrackingType , opted_in_beta : this . optedInBeta$ . value } } ;
319
347
}
320
348
321
349
static getAllowedNetworksMapFromPayloads ( payloads : Record < string , JsonType > ) : Record < string , string [ ] > {
0 commit comments