@vrplatform/log 2.0.0-alpha.13 → 2.0.0-alpha.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/main/tracking/index.d.ts +33 -27
- package/build/main/tracking/index.js +35 -18
- package/build/main/tracking/index.js.map +1 -1
- package/build/module/tracking/index.d.ts +33 -27
- package/build/module/tracking/index.js +35 -18
- package/build/module/tracking/index.js.map +1 -1
- package/package.json +1 -1
- package/src/tracking/index.ts +85 -41
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Analytics } from '@june-so/analytics-node';
|
|
2
2
|
import { PostHog } from 'posthog-node';
|
|
3
|
+
import { type WorkerLog } from '../log';
|
|
4
|
+
import { useIntercom } from './intercom';
|
|
3
5
|
export declare const isTest: (userId: string, groupId?: string, userEmail?: string) => boolean | "" | undefined;
|
|
4
6
|
export declare const convertKeysToSnakeCase: (obj?: Record<string, any>) => Record<string, any>;
|
|
5
7
|
type AuthEvent = 'account_signin_completed' | 'account_signup_code_requested' | 'account_signup_code_completed' | 'account_signup_code_failed' | 'account_signup_completed' | 'account_password_reset_requested' | 'account_password_reset_failed' | 'account_password_reset_completed' | 'account_invitation_accepted' | 'account_signed_out';
|
|
@@ -22,8 +24,26 @@ export type TrackingProps = {
|
|
|
22
24
|
JUNESO_TOKEN?: string;
|
|
23
25
|
POSTHOG_TOKEN?: string;
|
|
24
26
|
};
|
|
27
|
+
debugging?: boolean;
|
|
25
28
|
};
|
|
26
29
|
export type Tracking = ReturnType<typeof useTracking>;
|
|
30
|
+
type TrackProps = {
|
|
31
|
+
userId: string;
|
|
32
|
+
groupId: string;
|
|
33
|
+
event: TrackingEvent;
|
|
34
|
+
properties: Record<string, string | number | boolean | null | undefined>;
|
|
35
|
+
};
|
|
36
|
+
type IdentifyProps = {
|
|
37
|
+
userId: string;
|
|
38
|
+
traits: Record<string, any>;
|
|
39
|
+
disableGeoip?: boolean;
|
|
40
|
+
timestamp?: Date;
|
|
41
|
+
};
|
|
42
|
+
type GroupProps = OptionalUser<{
|
|
43
|
+
groupId: string;
|
|
44
|
+
traits?: Record<string, any>;
|
|
45
|
+
timestamp?: Date;
|
|
46
|
+
}>;
|
|
27
47
|
type OptionalUser<T> = T & ({
|
|
28
48
|
userId: string;
|
|
29
49
|
anonymousId?: string;
|
|
@@ -31,41 +51,27 @@ type OptionalUser<T> = T & ({
|
|
|
31
51
|
userId?: string;
|
|
32
52
|
anonymousId: string;
|
|
33
53
|
});
|
|
34
|
-
export declare const useTracking: ({ dataset, env, name }: TrackingProps, isDev?: boolean) => {
|
|
35
|
-
track: (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}) => Promise<void>;
|
|
41
|
-
identify: ({ traits, userId, disableGeoip, timestamp, }: {
|
|
42
|
-
userId: string;
|
|
43
|
-
traits: Record<string, any>;
|
|
44
|
-
disableGeoip?: boolean;
|
|
45
|
-
timestamp?: Date;
|
|
46
|
-
}) => void;
|
|
47
|
-
group: ({ traits, groupId, userId, anonymousId, timestamp, }: OptionalUser<{
|
|
48
|
-
groupId: string;
|
|
49
|
-
traits?: Record<string, any>;
|
|
50
|
-
timestamp?: Date;
|
|
51
|
-
}>) => void;
|
|
52
|
-
shutdown: () => Promise<[void | undefined, void | undefined, void]>;
|
|
53
|
-
alias: ((data: {
|
|
54
|
+
export declare const useTracking: ({ dataset, env, name, debugging }: TrackingProps, isDev?: boolean) => {
|
|
55
|
+
track: (props: TrackProps) => Promise<void>;
|
|
56
|
+
identify: (props: IdentifyProps) => Promise<void>;
|
|
57
|
+
group: (props: GroupProps) => Promise<void>;
|
|
58
|
+
shutdown: () => Promise<any>;
|
|
59
|
+
alias?: (data: {
|
|
54
60
|
distinctId: string;
|
|
55
61
|
alias: string;
|
|
56
62
|
disableGeoip?: boolean;
|
|
57
|
-
}) => void
|
|
58
|
-
isFeatureEnabled
|
|
63
|
+
}) => void;
|
|
64
|
+
isFeatureEnabled?: (key: string, distinctId: string, options?: {
|
|
59
65
|
groups?: Record<string, string>;
|
|
60
66
|
personProperties?: Record<string, string>;
|
|
61
67
|
groupProperties?: Record<string, Record<string, string>>;
|
|
62
68
|
onlyEvaluateLocally?: boolean;
|
|
63
69
|
sendFeatureFlagEvents?: boolean;
|
|
64
70
|
disableGeoip?: boolean;
|
|
65
|
-
}) => Promise<boolean | undefined
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
}) => Promise<boolean | undefined>;
|
|
72
|
+
log: WorkerLog;
|
|
73
|
+
june?: Analytics;
|
|
74
|
+
intercom?: ReturnType<typeof useIntercom>;
|
|
75
|
+
posthog?: PostHog;
|
|
70
76
|
};
|
|
71
77
|
export {};
|
|
@@ -40,12 +40,33 @@ const convertKeysToSnakeCase = (obj) => {
|
|
|
40
40
|
return result;
|
|
41
41
|
};
|
|
42
42
|
exports.convertKeysToSnakeCase = convertKeysToSnakeCase;
|
|
43
|
-
const useTracking = ({ dataset, env, name }, isDev) => {
|
|
44
|
-
|
|
43
|
+
const useTracking = ({ dataset, env, name, debugging }, isDev) => {
|
|
44
|
+
if (debugging) {
|
|
45
|
+
const log = {
|
|
46
|
+
...console,
|
|
47
|
+
child: () => log,
|
|
48
|
+
addContext: (data) => console.log(data),
|
|
49
|
+
flush: () => Promise.resolve(),
|
|
50
|
+
respond: () => Promise.resolve(new Response()),
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
track: async (props) => console.log(props),
|
|
54
|
+
identify: async (props) => console.log(props),
|
|
55
|
+
group: async (props) => console.log(props),
|
|
56
|
+
shutdown: () => Promise.resolve(),
|
|
57
|
+
log,
|
|
58
|
+
june: new analytics_node_1.Analytics(''),
|
|
59
|
+
intercom: (0, intercom_1.useIntercom)('', log),
|
|
60
|
+
posthog: new posthog_node_1.PostHog('', {
|
|
61
|
+
host: 'https://app.posthog.com',
|
|
62
|
+
}),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const log = (0, log_1.useLog)({ name, dataset, env });
|
|
45
66
|
const intercom = env.INTERCOM_TOKEN
|
|
46
|
-
? (0, intercom_1.useIntercom)(env.INTERCOM_TOKEN,
|
|
67
|
+
? (0, intercom_1.useIntercom)(env.INTERCOM_TOKEN, log)
|
|
47
68
|
: undefined;
|
|
48
|
-
const
|
|
69
|
+
const june = env.JUNESO_TOKEN ? new analytics_node_1.Analytics(env.JUNESO_TOKEN) : undefined;
|
|
49
70
|
const posthog = env.POSTHOG_TOKEN
|
|
50
71
|
? new posthog_node_1.PostHog(env.POSTHOG_TOKEN, {
|
|
51
72
|
host: 'https://app.posthog.com',
|
|
@@ -53,7 +74,7 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
53
74
|
: undefined;
|
|
54
75
|
return {
|
|
55
76
|
// General
|
|
56
|
-
track: async ({ userId, groupId, event, properties
|
|
77
|
+
track: async ({ userId, groupId, event, properties }) => {
|
|
57
78
|
if (isDev || (0, exports.isTest)(userId, groupId))
|
|
58
79
|
return;
|
|
59
80
|
posthog?.capture({
|
|
@@ -64,7 +85,7 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
64
85
|
},
|
|
65
86
|
properties: (0, exports.convertKeysToSnakeCase)(properties),
|
|
66
87
|
});
|
|
67
|
-
|
|
88
|
+
june?.track({
|
|
68
89
|
userId,
|
|
69
90
|
event,
|
|
70
91
|
properties,
|
|
@@ -84,7 +105,7 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
84
105
|
console.error(error);
|
|
85
106
|
}
|
|
86
107
|
},
|
|
87
|
-
identify: ({ traits, userId, disableGeoip, timestamp, }) => {
|
|
108
|
+
identify: async ({ traits, userId, disableGeoip, timestamp, }) => {
|
|
88
109
|
if (isDev || (0, exports.isTest)(userId))
|
|
89
110
|
return;
|
|
90
111
|
posthog?.identify({
|
|
@@ -92,7 +113,7 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
92
113
|
properties: (0, exports.convertKeysToSnakeCase)(traits),
|
|
93
114
|
disableGeoip,
|
|
94
115
|
});
|
|
95
|
-
|
|
116
|
+
june?.identify({
|
|
96
117
|
userId,
|
|
97
118
|
traits,
|
|
98
119
|
timestamp,
|
|
@@ -101,7 +122,7 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
101
122
|
console.error(err);
|
|
102
123
|
});
|
|
103
124
|
},
|
|
104
|
-
group: ({ traits, groupId, userId, anonymousId, timestamp, }) => {
|
|
125
|
+
group: async ({ traits, groupId, userId, anonymousId, timestamp, }) => {
|
|
105
126
|
if (isDev || (0, exports.isTest)(userId || anonymousId || '', groupId))
|
|
106
127
|
return;
|
|
107
128
|
posthog?.groupIdentify({
|
|
@@ -111,29 +132,25 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
111
132
|
distinctId: userId,
|
|
112
133
|
});
|
|
113
134
|
if (anonymousId)
|
|
114
|
-
|
|
135
|
+
june?.group({ anonymousId, groupId, traits, timestamp }, (err) => {
|
|
115
136
|
if (err)
|
|
116
137
|
console.error(err);
|
|
117
138
|
});
|
|
118
139
|
else if (userId)
|
|
119
|
-
|
|
140
|
+
june?.group({ userId, groupId, traits, timestamp }, (err) => {
|
|
120
141
|
if (err)
|
|
121
142
|
console.error(err);
|
|
122
143
|
});
|
|
123
144
|
},
|
|
124
|
-
shutdown: async () => Promise.all([
|
|
125
|
-
posthog?.shutdown(),
|
|
126
|
-
juneso?.closeAndFlush(),
|
|
127
|
-
axiom?.flush(),
|
|
128
|
-
]),
|
|
145
|
+
shutdown: async () => Promise.all([posthog?.shutdown(), june?.closeAndFlush(), log?.flush()]),
|
|
129
146
|
// Posthog specific
|
|
130
147
|
alias: posthog?.alias,
|
|
131
148
|
isFeatureEnabled: posthog?.isFeatureEnabled,
|
|
132
149
|
// export intercom and axiom
|
|
133
150
|
intercom,
|
|
134
151
|
posthog,
|
|
135
|
-
june
|
|
136
|
-
log
|
|
152
|
+
june,
|
|
153
|
+
log,
|
|
137
154
|
};
|
|
138
155
|
};
|
|
139
156
|
exports.useTracking = useTracking;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["tracking/index.ts"],"names":[],"mappings":";;;AAAA,4DAAoD;AACpD,+CAAuC;AACvC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["tracking/index.ts"],"names":[],"mappings":";;;AAAA,4DAAoD;AACpD,+CAAuC;AACvC,gCAAgD;AAChD,yCAAyC;AAEzC,MAAM,cAAc,GAAG;IACrB,gCAAgC,EAAE,sCAAsC;IACxE,+BAA+B,EAAE,sCAAsC;IACvE,0BAA0B,EAAE,sCAAsC;IAClE,2BAA2B,EAAE,sCAAsC;CACpE,CAAC;AAEF,MAAM,cAAc,GAAG;IACrB,uBAAuB,EAAE,sCAAsC;IAC/D,iBAAiB,EAAE,sCAAsC;CAC1D,CAAC;AAEK,MAAM,MAAM,GAAG,CAAC,MAAc,EAAE,OAAgB,EAAE,SAAkB,EAAE,EAAE,CAC7E,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC;IAC9C,CAAC,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC5D,CAAC,SAAS,IAAI,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC;AAHpD,QAAA,MAAM,UAG8C;AAEjE,MAAM,gBAAgB,GAAG,CAAC,GAAW,EAAE,EAAE,CACvC,GAAG,CAAC,OAAO,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC,WAAW,EAAE,CAAC;AAExD,sBAAsB;AACf,MAAM,sBAAsB,GAAG,CAAC,GAAyB,EAAE,EAAE;IAClE,MAAM,MAAM,GAAwB,EAAE,CAAC;IACvC,MAAM,cAAc,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC;IAE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|
|
2
2
|
import { PostHog } from 'posthog-node';
|
|
3
|
+
import { type WorkerLog } from '../log';
|
|
4
|
+
import { useIntercom } from './intercom';
|
|
3
5
|
export declare const isTest: (userId: string, groupId?: string, userEmail?: string) => boolean | "" | undefined;
|
|
4
6
|
export declare const convertKeysToSnakeCase: (obj?: Record<string, any>) => Record<string, any>;
|
|
5
7
|
type AuthEvent = 'account_signin_completed' | 'account_signup_code_requested' | 'account_signup_code_completed' | 'account_signup_code_failed' | 'account_signup_completed' | 'account_password_reset_requested' | 'account_password_reset_failed' | 'account_password_reset_completed' | 'account_invitation_accepted' | 'account_signed_out';
|
|
@@ -22,8 +24,26 @@ export type TrackingProps = {
|
|
|
22
24
|
JUNESO_TOKEN?: string;
|
|
23
25
|
POSTHOG_TOKEN?: string;
|
|
24
26
|
};
|
|
27
|
+
debugging?: boolean;
|
|
25
28
|
};
|
|
26
29
|
export type Tracking = ReturnType<typeof useTracking>;
|
|
30
|
+
type TrackProps = {
|
|
31
|
+
userId: string;
|
|
32
|
+
groupId: string;
|
|
33
|
+
event: TrackingEvent;
|
|
34
|
+
properties: Record<string, string | number | boolean | null | undefined>;
|
|
35
|
+
};
|
|
36
|
+
type IdentifyProps = {
|
|
37
|
+
userId: string;
|
|
38
|
+
traits: Record<string, any>;
|
|
39
|
+
disableGeoip?: boolean;
|
|
40
|
+
timestamp?: Date;
|
|
41
|
+
};
|
|
42
|
+
type GroupProps = OptionalUser<{
|
|
43
|
+
groupId: string;
|
|
44
|
+
traits?: Record<string, any>;
|
|
45
|
+
timestamp?: Date;
|
|
46
|
+
}>;
|
|
27
47
|
type OptionalUser<T> = T & ({
|
|
28
48
|
userId: string;
|
|
29
49
|
anonymousId?: string;
|
|
@@ -31,41 +51,27 @@ type OptionalUser<T> = T & ({
|
|
|
31
51
|
userId?: string;
|
|
32
52
|
anonymousId: string;
|
|
33
53
|
});
|
|
34
|
-
export declare const useTracking: ({ dataset, env, name }: TrackingProps, isDev?: boolean) => {
|
|
35
|
-
track: (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}) => Promise<void>;
|
|
41
|
-
identify: ({ traits, userId, disableGeoip, timestamp, }: {
|
|
42
|
-
userId: string;
|
|
43
|
-
traits: Record<string, any>;
|
|
44
|
-
disableGeoip?: boolean;
|
|
45
|
-
timestamp?: Date;
|
|
46
|
-
}) => void;
|
|
47
|
-
group: ({ traits, groupId, userId, anonymousId, timestamp, }: OptionalUser<{
|
|
48
|
-
groupId: string;
|
|
49
|
-
traits?: Record<string, any>;
|
|
50
|
-
timestamp?: Date;
|
|
51
|
-
}>) => void;
|
|
52
|
-
shutdown: () => Promise<[void | undefined, void | undefined, void]>;
|
|
53
|
-
alias: ((data: {
|
|
54
|
+
export declare const useTracking: ({ dataset, env, name, debugging }: TrackingProps, isDev?: boolean) => {
|
|
55
|
+
track: (props: TrackProps) => Promise<void>;
|
|
56
|
+
identify: (props: IdentifyProps) => Promise<void>;
|
|
57
|
+
group: (props: GroupProps) => Promise<void>;
|
|
58
|
+
shutdown: () => Promise<any>;
|
|
59
|
+
alias?: (data: {
|
|
54
60
|
distinctId: string;
|
|
55
61
|
alias: string;
|
|
56
62
|
disableGeoip?: boolean;
|
|
57
|
-
}) => void
|
|
58
|
-
isFeatureEnabled
|
|
63
|
+
}) => void;
|
|
64
|
+
isFeatureEnabled?: (key: string, distinctId: string, options?: {
|
|
59
65
|
groups?: Record<string, string>;
|
|
60
66
|
personProperties?: Record<string, string>;
|
|
61
67
|
groupProperties?: Record<string, Record<string, string>>;
|
|
62
68
|
onlyEvaluateLocally?: boolean;
|
|
63
69
|
sendFeatureFlagEvents?: boolean;
|
|
64
70
|
disableGeoip?: boolean;
|
|
65
|
-
}) => Promise<boolean | undefined
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
}) => Promise<boolean | undefined>;
|
|
72
|
+
log: WorkerLog;
|
|
73
|
+
june?: Analytics;
|
|
74
|
+
intercom?: ReturnType<typeof useIntercom>;
|
|
75
|
+
posthog?: PostHog;
|
|
70
76
|
};
|
|
71
77
|
export {};
|
|
@@ -35,12 +35,33 @@ export const convertKeysToSnakeCase = (obj) => {
|
|
|
35
35
|
}
|
|
36
36
|
return result;
|
|
37
37
|
};
|
|
38
|
-
export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
39
|
-
|
|
38
|
+
export const useTracking = ({ dataset, env, name, debugging }, isDev) => {
|
|
39
|
+
if (debugging) {
|
|
40
|
+
const log = {
|
|
41
|
+
...console,
|
|
42
|
+
child: () => log,
|
|
43
|
+
addContext: (data) => console.log(data),
|
|
44
|
+
flush: () => Promise.resolve(),
|
|
45
|
+
respond: () => Promise.resolve(new Response()),
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
track: async (props) => console.log(props),
|
|
49
|
+
identify: async (props) => console.log(props),
|
|
50
|
+
group: async (props) => console.log(props),
|
|
51
|
+
shutdown: () => Promise.resolve(),
|
|
52
|
+
log,
|
|
53
|
+
june: new Analytics(''),
|
|
54
|
+
intercom: useIntercom('', log),
|
|
55
|
+
posthog: new PostHog('', {
|
|
56
|
+
host: 'https://app.posthog.com',
|
|
57
|
+
}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const log = useLog({ name, dataset, env });
|
|
40
61
|
const intercom = env.INTERCOM_TOKEN
|
|
41
|
-
? useIntercom(env.INTERCOM_TOKEN,
|
|
62
|
+
? useIntercom(env.INTERCOM_TOKEN, log)
|
|
42
63
|
: undefined;
|
|
43
|
-
const
|
|
64
|
+
const june = env.JUNESO_TOKEN ? new Analytics(env.JUNESO_TOKEN) : undefined;
|
|
44
65
|
const posthog = env.POSTHOG_TOKEN
|
|
45
66
|
? new PostHog(env.POSTHOG_TOKEN, {
|
|
46
67
|
host: 'https://app.posthog.com',
|
|
@@ -48,7 +69,7 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
48
69
|
: undefined;
|
|
49
70
|
return {
|
|
50
71
|
// General
|
|
51
|
-
track: async ({ userId, groupId, event, properties
|
|
72
|
+
track: async ({ userId, groupId, event, properties }) => {
|
|
52
73
|
if (isDev || isTest(userId, groupId))
|
|
53
74
|
return;
|
|
54
75
|
posthog?.capture({
|
|
@@ -59,7 +80,7 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
59
80
|
},
|
|
60
81
|
properties: convertKeysToSnakeCase(properties),
|
|
61
82
|
});
|
|
62
|
-
|
|
83
|
+
june?.track({
|
|
63
84
|
userId,
|
|
64
85
|
event,
|
|
65
86
|
properties,
|
|
@@ -79,7 +100,7 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
79
100
|
console.error(error);
|
|
80
101
|
}
|
|
81
102
|
},
|
|
82
|
-
identify: ({ traits, userId, disableGeoip, timestamp, }) => {
|
|
103
|
+
identify: async ({ traits, userId, disableGeoip, timestamp, }) => {
|
|
83
104
|
if (isDev || isTest(userId))
|
|
84
105
|
return;
|
|
85
106
|
posthog?.identify({
|
|
@@ -87,7 +108,7 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
87
108
|
properties: convertKeysToSnakeCase(traits),
|
|
88
109
|
disableGeoip,
|
|
89
110
|
});
|
|
90
|
-
|
|
111
|
+
june?.identify({
|
|
91
112
|
userId,
|
|
92
113
|
traits,
|
|
93
114
|
timestamp,
|
|
@@ -96,7 +117,7 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
96
117
|
console.error(err);
|
|
97
118
|
});
|
|
98
119
|
},
|
|
99
|
-
group: ({ traits, groupId, userId, anonymousId, timestamp, }) => {
|
|
120
|
+
group: async ({ traits, groupId, userId, anonymousId, timestamp, }) => {
|
|
100
121
|
if (isDev || isTest(userId || anonymousId || '', groupId))
|
|
101
122
|
return;
|
|
102
123
|
posthog?.groupIdentify({
|
|
@@ -106,29 +127,25 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
106
127
|
distinctId: userId,
|
|
107
128
|
});
|
|
108
129
|
if (anonymousId)
|
|
109
|
-
|
|
130
|
+
june?.group({ anonymousId, groupId, traits, timestamp }, (err) => {
|
|
110
131
|
if (err)
|
|
111
132
|
console.error(err);
|
|
112
133
|
});
|
|
113
134
|
else if (userId)
|
|
114
|
-
|
|
135
|
+
june?.group({ userId, groupId, traits, timestamp }, (err) => {
|
|
115
136
|
if (err)
|
|
116
137
|
console.error(err);
|
|
117
138
|
});
|
|
118
139
|
},
|
|
119
|
-
shutdown: async () => Promise.all([
|
|
120
|
-
posthog?.shutdown(),
|
|
121
|
-
juneso?.closeAndFlush(),
|
|
122
|
-
axiom?.flush(),
|
|
123
|
-
]),
|
|
140
|
+
shutdown: async () => Promise.all([posthog?.shutdown(), june?.closeAndFlush(), log?.flush()]),
|
|
124
141
|
// Posthog specific
|
|
125
142
|
alias: posthog?.alias,
|
|
126
143
|
isFeatureEnabled: posthog?.isFeatureEnabled,
|
|
127
144
|
// export intercom and axiom
|
|
128
145
|
intercom,
|
|
129
146
|
posthog,
|
|
130
|
-
june
|
|
131
|
-
log
|
|
147
|
+
june,
|
|
148
|
+
log,
|
|
132
149
|
};
|
|
133
150
|
};
|
|
134
151
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["tracking/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["tracking/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAkB,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,MA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AE,SAAS,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE;oBAC/D,IAAI,GAAG;wBAAE,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBAC9B,CAAC,CAAC,CAAC;iBACA,IAAI,MAAM;gBACb,IAAI,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE;oBAC1D,IAAI,GAAG;wBAAE,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBAC9B,CAAC,CAAC,CAAC;QACP,CAAC;QACD,QAAQ,EAAE,KAAK,IAAI,EAAE,CACnB,OAAO,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;QACzE,mBAAmB;QACnB,KAAK,EAAE,OAAO,EAAE,KAAK;QACrB,gBAAgB,EAAE,OAAO,EAAE,gBAAgB;QAC3C,4BAA4B;QAC5B,QAAQ;QACR,OAAO;QACP,IAAI;QACJ,GAAG;KACJ,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/tracking/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Analytics } from '@june-so/analytics-node';
|
|
2
2
|
import { PostHog } from 'posthog-node';
|
|
3
|
-
import { useLog } from '../log';
|
|
3
|
+
import { type WorkerLog, useLog } from '../log';
|
|
4
4
|
import { useIntercom } from './intercom';
|
|
5
5
|
|
|
6
6
|
const E2E_TEST_USERS = {
|
|
@@ -143,9 +143,27 @@ export type TrackingProps = {
|
|
|
143
143
|
JUNESO_TOKEN?: string;
|
|
144
144
|
POSTHOG_TOKEN?: string;
|
|
145
145
|
};
|
|
146
|
+
debugging?: boolean;
|
|
146
147
|
};
|
|
147
148
|
export type Tracking = ReturnType<typeof useTracking>;
|
|
148
149
|
|
|
150
|
+
type TrackProps = {
|
|
151
|
+
userId: string;
|
|
152
|
+
groupId: string;
|
|
153
|
+
event: TrackingEvent;
|
|
154
|
+
properties: Record<string, string | number | boolean | null | undefined>;
|
|
155
|
+
};
|
|
156
|
+
type IdentifyProps = {
|
|
157
|
+
userId: string;
|
|
158
|
+
traits: Record<string, any>;
|
|
159
|
+
disableGeoip?: boolean;
|
|
160
|
+
timestamp?: Date;
|
|
161
|
+
};
|
|
162
|
+
type GroupProps = OptionalUser<{
|
|
163
|
+
groupId: string;
|
|
164
|
+
traits?: Record<string, any>;
|
|
165
|
+
timestamp?: Date;
|
|
166
|
+
}>;
|
|
149
167
|
type OptionalUser<T> = T &
|
|
150
168
|
(
|
|
151
169
|
| { userId: string; anonymousId?: string }
|
|
@@ -156,14 +174,63 @@ type OptionalUser<T> = T &
|
|
|
156
174
|
);
|
|
157
175
|
|
|
158
176
|
export const useTracking = (
|
|
159
|
-
{ dataset, env, name }: TrackingProps,
|
|
177
|
+
{ dataset, env, name, debugging }: TrackingProps,
|
|
160
178
|
isDev?: boolean
|
|
161
|
-
)
|
|
162
|
-
|
|
179
|
+
): {
|
|
180
|
+
track: (props: TrackProps) => Promise<void>;
|
|
181
|
+
identify: (props: IdentifyProps) => Promise<void>;
|
|
182
|
+
group: (props: GroupProps) => Promise<void>;
|
|
183
|
+
shutdown: () => Promise<any>;
|
|
184
|
+
alias?: (data: {
|
|
185
|
+
distinctId: string;
|
|
186
|
+
alias: string;
|
|
187
|
+
disableGeoip?: boolean;
|
|
188
|
+
}) => void;
|
|
189
|
+
isFeatureEnabled?: (
|
|
190
|
+
key: string,
|
|
191
|
+
distinctId: string,
|
|
192
|
+
options?: {
|
|
193
|
+
groups?: Record<string, string>;
|
|
194
|
+
personProperties?: Record<string, string>;
|
|
195
|
+
groupProperties?: Record<string, Record<string, string>>;
|
|
196
|
+
onlyEvaluateLocally?: boolean;
|
|
197
|
+
sendFeatureFlagEvents?: boolean;
|
|
198
|
+
disableGeoip?: boolean;
|
|
199
|
+
}
|
|
200
|
+
) => Promise<boolean | undefined>;
|
|
201
|
+
log: WorkerLog;
|
|
202
|
+
june?: Analytics;
|
|
203
|
+
intercom?: ReturnType<typeof useIntercom>;
|
|
204
|
+
posthog?: PostHog;
|
|
205
|
+
} => {
|
|
206
|
+
if (debugging) {
|
|
207
|
+
const log: WorkerLog = {
|
|
208
|
+
...console,
|
|
209
|
+
child: () => log,
|
|
210
|
+
addContext: (data: Record<string, any>) => console.log(data),
|
|
211
|
+
flush: () => Promise.resolve(),
|
|
212
|
+
respond: () => Promise.resolve(new Response()),
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
track: async (props: TrackProps) => console.log(props),
|
|
217
|
+
identify: async (props: IdentifyProps) => console.log(props),
|
|
218
|
+
group: async (props: GroupProps) => console.log(props),
|
|
219
|
+
shutdown: () => Promise.resolve(),
|
|
220
|
+
log,
|
|
221
|
+
june: new Analytics(''),
|
|
222
|
+
intercom: useIntercom('', log),
|
|
223
|
+
posthog: new PostHog('', {
|
|
224
|
+
host: 'https://app.posthog.com',
|
|
225
|
+
}),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const log = useLog({ name, dataset, env });
|
|
163
230
|
const intercom = env.INTERCOM_TOKEN
|
|
164
|
-
? useIntercom(env.INTERCOM_TOKEN,
|
|
231
|
+
? useIntercom(env.INTERCOM_TOKEN, log)
|
|
165
232
|
: undefined;
|
|
166
|
-
const
|
|
233
|
+
const june = env.JUNESO_TOKEN ? new Analytics(env.JUNESO_TOKEN) : undefined;
|
|
167
234
|
const posthog = env.POSTHOG_TOKEN
|
|
168
235
|
? new PostHog(env.POSTHOG_TOKEN, {
|
|
169
236
|
host: 'https://app.posthog.com',
|
|
@@ -172,17 +239,7 @@ export const useTracking = (
|
|
|
172
239
|
|
|
173
240
|
return {
|
|
174
241
|
// General
|
|
175
|
-
track: async ({
|
|
176
|
-
userId,
|
|
177
|
-
groupId,
|
|
178
|
-
event,
|
|
179
|
-
properties,
|
|
180
|
-
}: {
|
|
181
|
-
userId: string;
|
|
182
|
-
groupId: string;
|
|
183
|
-
event: TrackingEvent;
|
|
184
|
-
properties: Record<string, string | number | boolean | null | undefined>;
|
|
185
|
-
}) => {
|
|
242
|
+
track: async ({ userId, groupId, event, properties }: TrackProps) => {
|
|
186
243
|
if (isDev || isTest(userId, groupId)) return;
|
|
187
244
|
|
|
188
245
|
posthog?.capture({
|
|
@@ -193,7 +250,7 @@ export const useTracking = (
|
|
|
193
250
|
},
|
|
194
251
|
properties: convertKeysToSnakeCase(properties),
|
|
195
252
|
});
|
|
196
|
-
|
|
253
|
+
june?.track(
|
|
197
254
|
{
|
|
198
255
|
userId,
|
|
199
256
|
event,
|
|
@@ -215,17 +272,12 @@ export const useTracking = (
|
|
|
215
272
|
console.error(error);
|
|
216
273
|
}
|
|
217
274
|
},
|
|
218
|
-
identify: ({
|
|
275
|
+
identify: async ({
|
|
219
276
|
traits,
|
|
220
277
|
userId,
|
|
221
278
|
disableGeoip,
|
|
222
279
|
timestamp,
|
|
223
|
-
}: {
|
|
224
|
-
userId: string;
|
|
225
|
-
traits: Record<string, any>;
|
|
226
|
-
disableGeoip?: boolean;
|
|
227
|
-
timestamp?: Date;
|
|
228
|
-
}) => {
|
|
280
|
+
}: IdentifyProps) => {
|
|
229
281
|
if (isDev || isTest(userId)) return;
|
|
230
282
|
|
|
231
283
|
posthog?.identify({
|
|
@@ -233,7 +285,7 @@ export const useTracking = (
|
|
|
233
285
|
properties: convertKeysToSnakeCase(traits),
|
|
234
286
|
disableGeoip,
|
|
235
287
|
});
|
|
236
|
-
|
|
288
|
+
june?.identify(
|
|
237
289
|
{
|
|
238
290
|
userId,
|
|
239
291
|
traits,
|
|
@@ -244,17 +296,13 @@ export const useTracking = (
|
|
|
244
296
|
}
|
|
245
297
|
);
|
|
246
298
|
},
|
|
247
|
-
group: ({
|
|
299
|
+
group: async ({
|
|
248
300
|
traits,
|
|
249
301
|
groupId,
|
|
250
302
|
userId,
|
|
251
303
|
anonymousId,
|
|
252
304
|
timestamp,
|
|
253
|
-
}:
|
|
254
|
-
groupId: string;
|
|
255
|
-
traits?: Record<string, any>;
|
|
256
|
-
timestamp?: Date;
|
|
257
|
-
}>) => {
|
|
305
|
+
}: GroupProps) => {
|
|
258
306
|
if (isDev || isTest(userId || anonymousId || '', groupId)) return;
|
|
259
307
|
|
|
260
308
|
posthog?.groupIdentify({
|
|
@@ -264,27 +312,23 @@ export const useTracking = (
|
|
|
264
312
|
distinctId: userId,
|
|
265
313
|
});
|
|
266
314
|
if (anonymousId)
|
|
267
|
-
|
|
315
|
+
june?.group({ anonymousId, groupId, traits, timestamp }, (err) => {
|
|
268
316
|
if (err) console.error(err);
|
|
269
317
|
});
|
|
270
318
|
else if (userId)
|
|
271
|
-
|
|
319
|
+
june?.group({ userId, groupId, traits, timestamp }, (err) => {
|
|
272
320
|
if (err) console.error(err);
|
|
273
321
|
});
|
|
274
322
|
},
|
|
275
323
|
shutdown: async () =>
|
|
276
|
-
Promise.all([
|
|
277
|
-
posthog?.shutdown(),
|
|
278
|
-
juneso?.closeAndFlush(),
|
|
279
|
-
axiom?.flush(),
|
|
280
|
-
]),
|
|
324
|
+
Promise.all([posthog?.shutdown(), june?.closeAndFlush(), log?.flush()]),
|
|
281
325
|
// Posthog specific
|
|
282
326
|
alias: posthog?.alias,
|
|
283
327
|
isFeatureEnabled: posthog?.isFeatureEnabled,
|
|
284
328
|
// export intercom and axiom
|
|
285
329
|
intercom,
|
|
286
330
|
posthog,
|
|
287
|
-
june
|
|
288
|
-
log
|
|
331
|
+
june,
|
|
332
|
+
log,
|
|
289
333
|
};
|
|
290
334
|
};
|