@vrplatform/log 2.0.0-alpha.2 → 2.0.0-alpha.21

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.
@@ -1,15 +1,49 @@
1
- import { type IntercomAPI, useIntercom } from './intercom';
1
+ import { Analytics } from '@june-so/analytics-node';
2
+ import { PostHog } from 'posthog-node';
3
+ import { type WorkerLog } from '../log';
4
+ import { useIntercom } from './intercom';
5
+ export declare const isTest: (userId: string, groupId?: string, userEmail?: string) => boolean | "" | undefined;
6
+ export declare const convertKeysToSnakeCase: (obj?: Record<string, any>) => Record<string, any>;
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';
8
+ type TeamEvent = 'team_added' | 'team_info_updated' | 'team_deleted' | 'team_member_removed';
9
+ type ConnectionEvent = 'connection_created' | 'connection_reconnected' | 'connection_deleted' | 'connection_requested';
10
+ type OwnershipEvent = 'ownership_created' | 'ownership_updated' | 'ownership_deleted';
11
+ type OwnerEvent = 'owner_created' | 'owner_updated' | 'owner_deleted' | 'owner_tax_info_modal_opened' | 'owner_tax_info_modal_submitted';
12
+ type SetupEvent = 'setup_classes_set' | 'setup_accounting_version_completed' | 'setup_accounting_config_completed' | 'setup_owner_imported' | 'setup_listing_imported';
13
+ type AutomationEvent = 'automation_created' | 'automation_updated' | 'automation_deleted';
14
+ type TaxStatementEvent = 'tax_statement_preview_opened' | 'tax_statement_downloaded';
15
+ type GLOwnerStatementEvent = 'gl_owner_statement_preview_opened' | 'gl_owner_statement_downloaded';
16
+ type HyperlineEvent = 'hyperline_invoice_ready' | 'hyperline_invoice_settled' | 'hyperline_trial_started' | 'hyperline_trial_ended' | 'hyperline_subscription_activated' | 'hyperline_subscription_cancelled' | 'hyperline_subscription_created' | 'hyperline_subscription_errored' | 'hyperline_subscription_paused' | 'hyperline_subscription_voided' | 'hyperline_subscription_charged' | 'hyperline_customer_created' | 'hyperline_customer_updated' | 'hyperline_payment_method_created' | 'hyperline_payment_method_errored' | 'hyperline_payment_method_deleted';
17
+ type UserEvent = 'update_refresh_accepted' | 'owner_statement_opened';
18
+ type TestEvent = 'test_event' | 'test_error';
19
+ export type TrackingEvent = AuthEvent | TeamEvent | ConnectionEvent | OwnershipEvent | OwnerEvent | SetupEvent | AutomationEvent | TaxStatementEvent | GLOwnerStatementEvent | HyperlineEvent | UserEvent | TestEvent;
2
20
  export type TrackingProps = {
3
21
  name: string;
4
22
  dataset: string;
5
23
  env: {
6
- INTERCOM_TOKEN: string;
7
- JUNESO_ANALYTICS_WRITE_KEY: string;
8
- POSTHOG_API_KEY: string;
24
+ INTERCOM_TOKEN?: string;
25
+ JUNESO_TOKEN?: string;
26
+ POSTHOG_TOKEN?: string;
9
27
  };
28
+ debugging?: boolean;
10
29
  };
11
30
  export type Tracking = ReturnType<typeof useTracking>;
