1+ /* eslint-disable @typescript-eslint/naming-convention */
2+ import { version } from "@common/utils/package" ;
13import type { Integration } from "@sentry/types" ;
4+ import { totalmem } from "os" ;
25import type FrontPostHog from "posthog-js" ;
36import type NodeJsPostHog from "posthog-node" ;
47
@@ -10,18 +13,26 @@ import { TrackerProvider } from "./TrackerProvider";
1013const TRACKER_FAKE_HOST = ( process . env . TRACKER_FAKE_HREF ?? "" ) . split ( "//" ) [ 1 ] ! ;
1114
1215const DEFAULT_$SET = {
13- // eslint-disable-next-line @typescript-eslint/naming-convention
1416 $current_url : process . env . TRACKER_FAKE_HREF ,
1517 $host : TRACKER_FAKE_HOST ,
1618 $pathname : "" ,
1719} ;
1820const DEFAULT_$SET_ONCE = {
19- // eslint-disable-next-line @typescript-eslint/naming-convention
2021 $initial_current_url : process . env . TRACKER_FAKE_HREF ,
21- // eslint-disable-next-line @typescript-eslint/naming-convention
2222 $initial_pathname : "" ,
2323 $pathname : "" ,
2424} ;
25+
26+ function getCommonProperties ( ) {
27+ return {
28+ arch : process . arch ,
29+ date : new Date ( ) ,
30+ os : process . platform ,
31+ ram : totalmem ( ) / 1024 / 1024 / 1024 ,
32+ version,
33+ } ;
34+ }
35+
2536export class PostHogProvider extends TrackerProvider <
2637 typeof FrontPostHog ,
2738 NodeJsPostHog
@@ -51,30 +62,30 @@ export class PostHogProvider extends TrackerProvider<
5162 this . tracker . capture ( {
5263 distinctId : this . appId ,
5364 event : "$identify" ,
65+ properties : getCommonProperties ( ) ,
5466 } ) ;
5567 this . inited = true ;
56- } else {
57- return new Promise < void > (
58- ( resolve ) =>
59- void import ( "posthog-js" ) . then ( ( { default : frontPostHog } ) => {
60- this . hijackPostHog ( frontPostHog ) ;
61- frontPostHog . init ( process . env . TRACKER_POSTHOG_API_KEY , {
62- /* eslint-disable @typescript-eslint/naming-convention */
63- api_host : process . env . TRACKER_POSTHOG_URL ,
64- autocapture : false ,
65- capture_pageview : false ,
66- disable_session_recording : true ,
67- loaded : ( posthog ) => {
68- this . tracker = posthog ;
69- this . tracker . identify ( this . appId ) ;
70- this . inited = true ;
71- resolve ( ) ;
72- } ,
73- /* eslint-enable @typescript-eslint/naming-convention */
74- } ) ;
75- } )
76- ) ;
68+ return ;
7769 }
70+
71+ return new Promise < void > (
72+ ( resolve ) =>
73+ void import ( "posthog-js" ) . then ( ( { default : frontPostHog } ) => {
74+ this . hijackPostHog ( frontPostHog ) ;
75+ frontPostHog . init ( process . env . TRACKER_POSTHOG_API_KEY , {
76+ api_host : process . env . TRACKER_POSTHOG_URL ,
77+ autocapture : false ,
78+ capture_pageview : false ,
79+ disable_session_recording : true ,
80+ loaded : ( posthog ) => {
81+ this . tracker = posthog ;
82+ this . tracker . identify ( this . appId , getCommonProperties ( ) ) ;
83+ this . inited = true ;
84+ resolve ( ) ;
85+ } ,
86+ } ) ;
87+ } )
88+ ) ;
7889 }
7990
8091 public async uninit ( ) : Promise < void > {
@@ -101,7 +112,13 @@ export class PostHogProvider extends TrackerProvider<
101112
102113 public track < TEvent extends TrackEvent > ( ...args : TrackArgs < TEvent > ) : void {
103114 const [ event , properties ] = args ;
104- if ( ! this . tracker || this . disabled ) return ;
115+ if ( ! this . tracker || this . disabled ) {
116+ disabled : this . disabled ,
117+ tracker : this . tracker ,
118+ } ) ;
119+ return ;
120+ }
121+
105122 if ( this . isMain ( this . tracker ) ) {
106123 this . tracker . capture ( {
107124 distinctId : this . appId ,
@@ -111,12 +128,15 @@ export class PostHogProvider extends TrackerProvider<
111128 } else {
112129 this . tracker . capture ( event , properties ) ;
113130 }
131+ console . log ( `Event ${ event } tracked successfully` ) ;
114132 }
115133
116134 public enable ( ) : void {
117135 if ( ! this . tracker ) return ;
118136 if ( ! this . isMain ( this . tracker ) ) {
119137 this . tracker . opt_in_capturing ( ) ;
138+ const optInProperties = getCommonProperties ( ) ;
139+ this . tracker . capture ( "$opt_in" , optInProperties ) ;
120140 }
121141 super . enable ( ) ;
122142 }
@@ -139,7 +159,6 @@ export class PostHogProvider extends TrackerProvider<
139159 const originalCaptureFn = posthog . capture . bind ( posthog ) ;
140160 const hijack = {
141161 $set : DEFAULT_$SET ,
142- // eslint-disable-next-line @typescript-eslint/naming-convention
143162 $set_once : DEFAULT_$SET_ONCE ,
144163 ...DEFAULT_$SET ,
145164 } ;
0 commit comments