@vrplatform/log 2.0.0-alpha.29 → 2.0.0-alpha.30
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 +2 -2
- package/build/main/tracking/index.js +75 -64
- package/build/main/tracking/index.js.map +1 -1
- package/build/main/utils/convertKeysToSnakeCase.d.ts +1 -0
- package/build/main/utils/convertKeysToSnakeCase.js +23 -0
- package/build/main/utils/convertKeysToSnakeCase.js.map +1 -0
- package/build/main/utils/convertValuesToString.d.ts +5 -0
- package/build/main/utils/convertValuesToString.js +11 -0
- package/build/main/utils/convertValuesToString.js.map +1 -0
- package/build/main/utils/index.d.ts +3 -0
- package/build/main/utils/index.js +20 -0
- package/build/main/utils/index.js.map +1 -0
- package/build/main/utils/isTest.d.ts +1 -0
- package/build/main/utils/isTest.js +18 -0
- package/build/main/utils/isTest.js.map +1 -0
- package/build/module/tracking/index.d.ts +2 -2
- package/build/module/tracking/index.js +66 -53
- package/build/module/tracking/index.js.map +1 -1
- package/build/module/utils/convertKeysToSnakeCase.d.ts +1 -0
- package/build/module/utils/convertKeysToSnakeCase.js +19 -0
- package/build/module/utils/convertKeysToSnakeCase.js.map +1 -0
- package/build/module/utils/convertValuesToString.d.ts +5 -0
- package/build/module/utils/convertValuesToString.js +8 -0
- package/build/module/utils/convertValuesToString.js.map +1 -0
- package/build/module/utils/index.d.ts +3 -0
- package/build/module/utils/index.js +4 -0
- package/build/module/utils/index.js.map +1 -0
- package/build/module/utils/isTest.d.ts +1 -0
- package/build/module/utils/isTest.js +14 -0
- package/build/module/utils/isTest.js.map +1 -0
- package/package.json +1 -1
- package/src/tracking/index.ts +90 -59
- package/src/utils/convertKeysToSnakeCase.ts +21 -0
- package/src/utils/convertValuesToString.ts +10 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/isTest.ts +16 -0
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { Analytics } from '@june-so/analytics-node';
|
|
2
|
+
import { IntercomClient } from 'intercom-client';
|
|
2
3
|
import { PostHog } from 'posthog-node';
|
|
3
4
|
import { type LogBindings, type WorkerLog } from '../log';
|
|
4
5
|
import { useIntercom } from './intercom';
|
|
5
6
|
import type { TrackingEvent } from './types';
|
|
6
7
|
export * from './types';
|
|
7
|
-
export declare const isTest: (userId: string, groupId?: string, userEmail?: string) => boolean | "" | undefined;
|
|
8
|
-
export declare const convertKeysToSnakeCase: (obj?: Record<string, any>) => Record<string, any>;
|
|
9
8
|
export type TrackingProps = {
|
|
10
9
|
name: string;
|
|
11
10
|
dataset: string;
|
|
@@ -48,6 +47,7 @@ export declare const useTracking: ({ dataset, env, name }: TrackingProps, isDev?
|
|
|
48
47
|
shutdown: () => Promise<any>;
|
|
49
48
|
log: WorkerLog;
|
|
50
49
|
june?: Analytics;
|
|
50
|
+
intercom?: IntercomClient;
|
|
51
51
|
_intercom?: ReturnType<typeof useIntercom>;
|
|
52
52
|
posthog?: PostHog;
|
|
53
53
|
};
|
|
@@ -14,49 +14,27 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.useTracking =
|
|
17
|
+
exports.useTracking = void 0;
|
|
18
18
|
const analytics_node_1 = require("@june-so/analytics-node");
|
|
19
|
+
const intercom_client_1 = require("intercom-client");
|
|
20
|
+
const Fetcher_1 = require("intercom-client/core/fetcher/Fetcher");
|
|
19
21
|
const posthog_node_1 = require("posthog-node");
|
|
20
22
|
const log_1 = require("../log");
|
|
23
|
+
const utils_1 = require("../utils");
|
|
24
|
+
const convertValuesToString_1 = require("../utils/convertValuesToString");
|
|
21
25
|
const intercom_1 = require("./intercom");
|
|
22
26
|
__exportStar(require("./types"), exports);
|
|
23
|
-
const E2E_TEST_USERS = {
|
|
24
|
-
'e2e-invite-member@finalytic.io': '113d73d8-ee21-46b7-a12a-a74f632401ca',
|
|
25
|
-
'e2e-invite-owner@finalytic.io': '917b7c4e-cb03-491c-9e94-d33a5bdeca05',
|
|
26
|
-
'e2e-sign-up@finalytic.io': 'ec8e5572-74d0-4ae6-af73-bca8db9a27e9',
|
|
27
|
-
'lars+checkly@finalytic.co': '9e7dfc88-503c-4222-9905-9116169d2203',
|
|
28
|
-
};
|
|
29
|
-
const E2E_TEST_TEAMS = {
|
|
30
|
-
'VRP Automated Testing': 'c4fd21b4-8447-43a2-bdcb-f06a85a935ad',
|
|
31
|
-
test_company_xxxx: '8f21060e-afe6-410a-b2f4-00a3caa20786',
|
|
32
|
-
};
|
|
33
|
-
const isTest = (userId, groupId, userEmail) => Object.values(E2E_TEST_USERS).includes(userId) ||
|
|
34
|
-
(groupId && Object.values(E2E_TEST_TEAMS).includes(groupId)) ||
|
|
35
|
-
(userEmail && Object.keys(E2E_TEST_USERS).includes(userEmail));
|
|
36
|
-
exports.isTest = isTest;
|
|
37
|
-
const camelToSnakeCase = (str) => str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
|
|
38
|
-
// todo: move to utils
|
|
39
|
-
const convertKeysToSnakeCase = (obj) => {
|
|
40
|
-
const result = {};
|
|
41
|
-
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
42
|
-
for (const key in obj) {
|
|
43
|
-
if (hasOwnProperty.call(obj, key)) {
|
|
44
|
-
const snakeKey = camelToSnakeCase(key);
|
|
45
|
-
if (typeof obj[key] === 'object' &&
|
|
46
|
-
!Array.isArray(obj[key]) &&
|
|
47
|
-
obj[key] !== null) {
|
|
48
|
-
result[snakeKey] = (0, exports.convertKeysToSnakeCase)(obj[key]);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
result[snakeKey] = obj[key];
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return result;
|
|
56
|
-
};
|
|
57
|
-
exports.convertKeysToSnakeCase = convertKeysToSnakeCase;
|
|
58
27
|
const useTracking = ({ dataset, env, name }, isDev) => {
|
|
59
28
|
const log = (0, log_1.useLog)({ name, dataset, env });
|
|
29
|
+
const intercom = env.INTERCOM_TOKEN
|
|
30
|
+
? new intercom_client_1.IntercomClient({
|
|
31
|
+
token: env.INTERCOM_TOKEN,
|
|
32
|
+
fetcher: (args) => (0, Fetcher_1.fetcher)({
|
|
33
|
+
...args,
|
|
34
|
+
headers: { ...args.headers, accept: 'application/json' },
|
|
35
|
+
}),
|
|
36
|
+
})
|
|
37
|
+
: undefined;
|
|
60
38
|
const _intercom = env.INTERCOM_TOKEN
|
|
61
39
|
? (0, intercom_1.useIntercom)(env.INTERCOM_TOKEN, log)
|
|
62
40
|
: undefined;
|
|
@@ -69,7 +47,7 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
69
47
|
return {
|
|
70
48
|
// General
|
|
71
49
|
track: async ({ userId, anonymousId, groupId, event, properties, timestamp, }) => {
|
|
72
|
-
if (isDev || (0,
|
|
50
|
+
if (isDev || (0, utils_1.isTest)(userId || anonymousId || '', groupId))
|
|
73
51
|
return;
|
|
74
52
|
if (anonymousId) {
|
|
75
53
|
posthog?.capture({
|
|
@@ -79,7 +57,7 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
79
57
|
tenant: groupId,
|
|
80
58
|
},
|
|
81
59
|
timestamp,
|
|
82
|
-
properties: (0,
|
|
60
|
+
properties: (0, utils_1.convertKeysToSnakeCase)(properties),
|
|
83
61
|
});
|
|
84
62
|
june?.track({
|
|
85
63
|
anonymousId,
|
|
@@ -101,7 +79,7 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
101
79
|
tenant: groupId,
|
|
102
80
|
},
|
|
103
81
|
timestamp,
|
|
104
|
-
properties: (0,
|
|
82
|
+
properties: (0, utils_1.convertKeysToSnakeCase)(properties),
|
|
105
83
|
});
|
|
106
84
|
june?.track({
|
|
107
85
|
userId,
|
|
@@ -113,65 +91,98 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
113
91
|
if (err)
|
|
114
92
|
console.error(err);
|
|
115
93
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
94
|
+
const intercomUserId = (await intercom?.contacts.search({
|
|
95
|
+
query: {
|
|
96
|
+
field: 'external_id',
|
|
97
|
+
operator: intercom_client_1.Intercom.SingleFilterSearchRequestOperator.Equals,
|
|
98
|
+
value: userId,
|
|
99
|
+
},
|
|
100
|
+
}))?.data.at(0)?.id;
|
|
101
|
+
if (intercomUserId)
|
|
102
|
+
await intercom?.events.create({
|
|
103
|
+
user_id: intercomUserId,
|
|
119
104
|
event_name: event,
|
|
120
|
-
metadata: properties,
|
|
105
|
+
metadata: (0, convertValuesToString_1.convertValuesToString)(properties),
|
|
106
|
+
created_at: timestamp ? timestamp.getTime() : Date.now(),
|
|
121
107
|
});
|
|
122
|
-
}
|
|
123
|
-
catch (error) {
|
|
124
|
-
console.error(error);
|
|
125
|
-
}
|
|
126
108
|
}
|
|
127
109
|
},
|
|
128
110
|
identify: async ({ traits, userId, disableGeoip, timestamp, }) => {
|
|
129
|
-
if (isDev || (0,
|
|
111
|
+
if (isDev || (0, utils_1.isTest)(userId))
|
|
130
112
|
return;
|
|
131
113
|
posthog?.identify({
|
|
132
114
|
distinctId: userId,
|
|
133
|
-
properties: (0,
|
|
115
|
+
properties: (0, utils_1.convertKeysToSnakeCase)(traits),
|
|
134
116
|
disableGeoip,
|
|
135
117
|
});
|
|
136
118
|
june?.identify({
|
|
137
119
|
userId,
|
|
138
120
|
traits,
|
|
139
121
|
timestamp,
|
|
140
|
-
}, (err) => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
122
|
+
}, (err) => (err ? console.error(err) : {}));
|
|
123
|
+
const intercomUserId = (await intercom?.contacts.search({
|
|
124
|
+
query: {
|
|
125
|
+
field: 'external_id',
|
|
126
|
+
operator: intercom_client_1.Intercom.SingleFilterSearchRequestOperator.Equals,
|
|
127
|
+
value: userId,
|
|
128
|
+
},
|
|
129
|
+
}))?.data.at(0)?.id;
|
|
130
|
+
const props = {
|
|
131
|
+
external_id: userId,
|
|
132
|
+
name: traits.name ? traits.name.toString() : undefined,
|
|
133
|
+
email: traits.email ? traits.email.toString() : undefined,
|
|
134
|
+
role: traits.role ? traits.role.toString() : undefined,
|
|
135
|
+
avatar: traits.avatar ? traits.avatar.toString() : undefined,
|
|
136
|
+
custom_attributes: traits,
|
|
137
|
+
};
|
|
138
|
+
if (intercomUserId)
|
|
139
|
+
await intercom?.contacts.update(intercomUserId, props);
|
|
140
|
+
else
|
|
141
|
+
await intercom?.contacts.create(props);
|
|
144
142
|
},
|
|
145
143
|
group: async ({ traits, groupId, userId, anonymousId, timestamp, }) => {
|
|
146
|
-
if (isDev || (0,
|
|
144
|
+
if (isDev || (0, utils_1.isTest)(userId || anonymousId || '', groupId))
|
|
147
145
|
return;
|
|
146
|
+
// upsert intercom company
|
|
147
|
+
const intercomCompanyId = (await intercom?.companies.create({
|
|
148
|
+
name: groupId,
|
|
149
|
+
company_id: groupId,
|
|
150
|
+
remote_created_at: timestamp ? timestamp.getTime() : Date.now(),
|
|
151
|
+
}, traits))?.id;
|
|
148
152
|
if (anonymousId) {
|
|
149
153
|
posthog?.groupIdentify({
|
|
150
154
|
groupKey: groupId,
|
|
151
155
|
groupType: 'tenant',
|
|
152
|
-
properties: (0,
|
|
156
|
+
properties: (0, utils_1.convertKeysToSnakeCase)(traits),
|
|
153
157
|
distinctId: anonymousId,
|
|
154
158
|
});
|
|
155
|
-
june?.group({ anonymousId, groupId, traits, timestamp }, (err) => {
|
|
156
|
-
if (err)
|
|
157
|
-
console.error(err);
|
|
158
|
-
});
|
|
159
|
+
june?.group({ anonymousId, groupId, traits, timestamp }, (err) => err ? console.error(err) : {});
|
|
159
160
|
}
|
|
160
161
|
else if (userId) {
|
|
161
162
|
posthog?.groupIdentify({
|
|
162
163
|
groupKey: groupId,
|
|
163
164
|
groupType: 'tenant',
|
|
164
|
-
properties: (0,
|
|
165
|
+
properties: (0, utils_1.convertKeysToSnakeCase)(traits),
|
|
165
166
|
distinctId: userId,
|
|
166
167
|
});
|
|
167
|
-
june?.group({ userId, groupId, traits, timestamp }, (err) => {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
168
|
+
june?.group({ userId, groupId, traits, timestamp }, (err) => err ? console.error(err) : {});
|
|
169
|
+
// attach user to intercom company
|
|
170
|
+
const intercomUserId = (await intercom?.contacts.search({
|
|
171
|
+
query: {
|
|
172
|
+
field: 'external_id',
|
|
173
|
+
operator: intercom_client_1.Intercom.SingleFilterSearchRequestOperator.Equals,
|
|
174
|
+
value: userId,
|
|
175
|
+
},
|
|
176
|
+
}))?.data.at(0)?.id;
|
|
177
|
+
if (intercomUserId && intercomCompanyId)
|
|
178
|
+
intercom?.companies.attachContact(intercomUserId, {
|
|
179
|
+
id: intercomCompanyId,
|
|
180
|
+
});
|
|
171
181
|
}
|
|
172
182
|
},
|
|
173
183
|
shutdown: async () => Promise.all([posthog?.shutdown(), june?.closeAndFlush(), log?.flush()]),
|
|
174
184
|
// export dependencies
|
|
185
|
+
intercom,
|
|
175
186
|
_intercom,
|
|
176
187
|
posthog,
|
|
177
188
|
june,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["tracking/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,4DAAoD;AACpD,+CAAuC;AACvC,gCAAkE;AAClE,yCAAyC;AAGzC,0CAAwB;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["tracking/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,4DAAoD;AACpD,qDAA2D;AAC3D,kEAA+D;AAC/D,+CAAuC;AACvC,gCAAkE;AAClE,oCAA0D;AAC1D,0EAAuE;AACvE,yCAAyC;AAGzC,0CAAwB;AAwCjB,MAAM,WAAW,GAAG,CACzB,EAAE,OAAO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E,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,SAAS;QACT,OAAO;QACP,IAAI;QACJ,GAAG;KACJ,CAAC;AACJ,CAAC,CAAC;AAtOW,QAAA,WAAW,eAsOtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const convertKeysToSnakeCase: (obj?: Record<string, any>) => Record<string, any>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertKeysToSnakeCase = void 0;
|
|
4
|
+
const convertKeysToSnakeCase = (obj) => {
|
|
5
|
+
const result = {};
|
|
6
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
7
|
+
for (const key in obj) {
|
|
8
|
+
if (hasOwnProperty.call(obj, key)) {
|
|
9
|
+
const snakeKey = key.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
|
|
10
|
+
if (typeof obj[key] === 'object' &&
|
|
11
|
+
!Array.isArray(obj[key]) &&
|
|
12
|
+
obj[key] !== null) {
|
|
13
|
+
result[snakeKey] = (0, exports.convertKeysToSnakeCase)(obj[key]);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
result[snakeKey] = obj[key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
};
|
|
22
|
+
exports.convertKeysToSnakeCase = convertKeysToSnakeCase;
|
|
23
|
+
//# sourceMappingURL=convertKeysToSnakeCase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertKeysToSnakeCase.js","sourceRoot":"src/","sources":["utils/convertKeysToSnakeCase.ts"],"names":[],"mappings":";;;AAAO,MAAM,sBAAsB,GAAG,CAAC,GAAyB,EAAE,EAAE;IAClE,MAAM,MAAM,GAAwB,EAAE,CAAC;IACvC,MAAM,cAAc,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC;IAEvD,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,IAAI,cAAc,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC;YAClC,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC,WAAW,EAAE,CAAC;YACvE,IACE,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,QAAQ;gBAC5B,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBACxB,GAAG,CAAC,GAAG,CAAC,KAAK,IAAI,EACjB,CAAC;gBACD,MAAM,CAAC,QAAQ,CAAC,GAAG,IAAA,8BAAsB,EAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;YACtD,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AApBW,QAAA,sBAAsB,0BAoBjC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertValuesToString = convertValuesToString;
|
|
4
|
+
function convertValuesToString(obj) {
|
|
5
|
+
const result = {};
|
|
6
|
+
for (const key of Object.keys(obj))
|
|
7
|
+
if (obj[key] !== undefined)
|
|
8
|
+
result[key] = obj[key].toString();
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=convertValuesToString.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertValuesToString.js","sourceRoot":"src/","sources":["utils/convertValuesToString.ts"],"names":[],"mappings":";;AAAA,sDASC;AATD,SAAgB,qBAAqB,CAAC,GAA2B;IAG/D,MAAM,MAAM,GAA8B,EAAE,CAAC;IAE7C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;QAChC,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;IAEhE,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./convertKeysToSnakeCase"), exports);
|
|
18
|
+
__exportStar(require("./convertValuesToString"), exports);
|
|
19
|
+
__exportStar(require("./isTest"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2DAAyC;AACzC,0DAAwC;AACxC,2CAAyB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isTest: (userId: string, groupId?: string, userEmail?: string) => boolean | "" | undefined;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isTest = void 0;
|
|
4
|
+
const E2E_TEST_USERS = {
|
|
5
|
+
'e2e-invite-member@finalytic.io': '113d73d8-ee21-46b7-a12a-a74f632401ca',
|
|
6
|
+
'e2e-invite-owner@finalytic.io': '917b7c4e-cb03-491c-9e94-d33a5bdeca05',
|
|
7
|
+
'e2e-sign-up@finalytic.io': 'ec8e5572-74d0-4ae6-af73-bca8db9a27e9',
|
|
8
|
+
'lars+checkly@finalytic.co': '9e7dfc88-503c-4222-9905-9116169d2203',
|
|
9
|
+
};
|
|
10
|
+
const E2E_TEST_TEAMS = {
|
|
11
|
+
'VRP Automated Testing': 'c4fd21b4-8447-43a2-bdcb-f06a85a935ad',
|
|
12
|
+
test_company_xxxx: '8f21060e-afe6-410a-b2f4-00a3caa20786',
|
|
13
|
+
};
|
|
14
|
+
const isTest = (userId, groupId, userEmail) => Object.values(E2E_TEST_USERS).includes(userId) ||
|
|
15
|
+
(groupId && Object.values(E2E_TEST_TEAMS).includes(groupId)) ||
|
|
16
|
+
(userEmail && Object.keys(E2E_TEST_USERS).includes(userEmail));
|
|
17
|
+
exports.isTest = isTest;
|
|
18
|
+
//# sourceMappingURL=isTest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isTest.js","sourceRoot":"src/","sources":["utils/isTest.ts"],"names":[],"mappings":";;;AAAA,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"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { Analytics } from '@june-so/analytics-node';
|
|
2
|
+
import { IntercomClient } from 'intercom-client';
|
|
2
3
|
import { PostHog } from 'posthog-node';
|
|
3
4
|
import { type LogBindings, type WorkerLog } from '../log';
|
|
4
5
|
import { useIntercom } from './intercom';
|
|
5
6
|
import type { TrackingEvent } from './types';
|
|
6
7
|
export * from './types';
|
|
7
|
-
export declare const isTest: (userId: string, groupId?: string, userEmail?: string) => boolean | "" | undefined;
|
|
8
|
-
export declare const convertKeysToSnakeCase: (obj?: Record<string, any>) => Record<string, any>;
|
|
9
8
|
export type TrackingProps = {
|
|
10
9
|
name: string;
|
|
11
10
|
dataset: string;
|
|
@@ -48,6 +47,7 @@ export declare const useTracking: ({ dataset, env, name }: TrackingProps, isDev?
|
|
|
48
47
|
shutdown: () => Promise<any>;
|
|
49
48
|
log: WorkerLog;
|
|
50
49
|
june?: Analytics;
|
|
50
|
+
intercom?: IntercomClient;
|
|
51
51
|
_intercom?: ReturnType<typeof useIntercom>;
|
|
52
52
|
posthog?: PostHog;
|
|
53
53
|
};
|
|
@@ -1,43 +1,23 @@
|
|
|
1
1
|
import { Analytics } from '@june-so/analytics-node';
|
|
2
|
+
import { Intercom, IntercomClient } from 'intercom-client';
|
|
3
|
+
import { fetcher } from 'intercom-client/core/fetcher/Fetcher';
|
|
2
4
|
import { PostHog } from 'posthog-node';
|
|
3
5
|
import { useLog } from '../log';
|
|
6
|
+
import { convertKeysToSnakeCase, isTest } from '../utils';
|
|
7
|
+
import { convertValuesToString } from '../utils/convertValuesToString';
|
|
4
8
|
import { useIntercom } from './intercom';
|
|
5
9
|
export * from './types';
|
|
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
|
-
const E2E_TEST_TEAMS = {
|
|
13
|
-
'VRP Automated Testing': 'c4fd21b4-8447-43a2-bdcb-f06a85a935ad',
|
|
14
|
-
test_company_xxxx: '8f21060e-afe6-410a-b2f4-00a3caa20786',
|
|
15
|
-
};
|
|
16
|
-
export const isTest = (userId, groupId, userEmail) => Object.values(E2E_TEST_USERS).includes(userId) ||
|
|
17
|
-
(groupId && Object.values(E2E_TEST_TEAMS).includes(groupId)) ||
|
|
18
|
-
(userEmail && Object.keys(E2E_TEST_USERS).includes(userEmail));
|
|
19
|
-
const camelToSnakeCase = (str) => str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
|
|
20
|
-
// todo: move to utils
|
|
21
|
-
export const convertKeysToSnakeCase = (obj) => {
|
|
22
|
-
const result = {};
|
|
23
|
-
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
24
|
-
for (const key in obj) {
|
|
25
|
-
if (hasOwnProperty.call(obj, key)) {
|
|
26
|
-
const snakeKey = camelToSnakeCase(key);
|
|
27
|
-
if (typeof obj[key] === 'object' &&
|
|
28
|
-
!Array.isArray(obj[key]) &&
|
|
29
|
-
obj[key] !== null) {
|
|
30
|
-
result[snakeKey] = convertKeysToSnakeCase(obj[key]);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
result[snakeKey] = obj[key];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return result;
|
|
38
|
-
};
|
|
39
10
|
export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
40
11
|
const log = useLog({ name, dataset, env });
|
|
12
|
+
const intercom = env.INTERCOM_TOKEN
|
|
13
|
+
? new IntercomClient({
|
|
14
|
+
token: env.INTERCOM_TOKEN,
|
|
15
|
+
fetcher: (args) => fetcher({
|
|
16
|
+
...args,
|
|
17
|
+
headers: { ...args.headers, accept: 'application/json' },
|
|
18
|
+
}),
|
|
19
|
+
})
|
|
20
|
+
: undefined;
|
|
41
21
|
const _intercom = env.INTERCOM_TOKEN
|
|
42
22
|
? useIntercom(env.INTERCOM_TOKEN, log)
|
|
43
23
|
: undefined;
|
|
@@ -94,16 +74,20 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
94
74
|
if (err)
|
|
95
75
|
console.error(err);
|
|
96
76
|
});
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
77
|
+
const intercomUserId = (await intercom?.contacts.search({
|
|
78
|
+
query: {
|
|
79
|
+
field: 'external_id',
|
|
80
|
+
operator: Intercom.SingleFilterSearchRequestOperator.Equals,
|
|
81
|
+
value: userId,
|
|
82
|
+
},
|
|
83
|
+
}))?.data.at(0)?.id;
|
|
84
|
+
if (intercomUserId)
|
|
85
|
+
await intercom?.events.create({
|
|
86
|
+
user_id: intercomUserId,
|
|
100
87
|
event_name: event,
|
|
101
|
-
metadata: properties,
|
|
88
|
+
metadata: convertValuesToString(properties),
|
|
89
|
+
created_at: timestamp ? timestamp.getTime() : Date.now(),
|
|
102
90
|
});
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
console.error(error);
|
|
106
|
-
}
|
|
107
91
|
}
|
|
108
92
|
},
|
|
109
93
|
identify: async ({ traits, userId, disableGeoip, timestamp, }) => {
|
|
@@ -118,14 +102,36 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
118
102
|
userId,
|
|
119
103
|
traits,
|
|
120
104
|
timestamp,
|
|
121
|
-
}, (err) => {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
105
|
+
}, (err) => (err ? console.error(err) : {}));
|
|
106
|
+
const intercomUserId = (await intercom?.contacts.search({
|
|
107
|
+
query: {
|
|
108
|
+
field: 'external_id',
|
|
109
|
+
operator: Intercom.SingleFilterSearchRequestOperator.Equals,
|
|
110
|
+
value: userId,
|
|
111
|
+
},
|
|
112
|
+
}))?.data.at(0)?.id;
|
|
113
|
+
const props = {
|
|
114
|
+
external_id: userId,
|
|
115
|
+
name: traits.name ? traits.name.toString() : undefined,
|
|
116
|
+
email: traits.email ? traits.email.toString() : undefined,
|
|
117
|
+
role: traits.role ? traits.role.toString() : undefined,
|
|
118
|
+
avatar: traits.avatar ? traits.avatar.toString() : undefined,
|
|
119
|
+
custom_attributes: traits,
|
|
120
|
+
};
|
|
121
|
+
if (intercomUserId)
|
|
122
|
+
await intercom?.contacts.update(intercomUserId, props);
|
|
123
|
+
else
|
|
124
|
+
await intercom?.contacts.create(props);
|
|
125
125
|
},
|
|
126
126
|
group: async ({ traits, groupId, userId, anonymousId, timestamp, }) => {
|
|
127
127
|
if (isDev || isTest(userId || anonymousId || '', groupId))
|
|
128
128
|
return;
|
|
129
|
+
// upsert intercom company
|
|
130
|
+
const intercomCompanyId = (await intercom?.companies.create({
|
|
131
|
+
name: groupId,
|
|
132
|
+
company_id: groupId,
|
|
133
|
+
remote_created_at: timestamp ? timestamp.getTime() : Date.now(),
|
|
134
|
+
}, traits))?.id;
|
|
129
135
|
if (anonymousId) {
|
|
130
136
|
posthog?.groupIdentify({
|
|
131
137
|
groupKey: groupId,
|
|
@@ -133,10 +139,7 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
133
139
|
properties: convertKeysToSnakeCase(traits),
|
|
134
140
|
distinctId: anonymousId,
|
|
135
141
|
});
|
|
136
|
-
june?.group({ anonymousId, groupId, traits, timestamp }, (err) => {
|
|
137
|
-
if (err)
|
|
138
|
-
console.error(err);
|
|
139
|
-
});
|
|
142
|
+
june?.group({ anonymousId, groupId, traits, timestamp }, (err) => err ? console.error(err) : {});
|
|
140
143
|
}
|
|
141
144
|
else if (userId) {
|
|
142
145
|
posthog?.groupIdentify({
|
|
@@ -145,14 +148,24 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
145
148
|
properties: convertKeysToSnakeCase(traits),
|
|
146
149
|
distinctId: userId,
|
|
147
150
|
});
|
|
148
|
-
june?.group({ userId, groupId, traits, timestamp }, (err) => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
151
|
+
june?.group({ userId, groupId, traits, timestamp }, (err) => err ? console.error(err) : {});
|
|
152
|
+
// attach user to intercom company
|
|
153
|
+
const intercomUserId = (await intercom?.contacts.search({
|
|
154
|
+
query: {
|
|
155
|
+
field: 'external_id',
|
|
156
|
+
operator: Intercom.SingleFilterSearchRequestOperator.Equals,
|
|
157
|
+
value: userId,
|
|
158
|
+
},
|
|
159
|
+
}))?.data.at(0)?.id;
|
|
160
|
+
if (intercomUserId && intercomCompanyId)
|
|
161
|
+
intercom?.companies.attachContact(intercomUserId, {
|
|
162
|
+
id: intercomCompanyId,
|
|
163
|
+
});
|
|
152
164
|
}
|
|
153
165
|
},
|
|
154
166
|
shutdown: async () => Promise.all([posthog?.shutdown(), june?.closeAndFlush(), log?.flush()]),
|
|
155
167
|
// export dependencies
|
|
168
|
+
intercom,
|
|
156
169
|
_intercom,
|
|
157
170
|
posthog,
|
|
158
171
|
june,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["t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AAG,EAAE,EAAE,CAC1D,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAC9B,CAAC;gBAEF,kCAAkC;gBAClC,MAAM,cAAc,GAAG,CACrB,MAAM,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC;oBAC9B,KAAK,EAAE;wBACL,KAAK,EAAE,aAAa;wBACpB,QAAQ,EAAE,QAAQ,CAAC,iCAAiC,CAAC,MAAM;wBAC3D,KAAK,EAAE,MAAM;qBACd;iBACF,CAAC,CACH,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;gBAClB,IAAI,cAAc,IAAI,iBAAiB;oBACrC,QAAQ,EAAE,SAAS,CAAC,aAAa,CAAC,cAAc,EAAE;wBAChD,EAAE,EAAE,iBAAiB;qBACtB,CAAC,CAAC;YACP,CAAC;QACH,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,SAAS;QACT,OAAO;QACP,IAAI;QACJ,GAAG;KACJ,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const convertKeysToSnakeCase: (obj?: Record<string, any>) => Record<string, any>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const convertKeysToSnakeCase = (obj) => {
|
|
2
|
+
const result = {};
|
|
3
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
4
|
+
for (const key in obj) {
|
|
5
|
+
if (hasOwnProperty.call(obj, key)) {
|
|
6
|
+
const snakeKey = key.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
|
|
7
|
+
if (typeof obj[key] === 'object' &&
|
|
8
|
+
!Array.isArray(obj[key]) &&
|
|
9
|
+
obj[key] !== null) {
|
|
10
|
+
result[snakeKey] = convertKeysToSnakeCase(obj[key]);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
result[snakeKey] = obj[key];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=convertKeysToSnakeCase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertKeysToSnakeCase.js","sourceRoot":"src/","sources":["utils/convertKeysToSnakeCase.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,GAAyB,EAAE,EAAE;IAClE,MAAM,MAAM,GAAwB,EAAE,CAAC;IACvC,MAAM,cAAc,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC;IAEvD,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,IAAI,cAAc,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC;YAClC,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC,WAAW,EAAE,CAAC;YACvE,IACE,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,QAAQ;gBAC5B,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBACxB,GAAG,CAAC,GAAG,CAAC,KAAK,IAAI,EACjB,CAAC;gBACD,MAAM,CAAC,QAAQ,CAAC,GAAG,sBAAsB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;YACtD,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertValuesToString.js","sourceRoot":"src/","sources":["utils/convertValuesToString.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,qBAAqB,CAAC,GAA2B;IAG/D,MAAM,MAAM,GAA8B,EAAE,CAAC;IAE7C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;QAChC,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;IAEhE,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isTest: (userId: string, groupId?: string, userEmail?: string) => boolean | "" | undefined;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const E2E_TEST_USERS = {
|
|
2
|
+
'e2e-invite-member@finalytic.io': '113d73d8-ee21-46b7-a12a-a74f632401ca',
|
|
3
|
+
'e2e-invite-owner@finalytic.io': '917b7c4e-cb03-491c-9e94-d33a5bdeca05',
|
|
4
|
+
'e2e-sign-up@finalytic.io': 'ec8e5572-74d0-4ae6-af73-bca8db9a27e9',
|
|
5
|
+
'lars+checkly@finalytic.co': '9e7dfc88-503c-4222-9905-9116169d2203',
|
|
6
|
+
};
|
|
7
|
+
const E2E_TEST_TEAMS = {
|
|
8
|
+
'VRP Automated Testing': 'c4fd21b4-8447-43a2-bdcb-f06a85a935ad',
|
|
9
|
+
test_company_xxxx: '8f21060e-afe6-410a-b2f4-00a3caa20786',
|
|
10
|
+
};
|
|
11
|
+
export const isTest = (userId, groupId, userEmail) => Object.values(E2E_TEST_USERS).includes(userId) ||
|
|
12
|
+
(groupId && Object.values(E2E_TEST_TEAMS).includes(groupId)) ||
|
|
13
|
+
(userEmail && Object.keys(E2E_TEST_USERS).includes(userEmail));
|
|
14
|
+
//# sourceMappingURL=isTest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isTest.js","sourceRoot":"src/","sources":["utils/isTest.ts"],"names":[],"mappings":"AAAA,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;AAEF,MAAM,CAAC,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"}
|
package/package.json
CHANGED
package/src/tracking/index.ts
CHANGED
|
@@ -1,54 +1,15 @@
|
|
|
1
1
|
import { Analytics } from '@june-so/analytics-node';
|
|
2
|
+
import { Intercom, IntercomClient } from 'intercom-client';
|
|
3
|
+
import { fetcher } from 'intercom-client/core/fetcher/Fetcher';
|
|
2
4
|
import { PostHog } from 'posthog-node';
|
|
3
5
|
import { type LogBindings, type WorkerLog, useLog } from '../log';
|
|
6
|
+
import { convertKeysToSnakeCase, isTest } from '../utils';
|
|
7
|
+
import { convertValuesToString } from '../utils/convertValuesToString';
|
|
4
8
|
import { useIntercom } from './intercom';
|
|
5
9
|
import type { TrackingEvent } from './types';
|
|
6
10
|
|
|
7
11
|
export * from './types';
|
|
8
12
|
|
|
9
|
-
const E2E_TEST_USERS = {
|
|
10
|
-
'e2e-invite-member@finalytic.io': '113d73d8-ee21-46b7-a12a-a74f632401ca',
|
|
11
|
-
'e2e-invite-owner@finalytic.io': '917b7c4e-cb03-491c-9e94-d33a5bdeca05',
|
|
12
|
-
'e2e-sign-up@finalytic.io': 'ec8e5572-74d0-4ae6-af73-bca8db9a27e9',
|
|
13
|
-
'lars+checkly@finalytic.co': '9e7dfc88-503c-4222-9905-9116169d2203',
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const E2E_TEST_TEAMS = {
|
|
17
|
-
'VRP Automated Testing': 'c4fd21b4-8447-43a2-bdcb-f06a85a935ad',
|
|
18
|
-
test_company_xxxx: '8f21060e-afe6-410a-b2f4-00a3caa20786',
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const isTest = (userId: string, groupId?: string, userEmail?: string) =>
|
|
22
|
-
Object.values(E2E_TEST_USERS).includes(userId) ||
|
|
23
|
-
(groupId && Object.values(E2E_TEST_TEAMS).includes(groupId)) ||
|
|
24
|
-
(userEmail && Object.keys(E2E_TEST_USERS).includes(userEmail));
|
|
25
|
-
|
|
26
|
-
const camelToSnakeCase = (str: string) =>
|
|
27
|
-
str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
|
|
28
|
-
|
|
29
|
-
// todo: move to utils
|
|
30
|
-
export const convertKeysToSnakeCase = (obj?: Record<string, any>) => {
|
|
31
|
-
const result: Record<string, any> = {};
|
|
32
|
-
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
33
|
-
|
|
34
|
-
for (const key in obj) {
|
|
35
|
-
if (hasOwnProperty.call(obj, key)) {
|
|
36
|
-
const snakeKey = camelToSnakeCase(key);
|
|
37
|
-
if (
|
|
38
|
-
typeof obj[key] === 'object' &&
|
|
39
|
-
!Array.isArray(obj[key]) &&
|
|
40
|
-
obj[key] !== null
|
|
41
|
-
) {
|
|
42
|
-
result[snakeKey] = convertKeysToSnakeCase(obj[key]);
|
|
43
|
-
} else {
|
|
44
|
-
result[snakeKey] = obj[key];
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return result;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
13
|
export type TrackingProps = {
|
|
53
14
|
name: string;
|
|
54
15
|
dataset: string;
|
|
@@ -97,11 +58,22 @@ export const useTracking = (
|
|
|
97
58
|
shutdown: () => Promise<any>;
|
|
98
59
|
log: WorkerLog;
|
|
99
60
|
june?: Analytics;
|
|
61
|
+
intercom?: IntercomClient;
|
|
100
62
|
_intercom?: ReturnType<typeof useIntercom>;
|
|
101
63
|
posthog?: PostHog;
|
|
102
64
|
} => {
|
|
103
65
|
const log = useLog({ name, dataset, env });
|
|
104
66
|
|
|
67
|
+
const intercom = env.INTERCOM_TOKEN
|
|
68
|
+
? new IntercomClient({
|
|
69
|
+
token: env.INTERCOM_TOKEN,
|
|
70
|
+
fetcher: (args) =>
|
|
71
|
+
fetcher({
|
|
72
|
+
...args,
|
|
73
|
+
headers: { ...args.headers, accept: 'application/json' },
|
|
74
|
+
}),
|
|
75
|
+
})
|
|
76
|
+
: undefined;
|
|
105
77
|
const _intercom = env.INTERCOM_TOKEN
|
|
106
78
|
? useIntercom(env.INTERCOM_TOKEN, log)
|
|
107
79
|
: undefined;
|
|
@@ -136,6 +108,7 @@ export const useTracking = (
|
|
|
136
108
|
timestamp,
|
|
137
109
|
properties: convertKeysToSnakeCase(properties),
|
|
138
110
|
});
|
|
111
|
+
|
|
139
112
|
june?.track(
|
|
140
113
|
{
|
|
141
114
|
anonymousId,
|
|
@@ -160,6 +133,7 @@ export const useTracking = (
|
|
|
160
133
|
timestamp,
|
|
161
134
|
properties: convertKeysToSnakeCase(properties),
|
|
162
135
|
});
|
|
136
|
+
|
|
163
137
|
june?.track(
|
|
164
138
|
{
|
|
165
139
|
userId,
|
|
@@ -173,15 +147,22 @@ export const useTracking = (
|
|
|
173
147
|
}
|
|
174
148
|
);
|
|
175
149
|
|
|
176
|
-
|
|
177
|
-
await
|
|
178
|
-
|
|
150
|
+
const intercomUserId = (
|
|
151
|
+
await intercom?.contacts.search({
|
|
152
|
+
query: {
|
|
153
|
+
field: 'external_id',
|
|
154
|
+
operator: Intercom.SingleFilterSearchRequestOperator.Equals,
|
|
155
|
+
value: userId,
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
)?.data.at(0)?.id;
|
|
159
|
+
if (intercomUserId)
|
|
160
|
+
await intercom?.events.create({
|
|
161
|
+
user_id: intercomUserId,
|
|
179
162
|
event_name: event,
|
|
180
|
-
metadata: properties,
|
|
163
|
+
metadata: convertValuesToString(properties),
|
|
164
|
+
created_at: timestamp ? timestamp.getTime() : Date.now(),
|
|
181
165
|
});
|
|
182
|
-
} catch (error: any) {
|
|
183
|
-
console.error(error);
|
|
184
|
-
}
|
|
185
166
|
}
|
|
186
167
|
},
|
|
187
168
|
identify: async ({
|
|
@@ -197,16 +178,36 @@ export const useTracking = (
|
|
|
197
178
|
properties: convertKeysToSnakeCase(traits),
|
|
198
179
|
disableGeoip,
|
|
199
180
|
});
|
|
181
|
+
|
|
200
182
|
june?.identify(
|
|
201
183
|
{
|
|
202
184
|
userId,
|
|
203
185
|
traits,
|
|
204
186
|
timestamp,
|
|
205
187
|
},
|
|
206
|
-
(err) => {
|
|
207
|
-
if (err) console.error(err);
|
|
208
|
-
}
|
|
188
|
+
(err) => (err ? console.error(err) : {})
|
|
209
189
|
);
|
|
190
|
+
|
|
191
|
+
const intercomUserId = (
|
|
192
|
+
await intercom?.contacts.search({
|
|
193
|
+
query: {
|
|
194
|
+
field: 'external_id',
|
|
195
|
+
operator: Intercom.SingleFilterSearchRequestOperator.Equals,
|
|
196
|
+
value: userId,
|
|
197
|
+
},
|
|
198
|
+
})
|
|
199
|
+
)?.data.at(0)?.id;
|
|
200
|
+
const props = {
|
|
201
|
+
external_id: userId,
|
|
202
|
+
name: traits.name ? traits.name.toString() : undefined,
|
|
203
|
+
email: traits.email ? traits.email.toString() : undefined,
|
|
204
|
+
role: traits.role ? traits.role.toString() : undefined,
|
|
205
|
+
avatar: traits.avatar ? traits.avatar.toString() : undefined,
|
|
206
|
+
custom_attributes: traits,
|
|
207
|
+
};
|
|
208
|
+
if (intercomUserId)
|
|
209
|
+
await intercom?.contacts.update(intercomUserId, props);
|
|
210
|
+
else await intercom?.contacts.create(props);
|
|
210
211
|
},
|
|
211
212
|
group: async ({
|
|
212
213
|
traits,
|
|
@@ -217,6 +218,18 @@ export const useTracking = (
|
|
|
217
218
|
}: GroupProps) => {
|
|
218
219
|
if (isDev || isTest(userId || anonymousId || '', groupId)) return;
|
|
219
220
|
|
|
221
|
+
// upsert intercom company
|
|
222
|
+
const intercomCompanyId = (
|
|
223
|
+
await intercom?.companies.create(
|
|
224
|
+
{
|
|
225
|
+
name: groupId,
|
|
226
|
+
company_id: groupId,
|
|
227
|
+
remote_created_at: timestamp ? timestamp.getTime() : Date.now(),
|
|
228
|
+
},
|
|
229
|
+
traits
|
|
230
|
+
)
|
|
231
|
+
)?.id;
|
|
232
|
+
|
|
220
233
|
if (anonymousId) {
|
|
221
234
|
posthog?.groupIdentify({
|
|
222
235
|
groupKey: groupId,
|
|
@@ -224,9 +237,10 @@ export const useTracking = (
|
|
|
224
237
|
properties: convertKeysToSnakeCase(traits),
|
|
225
238
|
distinctId: anonymousId,
|
|
226
239
|
});
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
240
|
+
|
|
241
|
+
june?.group({ anonymousId, groupId, traits, timestamp }, (err) =>
|
|
242
|
+
err ? console.error(err) : {}
|
|
243
|
+
);
|
|
230
244
|
} else if (userId) {
|
|
231
245
|
posthog?.groupIdentify({
|
|
232
246
|
groupKey: groupId,
|
|
@@ -234,14 +248,31 @@ export const useTracking = (
|
|
|
234
248
|
properties: convertKeysToSnakeCase(traits),
|
|
235
249
|
distinctId: userId,
|
|
236
250
|
});
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
251
|
+
|
|
252
|
+
june?.group({ userId, groupId, traits, timestamp }, (err) =>
|
|
253
|
+
err ? console.error(err) : {}
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
// attach user to intercom company
|
|
257
|
+
const intercomUserId = (
|
|
258
|
+
await intercom?.contacts.search({
|
|
259
|
+
query: {
|
|
260
|
+
field: 'external_id',
|
|
261
|
+
operator: Intercom.SingleFilterSearchRequestOperator.Equals,
|
|
262
|
+
value: userId,
|
|
263
|
+
},
|
|
264
|
+
})
|
|
265
|
+
)?.data.at(0)?.id;
|
|
266
|
+
if (intercomUserId && intercomCompanyId)
|
|
267
|
+
intercom?.companies.attachContact(intercomUserId, {
|
|
268
|
+
id: intercomCompanyId,
|
|
269
|
+
});
|
|
240
270
|
}
|
|
241
271
|
},
|
|
242
272
|
shutdown: async () =>
|
|
243
273
|
Promise.all([posthog?.shutdown(), june?.closeAndFlush(), log?.flush()]),
|
|
244
274
|
// export dependencies
|
|
275
|
+
intercom,
|
|
245
276
|
_intercom,
|
|
246
277
|
posthog,
|
|
247
278
|
june,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const convertKeysToSnakeCase = (obj?: Record<string, any>) => {
|
|
2
|
+
const result: Record<string, any> = {};
|
|
3
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
4
|
+
|
|
5
|
+
for (const key in obj) {
|
|
6
|
+
if (hasOwnProperty.call(obj, key)) {
|
|
7
|
+
const snakeKey = key.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
|
|
8
|
+
if (
|
|
9
|
+
typeof obj[key] === 'object' &&
|
|
10
|
+
!Array.isArray(obj[key]) &&
|
|
11
|
+
obj[key] !== null
|
|
12
|
+
) {
|
|
13
|
+
result[snakeKey] = convertKeysToSnakeCase(obj[key]);
|
|
14
|
+
} else {
|
|
15
|
+
result[snakeKey] = obj[key];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return result;
|
|
21
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function convertValuesToString(obj: { [key: string]: any }): {
|
|
2
|
+
[key: string]: string;
|
|
3
|
+
} {
|
|
4
|
+
const result: { [key: string]: string } = {};
|
|
5
|
+
|
|
6
|
+
for (const key of Object.keys(obj))
|
|
7
|
+
if (obj[key] !== undefined) result[key] = obj[key].toString();
|
|
8
|
+
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const E2E_TEST_USERS = {
|
|
2
|
+
'e2e-invite-member@finalytic.io': '113d73d8-ee21-46b7-a12a-a74f632401ca',
|
|
3
|
+
'e2e-invite-owner@finalytic.io': '917b7c4e-cb03-491c-9e94-d33a5bdeca05',
|
|
4
|
+
'e2e-sign-up@finalytic.io': 'ec8e5572-74d0-4ae6-af73-bca8db9a27e9',
|
|
5
|
+
'lars+checkly@finalytic.co': '9e7dfc88-503c-4222-9905-9116169d2203',
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const E2E_TEST_TEAMS = {
|
|
9
|
+
'VRP Automated Testing': 'c4fd21b4-8447-43a2-bdcb-f06a85a935ad',
|
|
10
|
+
test_company_xxxx: '8f21060e-afe6-410a-b2f4-00a3caa20786',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const isTest = (userId: string, groupId?: string, userEmail?: string) =>
|
|
14
|
+
Object.values(E2E_TEST_USERS).includes(userId) ||
|
|
15
|
+
(groupId && Object.values(E2E_TEST_TEAMS).includes(groupId)) ||
|
|
16
|
+
(userEmail && Object.keys(E2E_TEST_USERS).includes(userEmail));
|