12
- export type Intercom = ReturnType<typeof useIntercom>;
31
+ type TrackProps = OptionalUser<{
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
+ }>;
13
47
  type OptionalUser<T> = T & ({
14
48
  userId: string;
15
49
  anonymousId?: string;
@@ -17,37 +51,14 @@ type OptionalUser<T> = T & ({
17
51
  userId?: string;
18
52
  anonymousId: string;
19
53
  });
20
- export declare const useTracking: ({ dataset, env, name }: TrackingProps, isDev?: boolean) => {
21
- track: ({ userId, groupId, event, properties, }: {
22
- userId: string;
23
- groupId: string;
24
- event: string;
25
- properties: Record<string, any>;
26
- }) => Promise<void>;
27
- identify: ({ traits, userId, disableGeoip, }: {
28
- userId: string;
29
- traits: Record<string, any>;
30
- disableGeoip?: boolean;
31
- }) => void;
32
- group: ({ traits, groupId, userId, anonymousId, }: OptionalUser<{
33
- groupId: string;
34
- traits?: Record<string, any>;
35
- }>) => void;
36
- shutdown: () => Promise<[void, void, void]>;
37
- alias: (data: {
38
- distinctId: string;
39
- alias: string;
40
- disableGeoip?: boolean;
41
- }) => void;
42
- isFeatureEnabled: (key: string, distinctId: string, options?: {
43
- groups?: Record<string, string>;
44
- personProperties?: Record<string, string>;
45
- groupProperties?: Record<string, Record<string, string>>;
46
- onlyEvaluateLocally?: boolean;
47
- sendFeatureFlagEvents?: boolean;
48
- disableGeoip?: boolean;
49
- }) => Promise<boolean | undefined>;
50
- intercom: IntercomAPI;
51
- log: import("../log").WorkerLog;
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
+ log: WorkerLog;
60
+ june?: Analytics;
61
+ intercom?: ReturnType<typeof useIntercom>;
62
+ posthog?: PostHog;
52
63
  };
53
64
  export {};
@@ -1,77 +1,160 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useTracking = void 0;
3
+ exports.useTracking = exports.convertKeysToSnakeCase = exports.isTest = void 0;
4
4
  const analytics_node_1 = require("@june-so/analytics-node");
5
5
  const posthog_node_1 = require("posthog-node");
6
6
  const log_1 = require("../log");
7
7
  const intercom_1 = require("./intercom");
8
- const useTracking = ({ dataset, env, name }, isDev) => {
9
- const axiom = (0, log_1.useLog)({ name, dataset, env });
10
- const intercom = (0, intercom_1.useIntercom)(env.INTERCOM_TOKEN, axiom);
11
- const juneso = new analytics_node_1.Analytics(env.JUNESO_ANALYTICS_WRITE_KEY);
12
- const posthog = new posthog_node_1.PostHog(env.POSTHOG_API_KEY, {
13
- host: 'https://app.posthog.com',
14
- });
8
+ const E2E_TEST_USERS = {
9
+ 'e2e-invite-member@finalytic.io': '113d73d8-ee21-46b7-a12a-a74f632401ca',
10
+ 'e2e-invite-owner@finalytic.io': '917b7c4e-cb03-491c-9e94-d33a5bdeca05',
11
+ 'e2e-sign-up@finalytic.io': 'ec8e5572-74d0-4ae6-af73-bca8db9a27e9',
12
+ 'lars+checkly@finalytic.co': '9e7dfc88-503c-4222-9905-9116169d2203',
13
+ };
14
+ const E2E_TEST_TEAMS = {
15
+ 'VRP Automated Testing': 'c4fd21b4-8447-43a2-bdcb-f06a85a935ad',
16
+ test_company_xxxx: '8f21060e-afe6-410a-b2f4-00a3caa20786',
17
+ };
18
+ const isTest = (userId, groupId, userEmail) => Object.values(E2E_TEST_USERS).includes(userId) ||
19
+ (groupId && Object.values(E2E_TEST_TEAMS).includes(groupId)) ||
20
+ (userEmail && Object.keys(E2E_TEST_USERS).includes(userEmail));
21
+ exports.isTest = isTest;
22
+ const camelToSnakeCase = (str) => str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
23
+ // todo: move to utils
24
+ const convertKeysToSnakeCase = (obj) => {
25
+ const result = {};
26
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
27
+ for (const key in obj) {
28
+ if (hasOwnProperty.call(obj, key)) {
29
+ const snakeKey = camelToSnakeCase(key);
30
+ if (typeof obj[key] === 'object' &&
31
+ !Array.isArray(obj[key]) &&
32
+ obj[key] !== null) {
33
+ result[snakeKey] = (0, exports.convertKeysToSnakeCase)(obj[key]);
34
+ }
35
+ else {
36
+ result[snakeKey] = obj[key];
37
+ }
38
+ }
39
+ }
40
+ return result;
41
+ };
42
+ exports.convertKeysToSnakeCase = convertKeysToSnakeCase;
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('track', props),
54
+ identify: async (props) => console.log('identify', props),
55
+ group: async (props) => console.log('group', props),
56
+ shutdown: () => Promise.resolve(),
57
+ log,
58
+ };
59
+ }
60
+ const log = (0, log_1.useLog)({ name, dataset, env });
61
+ const intercom = env.INTERCOM_TOKEN
62
+ ? (0, intercom_1.useIntercom)(env.INTERCOM_TOKEN, log)
63
+ : undefined;
64
+ const june = env.JUNESO_TOKEN ? new analytics_node_1.Analytics(env.JUNESO_TOKEN) : undefined;
65
+ const posthog = env.POSTHOG_TOKEN
66
+ ? new posthog_node_1.PostHog(env.POSTHOG_TOKEN, {
67
+ host: 'https://app.posthog.com',
68
+ })
69
+ : undefined;
15
70
  return {
16
71
  // General
17
- track: async ({ userId, groupId, event, properties, }) => {
18
- if (isDev)
72
+ track: async ({ userId, anonymousId, groupId, event, properties, }) => {
73
+ if (isDev || (0, exports.isTest)(userId || anonymousId || '', groupId))
19
74
  return;
20
- posthog.capture({
21
- distinctId: userId,
22
- event,
23
- groups: {
24
- tenant: groupId,
25
- },
26
- properties,
27
- });
28
- juneso.track({
29
- userId,
30
- event,
31
- properties,
32
- context: { groupId },
33
- });
34
- try {
35
- await intercom.trackEvent({
36
- user_id: userId,
37
- event_name: event,
38
- metadata: properties,
75
+ if (anonymousId)
76
+ june?.track({
77
+ anonymousId,
78
+ event,
79
+ properties,
80
+ context: { groupId },
81
+ }, (err) => {
82
+ if (err)
83
+ console.error(err);
39
84
  });
40
- }
41
- catch (error) {
42
- console.error(error);
85
+ else if (userId) {
86
+ posthog?.capture({
87
+ distinctId: userId,
88
+ event,
89
+ groups: {
90
+ tenant: groupId,
91
+ },
92
+ properties: (0, exports.convertKeysToSnakeCase)(properties),
93
+ });
94
+ june?.track({
95
+ userId,
96
+ event,
97
+ properties,
98
+ context: { groupId },
99
+ }, (err) => {
100
+ if (err)
101
+ console.error(err);
102
+ });
103
+ try {
104
+ await intercom?.trackEvent({
105
+ user_id: userId,
106
+ event_name: event,
107
+ metadata: properties,
108
+ });
109
+ }
110
+ catch (error) {
111
+ console.error(error);
112
+ }
43
113
  }
44
114
  },
45
- identify: ({ traits, userId, disableGeoip, }) => {
46
- posthog.identify({
115
+ identify: async ({ traits, userId, disableGeoip, timestamp, }) => {
116
+ if (isDev || (0, exports.isTest)(userId))
117
+ return;
118
+ posthog?.identify({
47
119
  distinctId: userId,
48
- properties: traits,
120
+ properties: (0, exports.convertKeysToSnakeCase)(traits),
49
121
  disableGeoip,
50
122
  });
51
- juneso.identify({
123
+ june?.identify({
52
124
  userId,
53
125
  traits,
126
+ timestamp,
127
+ }, (err) => {
128
+ if (err)
129
+ console.error(err);
54
130
  });
55
131
  },
56
- group: ({ traits, groupId, userId, anonymousId, }) => {
57
- posthog.groupIdentify({
132
+ group: async ({ traits, groupId, userId, anonymousId, timestamp, }) => {
133
+ if (isDev || (0, exports.isTest)(userId || anonymousId || '', groupId))
134
+ return;
135
+ posthog?.groupIdentify({
58
136
  groupKey: groupId,
59
137
  groupType: 'tenant',
60
- properties: traits,
138
+ properties: (0, exports.convertKeysToSnakeCase)(traits),
61
139
  distinctId: userId,
62
140
  });
63
141
  if (anonymousId)
64
- juneso.group({ anonymousId, groupId, traits });
142
+ june?.group({ anonymousId, groupId, traits, timestamp }, (err) => {
143
+ if (err)
144
+ console.error(err);
145
+ });
65
146
  else if (userId)
66
- juneso.group({ userId, groupId, traits });
147
+ june?.group({ userId, groupId, traits, timestamp }, (err) => {
148
+ if (err)
149
+ console.error(err);
150
+ });
67
151
  },
68
- shutdown: async () => Promise.all([posthog.shutdown(), juneso.closeAndFlush(), axiom.flush()]),
69
- // Posthog specific
70
- alias: posthog.alias,
71
- isFeatureEnabled: posthog.isFeatureEnabled,
72
- // export intercom and axiom
152
+ shutdown: async () => Promise.all([posthog?.shutdown(), june?.closeAndFlush(), log?.flush()]),
153
+ // export dependencies
73
154
  intercom,
74
- log: axiom,
155
+ posthog,
156
+ june,
157
+ log,
75
158
  };
76
159
  };
77
160
  exports.useTracking = useTracking;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"src/","sources":["tracking/index.ts"],"names":[],"mappings":";;;AAAA,4DAAoD;AACpD,+CAAuC;AACvC,gCAAgC;AAChC,yCAA2D;AAuBpD,MAAM,WAAW,GAAG,CACzB,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAiB,EACrC,KAAe,EACf,EAAE;IACF,MAAM,KAAK,GAAG,IAAA,YAAM,EAAC,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;IAC7C,MAAM,QAAQ,GAAG,IAAA,sBAAW,EAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IACxD,MAAM,MAAM,GAAG,IAAI,0BAAS,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC;IAC7D,MAAM,OAAO,GAAG,IAAI,sBAAO,CAAC,GAAG,CAAC,eAAe,EAAE;QAC/C,IAAI,EAAE,yBAAyB;KAChC,CAAC,CAAC;IAEH,OAAO;QACL,UAAU;QACV,KAAK,EAAE,KAAK,EAAE,EACZ,MAAM,EACN,OAAO,EACP,KAAK,EACL,UAAU,GAMX,EAAE,EAAE;YACH,IAAI,KAAK;gBAAE,OAAO;YAElB,OAAO,CAAC,OAAO,CAAC;gBACd,UAAU,EAAE,MAAM;gBAClB,KAAK;gBACL,MAAM,EAAE;oBACN,MAAM,EAAE,OAAO;iBAChB;gBACD,UAAU;aACX,CAAC,CAAC;YACH,MAAM,CAAC,KAAK,CAAC;gBACX,MAAM;gBACN,KAAK;gBACL,UAAU;gBACV,OAAO,EAAE,EAAE,OAAO,EAAE;aACrB,CAAC,CAAC;YAEH,IAAI,CAAC;gBACH,MAAM,QAAQ,CAAC,UAAU,CAAC;oBACxB,OAAO,EAAE,MAAM;oBACf,UAAU,EAAE,KAAK;oBACjB,QAAQ,EAAE,UAAU;iBACrB,CAAC,CAAC;YACL,CA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AK,IAAI,IAAA,cAAM,EAAC,MAAM,IAAI,WAAW,IAAI,EAAE,EAAE,OAAO,CAAC;gBAAE,OAAO;YAElE,OAAO,EAAE,aAAa,CAAC;gBACrB,QAAQ,EAAE,OAAO;gBACjB,SAAS,EAAE,QAAQ;gBACnB,UAAU,EAAE,IAAA,8BAAsB,EAAC,MAAM,CAAC;gBAC1C,UAAU,EAAE,MAAM;aACnB,CAAC,CAAC;YACH,IAAI,WAAW;gBACb,IAAI,EAAE,KAAK,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,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,sBAAsB;QACtB,QAAQ;QACR,OAAO;QACP,IAAI;QACJ,GAAG;KACJ,CAAC;AACJ,CAAC,CAAC;AAzJW,QAAA,WAAW,eAyJtB"}
@@ -1,15 +1,49 @@
1
- import { type IntercomAPI, useIntercom } from './intercom';
1
+ import { Analytics } from '@june-so/analytics-node';
2
+ import { PostHog } from 'posthog-node';
3
+ import { type WorkerLog } from '../log';
4
+ import { useIntercom } from './intercom';
5
+ export declare const isTest: (userId: string, groupId?: string, userEmail?: string) => boolean | "" | undefined;
6
+ export declare const convertKeysToSnakeCase: (obj?: Record<string, any>) => Record<string, any>;
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';
8
+ type TeamEvent = 'team_added' | 'team_info_updated' | 'team_deleted' | 'team_member_removed';
9
+ type ConnectionEvent = 'connection_created' | 'connection_reconnected' | 'connection_deleted' | 'connection_requested';
10
+ type OwnershipEvent = 'ownership_created' | 'ownership_updated' | 'ownership_deleted';
11
+ type OwnerEvent = 'owner_created' | 'owner_updated' | 'owner_deleted' | 'owner_tax_info_modal_opened' | 'owner_tax_info_modal_submitted';
12
+ type SetupEvent = 'setup_classes_set' | 'setup_accounting_version_completed' | 'setup_accounting_config_completed' | 'setup_owner_imported' | 'setup_listing_imported';
13
+ type AutomationEvent = 'automation_created' | 'automation_updated' | 'automation_deleted';
14
+ type TaxStatementEvent = 'tax_statement_preview_opened' | 'tax_statement_downloaded';
15
+ type GLOwnerStatementEvent = 'gl_owner_statement_preview_opened' | 'gl_owner_statement_downloaded';
16
+ type HyperlineEvent = 'hyperline_invoice_ready' | 'hyperline_invoice_settled' | 'hyperline_trial_started' | 'hyperline_trial_ended' | 'hyperline_subscription_activated' | 'hyperline_subscription_cancelled' | 'hyperline_subscription_created' | 'hyperline_subscription_errored' | 'hyperline_subscription_paused' | 'hyperline_subscription_voided' | 'hyperline_subscription_charged' | 'hyperline_customer_created' | 'hyperline_customer_updated' | 'hyperline_payment_method_created' | 'hyperline_payment_method_errored' | 'hyperline_payment_method_deleted';
17
+ type UserEvent = 'update_refresh_accepted' | 'owner_statement_opened';
18
+ type TestEvent = 'test_event' | 'test_error';
19
+ export type TrackingEvent = AuthEvent | TeamEvent | ConnectionEvent | OwnershipEvent | OwnerEvent | SetupEvent | AutomationEvent | TaxStatementEvent | GLOwnerStatementEvent | HyperlineEvent | UserEvent | TestEvent;
2
20
  export type TrackingProps = {
3
21
  name: string;
4
22
  dataset: string;
5
23
  env: {
6
- INTERCOM_TOKEN: string;
7
- JUNESO_ANALYTICS_WRITE_KEY: string;
8
- POSTHOG_API_KEY: string;
24
+ INTERCOM_TOKEN?: string;
25
+ JUNESO_TOKEN?: string;
26
+ POSTHOG_TOKEN?: string;
9
27
  };
28
+ debugging?: boolean;
10
29
  };
11
30
  export type Tracking = ReturnType<typeof useTracking>;
12
- export type Intercom = ReturnType<typeof useIntercom>;
31
+ type TrackProps = OptionalUser<{
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
+ }>;
13
47
  type OptionalUser<T> = T & ({
14
48
  userId: string;
15
49
  anonymousId?: string;
@@ -17,37 +51,14 @@ type OptionalUser<T> = T & ({
17
51
  userId?: string;
18
52
  anonymousId: string;
19
53
  });
20
- export declare const useTracking: ({ dataset, env, name }: TrackingProps, isDev?: boolean) => {
21
- track: ({ userId, groupId, event, properties, }: {
22
- userId: string;
23
- groupId: string;
24
- event: string;
25
- properties: Record<string, any>;
26
- }) => Promise<void>;
27
- identify: ({ traits, userId, disableGeoip, }: {
28
- userId: string;
29
- traits: Record<string, any>;
30
- disableGeoip?: boolean;
31
- }) => void;
32
- group: ({ traits, groupId, userId, anonymousId, }: OptionalUser<{
33
- groupId: string;
34
- traits?: Record<string, any>;
35
- }>) => void;
36
- shutdown: () => Promise<[void, void, void]>;
37
- alias: (data: {
38
- distinctId: string;
39
- alias: string;
40
- disableGeoip?: boolean;
41
- }) => void;
42
- isFeatureEnabled: (key: string, distinctId: string, options?: {
43
- groups?: Record<string, string>;
44
- personProperties?: Record<string, string>;
45
- groupProperties?: Record<string, Record<string, string>>;
46
- onlyEvaluateLocally?: boolean;
47
- sendFeatureFlagEvents?: boolean;
48
- disableGeoip?: boolean;
49
- }) => Promise<boolean | undefined>;
50
- intercom: IntercomAPI;
51
- log: import("../log").WorkerLog;
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
+ log: WorkerLog;
60
+ june?: Analytics;
61
+ intercom?: ReturnType<typeof useIntercom>;
62
+ posthog?: PostHog;
52
63
  };
53
64
  export {};
@@ -2,73 +2,154 @@ import { Analytics } from '@june-so/analytics-node';
2
2
  import { PostHog } from 'posthog-node';
3
3
  import { useLog } from '../log';
4
4
  import { useIntercom } from './intercom';
5
- export const useTracking = ({ dataset, env, name }, isDev) => {
6
- const axiom = useLog({ name, dataset, env });
7
- const intercom = useIntercom(env.INTERCOM_TOKEN, axiom);
8
- const juneso = new Analytics(env.JUNESO_ANALYTICS_WRITE_KEY);
9
- const posthog = new PostHog(env.POSTHOG_API_KEY, {
10
- host: 'https://app.posthog.com',
11
- });
5
+ const E2E_TEST_USERS = {
6
+ 'e2e-invite-member@finalytic.io': '113d73d8-ee21-46b7-a12a-a74f632401ca',
7
+ 'e2e-invite-owner@finalytic.io': '917b7c4e-cb03-491c-9e94-d33a5bdeca05',
8
+ 'e2e-sign-up@finalytic.io': 'ec8e5572-74d0-4ae6-af73-bca8db9a27e9',
9
+ 'lars+checkly@finalytic.co': '9e7dfc88-503c-4222-9905-9116169d2203',
10
+ };
11
+ const E2E_TEST_TEAMS = {
12
+ 'VRP Automated Testing': 'c4fd21b4-8447-43a2-bdcb-f06a85a935ad',
13
+ test_company_xxxx: '8f21060e-afe6-410a-b2f4-00a3caa20786',
14
+ };
15
+ export const isTest = (userId, groupId, userEmail) => Object.values(E2E_TEST_USERS).includes(userId) ||
16
+ (groupId && Object.values(E2E_TEST_TEAMS).includes(groupId)) ||
17
+ (userEmail && Object.keys(E2E_TEST_USERS).includes(userEmail));
18
+ const camelToSnakeCase = (str) => str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
19
+ // todo: move to utils
20
+ export const convertKeysToSnakeCase = (obj) => {
21
+ const result = {};
22
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
23
+ for (const key in obj) {
24
+ if (hasOwnProperty.call(obj, key)) {
25
+ const snakeKey = camelToSnakeCase(key);
26
+ if (typeof obj[key] === 'object' &&
27
+ !Array.isArray(obj[key]) &&
28
+ obj[key] !== null) {
29
+ result[snakeKey] = convertKeysToSnakeCase(obj[key]);
30
+ }
31
+ else {
32
+ result[snakeKey] = obj[key];
33
+ }
34
+ }
35
+ }
36
+ return result;
37
+ };
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('track', props),
49
+ identify: async (props) => console.log('identify', props),
50
+ group: async (props) => console.log('group', props),
51
+ shutdown: () => Promise.resolve(),
52
+ log,
53
+ };
54
+ }
55
+ const log = useLog({ name, dataset, env });
56
+ const intercom = env.INTERCOM_TOKEN
57
+ ? useIntercom(env.INTERCOM_TOKEN, log)
58
+ : undefined;
59
+ const june = env.JUNESO_TOKEN ? new Analytics(env.JUNESO_TOKEN) : undefined;
60
+ const posthog = env.POSTHOG_TOKEN
61
+ ? new PostHog(env.POSTHOG_TOKEN, {
62
+ host: 'https://app.posthog.com',
63
+ })
64
+ : undefined;
12
65
  return {
13
66
  // General
14
- track: async ({ userId, groupId, event, properties, }) => {
15
- if (isDev)
67
+ track: async ({ userId, anonymousId, groupId, event, properties, }) => {
68
+ if (isDev || isTest(userId || anonymousId || '', groupId))
16
69
  return;
17
- posthog.capture({
18
- distinctId: userId,
19
- event,
20
- groups: {
21
- tenant: groupId,
22
- },
23
- properties,
24
- });
25
- juneso.track({
26
- userId,
27
- event,
28
- properties,
29
- context: { groupId },
30
- });
31
- try {
32
- await intercom.trackEvent({
33
- user_id: userId,
34
- event_name: event,
35
- metadata: properties,
70
+ if (anonymousId)
71
+ june?.track({
72
+ anonymousId,
73
+ event,
74
+ properties,
75
+ context: { groupId },
76
+ }, (err) => {
77
+ if (err)
78
+ console.error(err);
36
79
  });
37
- }
38
- catch (error) {
39
- console.error(error);
80
+ else if (userId) {
81
+ posthog?.capture({
82
+ distinctId: userId,
83
+ event,
84
+ groups: {
85
+ tenant: groupId,
86
+ },
87
+ properties: convertKeysToSnakeCase(properties),
88
+ });
89
+ june?.track({
90
+ userId,
91
+ event,
92
+ properties,
93
+ context: { groupId },
94
+ }, (err) => {
95
+ if (err)
96
+ console.error(err);
97
+ });
98
+ try {
99
+ await intercom?.trackEvent({
100
+ user_id: userId,
101
+ event_name: event,
102
+ metadata: properties,
103
+ });
104
+ }
105
+ catch (error) {
106
+ console.error(error);
107
+ }
40
108
  }
41
109
  },
42
- identify: ({ traits, userId, disableGeoip, }) => {
43
- posthog.identify({
110
+ identify: async ({ traits, userId, disableGeoip, timestamp, }) => {
111
+ if (isDev || isTest(userId))
112
+ return;
113
+ posthog?.identify({
44
114
  distinctId: userId,
45
- properties: traits,
115
+ properties: convertKeysToSnakeCase(traits),
46
116
  disableGeoip,
47
117
  });
48
- juneso.identify({
118
+ june?.identify({
49
119
  userId,
50
120
  traits,
121
+ timestamp,
122
+ }, (err) => {
123
+ if (err)
124
+ console.error(err);
51
125
  });
52
126
  },
53
- group: ({ traits, groupId, userId, anonymousId, }) => {
54
- posthog.groupIdentify({
127
+ group: async ({ traits, groupId, userId, anonymousId, timestamp, }) => {
128
+ if (isDev || isTest(userId || anonymousId || '', groupId))
129
+ return;
130
+ posthog?.groupIdentify({
55
131
  groupKey: groupId,
56
132
  groupType: 'tenant',
57
- properties: traits,
133
+ properties: convertKeysToSnakeCase(traits),
58
134
  distinctId: userId,
59
135
  });
60
136
  if (anonymousId)
61
- juneso.group({ anonymousId, groupId, traits });
137
+ june?.group({ anonymousId, groupId, traits, timestamp }, (err) => {
138
+ if (err)
139
+ console.error(err);
140
+ });
62
141
  else if (userId)
63
- juneso.group({ userId, groupId, traits });
142
+ june?.group({ userId, groupId, traits, timestamp }, (err) => {
143
+ if (err)
144
+ console.error(err);
145
+ });
64
146
  },
65
- shutdown: async () => Promise.all([posthog.shutdown(), juneso.closeAndFlush(), axiom.flush()]),
66
- // Posthog specific
67
- alias: posthog.alias,
68
- isFeatureEnabled: posthog.isFeatureEnabled,
69
- // export intercom and axiom
147
+ shutdown: async () => Promise.all([posthog?.shutdown(), june?.closeAndFlush(), log?.flush()]),
148
+ // export dependencies
70
149
  intercom,
71
- log: axiom,
150
+ posthog,
151
+ june,
152
+ log,
72
153
  };
73
154
  };
74
155
  //# 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,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAoB,WAAW,EAAE,MAAM,YAAY,CAAC;AAuB3D,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAiB,EACrC,KAAe,EACf,EAAE;IACF,MAAM,KAAK,GAAG,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;IAC7C,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IACxD,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC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AC,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,sBAAsB;QACtB,QAAQ;QACR,OAAO;QACP,IAAI;QACJ,GAAG;KACJ,CAAC;AACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vrplatform/log",
3
- "version": "2.0.0-alpha.2",
3
+ "version": "2.0.0-alpha.21",
4
4
  "main": "src/index.ts",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -1,20 +1,171 @@
1
1
  import { Analytics } from '@june-so/analytics-node';
2
2
  import { PostHog } from 'posthog-node';
3
- import { useLog } from '../log';
4
- import { type IntercomAPI, useIntercom } from './intercom';
3
+ import { type WorkerLog, useLog } from '../log';
4
+ import { useIntercom } from './intercom';
5
+
6
+ const E2E_TEST_USERS = {
7
+ 'e2e-invite-member@finalytic.io': '113d73d8-ee21-46b7-a12a-a74f632401ca',
8
+ 'e2e-invite-owner@finalytic.io': '917b7c4e-cb03-491c-9e94-d33a5bdeca05',
9
+ 'e2e-sign-up@finalytic.io': 'ec8e5572-74d0-4ae6-af73-bca8db9a27e9',
10
+ 'lars+checkly@finalytic.co': '9e7dfc88-503c-4222-9905-9116169d2203',
11
+ };
12
+
13
+ const E2E_TEST_TEAMS = {
14
+ 'VRP Automated Testing': 'c4fd21b4-8447-43a2-bdcb-f06a85a935ad',
15
+ test_company_xxxx: '8f21060e-afe6-410a-b2f4-00a3caa20786',
16
+ };
17
+
18
+ export const isTest = (userId: string, groupId?: string, userEmail?: string) =>
19
+ Object.values(E2E_TEST_USERS).includes(userId) ||
20
+ (groupId && Object.values(E2E_TEST_TEAMS).includes(groupId)) ||
21
+ (userEmail && Object.keys(E2E_TEST_USERS).includes(userEmail));
22
+
23
+ const camelToSnakeCase = (str: string) =>
24
+ str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
25
+
26
+ // todo: move to utils
27
+ export const convertKeysToSnakeCase = (obj?: Record<string, any>) => {
28
+ const result: Record<string, any> = {};
29
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
30
+
31
+ for (const key in obj) {
32
+ if (hasOwnProperty.call(obj, key)) {
33
+ const snakeKey = camelToSnakeCase(key);
34
+ if (
35
+ typeof obj[key] === 'object' &&
36
+ !Array.isArray(obj[key]) &&
37
+ obj[key] !== null
38
+ ) {
39
+ result[snakeKey] = convertKeysToSnakeCase(obj[key]);
40
+ } else {
41
+ result[snakeKey] = obj[key];
42
+ }
43
+ }
44
+ }
45
+
46
+ return result;
47
+ };
48
+
49
+ type AuthEvent =
50
+ | 'account_signin_completed'
51
+ | 'account_signup_code_requested'
52
+ | 'account_signup_code_completed'
53
+ | 'account_signup_code_failed'
54
+ | 'account_signup_completed'
55
+ | 'account_password_reset_requested'
56
+ | 'account_password_reset_failed'
57
+ | 'account_password_reset_completed'
58
+ | 'account_invitation_accepted'
59
+ | 'account_signed_out';
60
+
61
+ type TeamEvent =
62
+ | 'team_added'
63
+ | 'team_info_updated'
64
+ | 'team_deleted'
65
+ | 'team_member_removed';
66
+
67
+ type ConnectionEvent =
68
+ | 'connection_created'
69
+ | 'connection_reconnected'
70
+ | 'connection_deleted'
71
+ | 'connection_requested';
72
+
73
+ type OwnershipEvent =
74
+ | 'ownership_created'
75
+ | 'ownership_updated'
76
+ | 'ownership_deleted';
77
+
78
+ type OwnerEvent =
79
+ | 'owner_created'
80
+ | 'owner_updated'
81
+ | 'owner_deleted'
82
+ | 'owner_tax_info_modal_opened'
83
+ | 'owner_tax_info_modal_submitted';
84
+
85
+ type SetupEvent =
86
+ | 'setup_classes_set'
87
+ | 'setup_accounting_version_completed'
88
+ | 'setup_accounting_config_completed'
89
+ | 'setup_owner_imported'
90
+ | 'setup_listing_imported';
91
+
92
+ type AutomationEvent =
93
+ | 'automation_created'
94
+ | 'automation_updated'
95
+ | 'automation_deleted';
96
+
97
+ type TaxStatementEvent =
98
+ | 'tax_statement_preview_opened'
99
+ | 'tax_statement_downloaded';
100
+
101
+ type GLOwnerStatementEvent =
102
+ | 'gl_owner_statement_preview_opened'
103
+ | 'gl_owner_statement_downloaded';
104
+
105
+ type HyperlineEvent =
106
+ | 'hyperline_invoice_ready'
107
+ | 'hyperline_invoice_settled'
108
+ | 'hyperline_trial_started'
109
+ | 'hyperline_trial_ended'
110
+ | 'hyperline_subscription_activated'
111
+ | 'hyperline_subscription_cancelled'
112
+ | 'hyperline_subscription_created'
113
+ | 'hyperline_subscription_errored'
114
+ | 'hyperline_subscription_paused'
115
+ | 'hyperline_subscription_voided'
116
+ | 'hyperline_subscription_charged'
117
+ | 'hyperline_customer_created'
118
+ | 'hyperline_customer_updated'
119
+ | 'hyperline_payment_method_created'
120
+ | 'hyperline_payment_method_errored'
121
+ | 'hyperline_payment_method_deleted';
122
+
123
+ type UserEvent = 'update_refresh_accepted' | 'owner_statement_opened';
124
+
125
+ type TestEvent = 'test_event' | 'test_error';
126
+
127
+ export type TrackingEvent =
128
+ | AuthEvent
129
+ | TeamEvent
130
+ | ConnectionEvent
131
+ | OwnershipEvent
132
+ | OwnerEvent
133
+ | SetupEvent
134
+ | AutomationEvent
135
+ | TaxStatementEvent
136
+ | GLOwnerStatementEvent
137
+ | HyperlineEvent
138
+ | UserEvent
139
+ | TestEvent;
5
140
 
6
141
  export type TrackingProps = {
7
142
  name: string;
8
143
  dataset: string;
9
144
  env: {
10
- INTERCOM_TOKEN: string;
11
- JUNESO_ANALYTICS_WRITE_KEY: string;
12
- POSTHOG_API_KEY: string;
145
+ INTERCOM_TOKEN?: string;
146
+ JUNESO_TOKEN?: string;
147
+ POSTHOG_TOKEN?: string;
13
148
  };
149
+ debugging?: boolean;
14
150
  };
15
151
  export type Tracking = ReturnType<typeof useTracking>;
16
- export type Intercom = ReturnType<typeof useIntercom>;
17
152
 
153
+ type TrackProps = OptionalUser<{
154
+ groupId: string;
155
+ event: TrackingEvent;
156
+ properties: Record<string, string | number | boolean | null | undefined>;
157
+ }>;
158
+ type IdentifyProps = {
159
+ userId: string;
160
+ traits: Record<string, any>;
161
+ disableGeoip?: boolean;
162
+ timestamp?: Date;
163
+ };
164
+ type GroupProps = OptionalUser<{
165
+ groupId: string;
166
+ traits?: Record<string, any>;
167
+ timestamp?: Date;
168
+ }>;
18
169
  type OptionalUser<T> = T &
19
170
  (
20
171
  | { userId: string; anonymousId?: string }
@@ -25,100 +176,156 @@ type OptionalUser<T> = T &
25
176
  );
26
177
 
27
178
  export const useTracking = (
28
- { dataset, env, name }: TrackingProps,
179
+ { dataset, env, name, debugging }: TrackingProps,
29
180
  isDev?: boolean
30
- ) => {
31
- const axiom = useLog({ name, dataset, env });
32
- const intercom = useIntercom(env.INTERCOM_TOKEN, axiom);
33
- const juneso = new Analytics(env.JUNESO_ANALYTICS_WRITE_KEY);
34
- const posthog = new PostHog(env.POSTHOG_API_KEY, {
35
- host: 'https://app.posthog.com',
36
- });
181
+ ): {
182
+ track: (props: TrackProps) => Promise<void>;
183
+ identify: (props: IdentifyProps) => Promise<void>;
184
+ group: (props: GroupProps) => Promise<void>;
185
+ shutdown: () => Promise<any>;
186
+ log: WorkerLog;
187
+ june?: Analytics;
188
+ intercom?: ReturnType<typeof useIntercom>;
189
+ posthog?: PostHog;
190
+ } => {
191
+ if (debugging) {
192
+ const log = {
193
+ ...console,
194
+ child: () => log,
195
+ addContext: (data: Record<string, any>) => console.log(data),
196
+ flush: () => Promise.resolve(),
197
+ respond: () => Promise.resolve(new Response()),
198
+ };
199
+
200
+ return {
201
+ track: async (props: TrackProps) => console.log('track', props),
202
+ identify: async (props: IdentifyProps) => console.log('identify', props),
203
+ group: async (props: GroupProps) => console.log('group', props),
204
+ shutdown: () => Promise.resolve(),
205
+ log,
206
+ };
207
+ }
208
+
209
+ const log = useLog({ name, dataset, env });
210
+ const intercom = env.INTERCOM_TOKEN
211
+ ? useIntercom(env.INTERCOM_TOKEN, log)
212
+ : undefined;
213
+ const june = env.JUNESO_TOKEN ? new Analytics(env.JUNESO_TOKEN) : undefined;
214
+ const posthog = env.POSTHOG_TOKEN
215
+ ? new PostHog(env.POSTHOG_TOKEN, {
216
+ host: 'https://app.posthog.com',
217
+ })
218
+ : undefined;
37
219
 
38
220
  return {
39
221
  // General
40
222
  track: async ({
41
223
  userId,
224
+ anonymousId,
42
225
  groupId,
43
226
  event,
44
227
  properties,
45
- }: {
46
- userId: string;
47
- groupId: string;
48
- event: string;
49
- properties: Record<string, any>;
50
- }) => {
51
- if (isDev) return;
52
-
53
- posthog.capture({
54
- distinctId: userId,
55
- event,
56
- groups: {
57
- tenant: groupId,
58
- },
59
- properties,
60
- });
61
- juneso.track({
62
- userId,
63
- event,
64
- properties,
65
- context: { groupId },
66
- });
228
+ }: TrackProps) => {
229
+ if (isDev || isTest(userId || anonymousId || '', groupId)) return;
67
230
 
68
- try {
69
- await intercom.trackEvent({
70
- user_id: userId,
71
- event_name: event,
72
- metadata: properties,
231
+ if (anonymousId)
232
+ june?.track(
233
+ {
234
+ anonymousId,
235
+ event,
236
+ properties,
237
+ context: { groupId },
238
+ },
239
+ (err) => {
240
+ if (err) console.error(err);
241
+ }
242
+ );
243
+ else if (userId) {
244
+ posthog?.capture({
245
+ distinctId: userId,
246
+ event,
247
+ groups: {
248
+ tenant: groupId,
249
+ },
250
+ properties: convertKeysToSnakeCase(properties),
73
251
  });
74
- } catch (error: any) {
75
- console.error(error);
252
+ june?.track(
253
+ {
254
+ userId,
255
+ event,
256
+ properties,
257
+ context: { groupId },
258
+ },
259
+ (err) => {
260
+ if (err) console.error(err);
261
+ }
262
+ );
263
+
264
+ try {
265
+ await intercom?.trackEvent({
266
+ user_id: userId,
267
+ event_name: event,
268
+ metadata: properties,
269
+ });
270
+ } catch (error: any) {
271
+ console.error(error);
272
+ }
76
273
  }
77
274
  },
78
- identify: ({
275
+ identify: async ({
79
276
  traits,
80
277
  userId,
81
278
  disableGeoip,
82
- }: {
83
- userId: string;
84
- traits: Record<string, any>;
85
- disableGeoip?: boolean;
86
- }) => {
87
- posthog.identify({
279
+ timestamp,
280
+ }: IdentifyProps) => {
281
+ if (isDev || isTest(userId)) return;
282
+
283
+ posthog?.identify({
88
284
  distinctId: userId,
89
- properties: traits,
285
+ properties: convertKeysToSnakeCase(traits),
90
286
  disableGeoip,
91
287
  });
92
- juneso.identify({
93
- userId,
94
- traits,
95
- });
288
+ june?.identify(
289
+ {
290
+ userId,
291
+ traits,
292
+ timestamp,
293
+ },
294
+ (err) => {
295
+ if (err) console.error(err);
296
+ }
297
+ );
96
298
  },
97
- group: ({
299
+ group: async ({
98
300
  traits,
99
301
  groupId,
100
302
  userId,
101
303
  anonymousId,
102
- }: OptionalUser<{
103
- groupId: string;
104
- traits?: Record<string, any>;
105
- }>) => {
106
- posthog.groupIdentify({
304
+ timestamp,
305
+ }: GroupProps) => {
306
+ if (isDev || isTest(userId || anonymousId || '', groupId)) return;
307
+
308
+ posthog?.groupIdentify({
107
309
  groupKey: groupId,
108
310
  groupType: 'tenant',
109
- properties: traits,
311
+ properties: convertKeysToSnakeCase(traits),
110
312
  distinctId: userId,
111
313
  });
112
- if (anonymousId) juneso.group({ anonymousId, groupId, traits });
113
- else if (userId) juneso.group({ userId, groupId, traits });
314
+ if (anonymousId)
315
+ june?.group({ anonymousId, groupId, traits, timestamp }, (err) => {
316
+ if (err) console.error(err);
317
+ });
318
+ else if (userId)
319
+ june?.group({ userId, groupId, traits, timestamp }, (err) => {
320
+ if (err) console.error(err);
321
+ });
114
322
  },
115
323
  shutdown: async () =>
116
- Promise.all([posthog.shutdown(), juneso.closeAndFlush(), axiom.flush()]),
117
- // Posthog specific
118
- alias: posthog.alias,
119
- isFeatureEnabled: posthog.isFeatureEnabled,
120
- // export intercom and axiom
324
+ Promise.all([posthog?.shutdown(), june?.closeAndFlush(), log?.flush()]),
325
+ // export dependencies
121
326
  intercom,
122
- log: axiom,
327
+ posthog,
328
+ june,
329
+ log,
123
330
  };
124
331
  };