@vrplatform/log 1.0.16 → 1.0.18
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 +3 -8
- package/build/main/tracking/index.js +7 -19
- package/build/main/tracking/index.js.map +1 -1
- package/build/main/tracking/intercom.d.ts +45 -49
- package/build/main/tracking/intercom.js +60 -45
- package/build/main/tracking/intercom.js.map +1 -1
- package/build/module/tracking/index.d.ts +3 -8
- package/build/module/tracking/index.js +7 -19
- package/build/module/tracking/index.js.map +1 -1
- package/build/module/tracking/intercom.d.ts +45 -49
- package/build/module/tracking/intercom.js +60 -45
- package/build/module/tracking/intercom.js.map +1 -1
- package/package.json +1 -1
- package/src/tracking/index.ts +12 -30
- package/src/tracking/intercom.ts +115 -108
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type IntercomAPI } from './intercom';
|
|
2
|
-
type
|
|
2
|
+
export type TrackingProps = {
|
|
3
3
|
name: string;
|
|
4
4
|
dataset: string;
|
|
5
5
|
env: {
|
|
@@ -9,7 +9,7 @@ type Props = {
|
|
|
9
9
|
};
|
|
10
10
|
};
|
|
11
11
|
export type Tracking = ReturnType<typeof useTracking>;
|
|
12
|
-
export declare const useTracking: ({ dataset, env, name }:
|
|
12
|
+
export declare const useTracking: ({ dataset, env, name }: TrackingProps, isDev?: boolean) => {
|
|
13
13
|
track: ({ userId, event, tenantId, properties, }: {
|
|
14
14
|
userId: string;
|
|
15
15
|
event: string;
|
|
@@ -30,11 +30,6 @@ export declare const useTracking: ({ dataset, env, name }: Props, isDev?: boolea
|
|
|
30
30
|
alias: string;
|
|
31
31
|
}) => void;
|
|
32
32
|
shutdown: () => Promise<void>;
|
|
33
|
-
|
|
34
|
-
tenantId: string;
|
|
35
|
-
customerId: string;
|
|
36
|
-
data: Parameters<IntercomAPI["updateCompany"]>[0]["data"];
|
|
37
|
-
}) => Promise<void>;
|
|
33
|
+
intercom: IntercomAPI;
|
|
38
34
|
log: import("../log").WorkerLog;
|
|
39
35
|
};
|
|
40
|
-
export {};
|
|
@@ -6,13 +6,14 @@ const posthog_node_1 = require("posthog-node");
|
|
|
6
6
|
const log_1 = require("../log");
|
|
7
7
|
const intercom_1 = require("./intercom");
|
|
8
8
|
const useTracking = ({ dataset, env, name }, isDev) => {
|
|
9
|
-
const
|
|
10
|
-
const intercom = (0, intercom_1.useIntercom)(env.INTERCOM_TOKEN,
|
|
9
|
+
const axiom = (0, log_1.useLog)({ name, dataset, env });
|
|
10
|
+
const intercom = (0, intercom_1.useIntercom)(env.INTERCOM_TOKEN, axiom);
|
|
11
11
|
const juneso = new analytics_node_1.Analytics(env.JUNESO_ANALYTICS_WRITE_KEY);
|
|
12
12
|
const posthog = new posthog_node_1.PostHog(env.POSTHOG_API_KEY, {
|
|
13
13
|
host: 'https://app.posthog.com',
|
|
14
14
|
});
|
|
15
15
|
return {
|
|
16
|
+
// General
|
|
16
17
|
track: async ({ userId, event, tenantId, properties, }) => {
|
|
17
18
|
if (isDev)
|
|
18
19
|
return;
|
|
@@ -64,28 +65,15 @@ const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
64
65
|
traits: properties,
|
|
65
66
|
});
|
|
66
67
|
},
|
|
68
|
+
// Posthog specific
|
|
67
69
|
alias: ({ distinctId, alias }) => posthog.alias({ distinctId, alias }),
|
|
68
70
|
shutdown: async () => {
|
|
69
71
|
await posthog.shutdown();
|
|
70
72
|
await juneso.closeAndFlush();
|
|
71
73
|
},
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (!intercomCompany)
|
|
76
|
-
return;
|
|
77
|
-
await intercom.updateCompany({
|
|
78
|
-
companyId: intercomCompany.id,
|
|
79
|
-
data: {
|
|
80
|
-
...data,
|
|
81
|
-
custom_attributes: {
|
|
82
|
-
...data.custom_attributes,
|
|
83
|
-
billingPortalUrl: `https://billing.vrplatform.app/portal/${customerId}`,
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
},
|
|
88
|
-
log,
|
|
74
|
+
// export intercom and axiom
|
|
75
|
+
intercom,
|
|
76
|
+
log: axiom,
|
|
89
77
|
};
|
|
90
78
|
};
|
|
91
79
|
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;AAcpD,MAAM,WAAW,GAAG,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["tracking/index.ts"],"names":[],"mappings":";;;AAAA,4DAAoD;AACpD,+CAAuC;AACvC,gCAAgC;AAChC,yCAA2D;AAcpD,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,KAAK,EACL,QAAQ,EACR,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,QAAQ;iBACjB;gBACD,UAAU;aACX,CAAC,CAAC;YACH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AE,CAAC;QAC/B,CAAC;QACD,4BAA4B;QAC5B,QAAQ;QACR,GAAG,EAAE,KAAK;KACX,CAAC;AACJ,CAAC,CAAC;AAhGW,QAAA,WAAW,eAgGtB"}
|
|
@@ -1,25 +1,22 @@
|
|
|
1
1
|
import type { Log } from '../log';
|
|
2
2
|
export declare class IntercomAPI {
|
|
3
|
-
constructor(token: string, log
|
|
3
|
+
constructor(token: string, log?: Log);
|
|
4
4
|
private headers;
|
|
5
|
-
private log
|
|
5
|
+
private log?;
|
|
6
6
|
private fetch;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
data: {
|
|
10
|
-
email: string | undefined;
|
|
11
|
-
name: string;
|
|
12
|
-
custom_attributes: Record<string, any>;
|
|
13
|
-
};
|
|
14
|
-
}): Promise<unknown>;
|
|
15
|
-
updateCompany({ data, companyId, }: {
|
|
16
|
-
companyId: string;
|
|
7
|
+
getUserByUserId(userId: string): Promise<{
|
|
8
|
+
type: "list";
|
|
17
9
|
data: {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
10
|
+
id: string;
|
|
11
|
+
}[] | undefined;
|
|
12
|
+
total_count: 0;
|
|
13
|
+
pages: {
|
|
14
|
+
type: "pages";
|
|
15
|
+
page: 1;
|
|
16
|
+
per_page: 10;
|
|
17
|
+
total_pages: 0;
|
|
21
18
|
};
|
|
22
|
-
}
|
|
19
|
+
} | undefined>;
|
|
23
20
|
createUser({ data, userId, }: {
|
|
24
21
|
userId: string;
|
|
25
22
|
data: {
|
|
@@ -28,18 +25,35 @@ export declare class IntercomAPI {
|
|
|
28
25
|
custom_attributes: Record<string, any>;
|
|
29
26
|
};
|
|
30
27
|
}): Promise<unknown>;
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
updateUser({ data, contactId, }: {
|
|
29
|
+
contactId: string;
|
|
33
30
|
data: {
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
email?: string;
|
|
32
|
+
name?: string;
|
|
36
33
|
custom_attributes: Record<string, any>;
|
|
37
34
|
};
|
|
38
35
|
}): Promise<unknown>;
|
|
36
|
+
deleteUser(intercom_contact_id: string): Promise<{
|
|
37
|
+
id: "6657adf56abd0167d9419d1c";
|
|
38
|
+
external_id: "70";
|
|
39
|
+
type: "contact";
|
|
40
|
+
deleted: true;
|
|
41
|
+
} | undefined>;
|
|
39
42
|
createCompanyUser(data: {
|
|
40
43
|
intercom_contact_id: string;
|
|
41
44
|
intercom_company_id: string;
|
|
42
45
|
}): Promise<unknown>;
|
|
46
|
+
deleteCompanyUser({ intercom_company_id, intercom_contact_id, }: {
|
|
47
|
+
intercom_contact_id: string;
|
|
48
|
+
intercom_company_id: string;
|
|
49
|
+
}): Promise<{
|
|
50
|
+
id: "6657adc96abd0167d9419ca7";
|
|
51
|
+
} | undefined>;
|
|
52
|
+
trackEvent(data: {
|
|
53
|
+
user_id: string;
|
|
54
|
+
event_name: string;
|
|
55
|
+
metadata: Record<string, any>;
|
|
56
|
+
}): Promise<unknown>;
|
|
43
57
|
getCompanyByTenantId(tenantId: string): Promise<{
|
|
44
58
|
type: "list";
|
|
45
59
|
data: [{
|
|
@@ -73,40 +87,22 @@ export declare class IntercomAPI {
|
|
|
73
87
|
};
|
|
74
88
|
total_count: 1;
|
|
75
89
|
} | undefined>;
|
|
76
|
-
|
|
77
|
-
|
|
90
|
+
createCompany({ data, tenantId, }: {
|
|
91
|
+
tenantId: string;
|
|
78
92
|
data: {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
pages: {
|
|
83
|
-
type: "pages";
|
|
84
|
-
page: 1;
|
|
85
|
-
per_page: 10;
|
|
86
|
-
total_pages: 0;
|
|
93
|
+
name: string;
|
|
94
|
+
remote_created_at: string;
|
|
95
|
+
custom_attributes: Record<string, any>;
|
|
87
96
|
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
deleted: true;
|
|
94
|
-
} | undefined>;
|
|
97
|
+
}): Promise<unknown>;
|
|
98
|
+
updateCompany({ data, companyId, }: {
|
|
99
|
+
companyId: string;
|
|
100
|
+
data: Record<string, any>;
|
|
101
|
+
}): Promise<unknown>;
|
|
95
102
|
deleteCompany(intercom_company_id: string): Promise<{
|
|
96
103
|
id: "6657adc96abd0167d9419ca7";
|
|
97
104
|
object: "company";
|
|
98
105
|
deleted: true;
|
|
99
106
|
} | undefined>;
|
|
100
|
-
deleteCompanyUser({ intercom_company_id, intercom_contact_id, }: {
|
|
101
|
-
intercom_contact_id: string;
|
|
102
|
-
intercom_company_id: string;
|
|
103
|
-
}): Promise<{
|
|
104
|
-
id: "6657adc96abd0167d9419ca7";
|
|
105
|
-
} | undefined>;
|
|
106
|
-
trackEvent(data: {
|
|
107
|
-
user_id: string;
|
|
108
|
-
event_name: string;
|
|
109
|
-
metadata: Record<string, any>;
|
|
110
|
-
}): Promise<unknown>;
|
|
111
107
|
}
|
|
112
|
-
export declare function useIntercom(token: string, log
|
|
108
|
+
export declare function useIntercom(token: string, log?: Log): IntercomAPI;
|
|
@@ -20,36 +20,50 @@ class IntercomAPI {
|
|
|
20
20
|
try {
|
|
21
21
|
const result = await fetch(`https://api.intercom.io${path}`, {
|
|
22
22
|
method: 'GET',
|
|
23
|
-
...options,
|
|
24
23
|
headers: this.headers,
|
|
24
|
+
...options,
|
|
25
25
|
});
|
|
26
26
|
if (!result.ok) {
|
|
27
|
-
this.log
|
|
27
|
+
this.log?.info('Failed to fetch from Intercom', {
|
|
28
28
|
result: {
|
|
29
29
|
status: result.status,
|
|
30
30
|
statusText: result.statusText,
|
|
31
31
|
body: await result.text(),
|
|
32
32
|
},
|
|
33
33
|
});
|
|
34
|
-
|
|
34
|
+
if (result.status === 404)
|
|
35
|
+
return undefined;
|
|
36
|
+
if (result.status === 429) {
|
|
37
|
+
console.log('Intercom rate limit exceeded, waiting for 1 minute');
|
|
38
|
+
// X-RateLimit-Reset: 1487332520 => timestamp from header response when resetting
|
|
39
|
+
const reset = Number(result.headers.get('X-RateLimit-Reset'));
|
|
40
|
+
if (reset) {
|
|
41
|
+
const now = Math.floor(Date.now() / 1000);
|
|
42
|
+
const wait = reset - now + 1;
|
|
43
|
+
console.log(`Waiting for ${wait} seconds`);
|
|
44
|
+
await new Promise((resolve) => setTimeout(resolve, wait * 1000));
|
|
45
|
+
return await this.fetch(path, options);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
35
48
|
}
|
|
36
49
|
return result.status === 202 ? {} : (await result.json());
|
|
37
50
|
}
|
|
38
51
|
catch (error) {
|
|
39
|
-
this.log
|
|
52
|
+
this.log?.error(error);
|
|
40
53
|
return undefined;
|
|
41
54
|
}
|
|
42
55
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
56
|
+
// User
|
|
57
|
+
async getUserByUserId(userId) {
|
|
58
|
+
return await this.fetch('/contacts/search', {
|
|
59
|
+
method: 'POST',
|
|
60
|
+
data: {
|
|
61
|
+
query: {
|
|
62
|
+
field: 'external_id',
|
|
63
|
+
operator: '=',
|
|
64
|
+
value: userId,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
53
67
|
});
|
|
54
68
|
}
|
|
55
69
|
async createUser({ data, userId, }) {
|
|
@@ -58,13 +72,15 @@ class IntercomAPI {
|
|
|
58
72
|
data: { ...data, external_id: userId },
|
|
59
73
|
});
|
|
60
74
|
}
|
|
61
|
-
async
|
|
62
|
-
return await this.fetch(
|
|
63
|
-
method: '
|
|
64
|
-
data
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
75
|
+
async updateUser({ data, contactId, }) {
|
|
76
|
+
return await this.fetch(`/contacts/${contactId}`, {
|
|
77
|
+
method: 'PUT',
|
|
78
|
+
data,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async deleteUser(intercom_contact_id) {
|
|
82
|
+
return await this.fetch(`/contacts/${intercom_contact_id}`, {
|
|
83
|
+
method: 'DELETE',
|
|
68
84
|
});
|
|
69
85
|
}
|
|
70
86
|
async createCompanyUser(data) {
|
|
@@ -75,26 +91,39 @@ class IntercomAPI {
|
|
|
75
91
|
},
|
|
76
92
|
});
|
|
77
93
|
}
|
|
94
|
+
async deleteCompanyUser({ intercom_company_id, intercom_contact_id, }) {
|
|
95
|
+
return await this.fetch(`/contacts/${intercom_contact_id}/companies/${intercom_company_id}`, {
|
|
96
|
+
method: 'DELETE',
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async trackEvent(data) {
|
|
100
|
+
return await this.fetch('/events', {
|
|
101
|
+
method: 'POST',
|
|
102
|
+
data: {
|
|
103
|
+
created_at: Math.floor(new Date().getTime() / 1000), // to seconds
|
|
104
|
+
...data,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
// Company
|
|
78
109
|
async getCompanyByTenantId(tenantId) {
|
|
79
110
|
return await this.fetch(`/companies?company_id=${tenantId}`, {
|
|
80
111
|
method: 'GET',
|
|
81
112
|
});
|
|
82
113
|
}
|
|
83
|
-
async
|
|
84
|
-
return await this.fetch('/
|
|
114
|
+
async createCompany({ data, tenantId, }) {
|
|
115
|
+
return await this.fetch('/companies', {
|
|
85
116
|
method: 'POST',
|
|
86
117
|
data: {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
operator: '=',
|
|
90
|
-
value: userId,
|
|
91
|
-
},
|
|
118
|
+
...data,
|
|
119
|
+
company_id: tenantId,
|
|
92
120
|
},
|
|
93
121
|
});
|
|
94
122
|
}
|
|
95
|
-
async
|
|
96
|
-
return await this.fetch(`/
|
|
97
|
-
method: '
|
|
123
|
+
async updateCompany({ data, companyId, }) {
|
|
124
|
+
return await this.fetch(`/companies/${companyId}`, {
|
|
125
|
+
method: 'PUT',
|
|
126
|
+
data,
|
|
98
127
|
});
|
|
99
128
|
}
|
|
100
129
|
async deleteCompany(intercom_company_id) {
|
|
@@ -102,20 +131,6 @@ class IntercomAPI {
|
|
|
102
131
|
method: 'DELETE',
|
|
103
132
|
});
|
|
104
133
|
}
|
|
105
|
-
async deleteCompanyUser({ intercom_company_id, intercom_contact_id, }) {
|
|
106
|
-
return await this.fetch(`/contacts/${intercom_contact_id}/companies/${intercom_company_id}`, {
|
|
107
|
-
method: 'DELETE',
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
async trackEvent(data) {
|
|
111
|
-
return await this.fetch('/events', {
|
|
112
|
-
method: 'POST',
|
|
113
|
-
data: {
|
|
114
|
-
created_at: Math.floor(new Date().getTime() / 1000), // to seconds
|
|
115
|
-
...data,
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
134
|
}
|
|
120
135
|
exports.IntercomAPI = IntercomAPI;
|
|
121
136
|
function useIntercom(token, log) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intercom.js","sourceRoot":"src/","sources":["tracking/intercom.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"intercom.js","sourceRoot":"src/","sources":["tracking/intercom.ts"],"names":[],"mappings":";;;AA8PA,kCAEC;AA9PD,MAAa,WAAW;IACtB,YAAY,KAAa,EAAE,GAAS;QAClC,IAAI,CAAC,OAAO,GAAG;YACb,cAAc,EAAE,kBAAkB;YAClC,MAAM,EAAE,kBAAkB;YAC1B,kBAAkB,EAAE,MAAM;YAC1B,aAAa,EAAE,UAAU,KAAK,EAAE;SACjC,CAAC;QAEF,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAKO,KAAK,CAAC,KAAK,CACjB,IAAY,EACZ,OAAsC;QAEtC,IAAI,OAAO,EAAE,IAAI,EAAE,CAAC;YAClB,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YAC5C,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC;QAC3B,CAAC;QAED,IAAI,CAAC;YACH,MAAM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AC,mBAA2B;QAC7C,OAAO,MAAM,IAAI,CAAC,KAAK,CAIpB,cAAc,mBAAmB,EAAE,EAAE;YACtC,MAAM,EAAE,QAAQ;SACjB,CAAC,CAAC;IACL,CAAC;CACF;AA1PD,kCA0PC;AAED,SAAgB,WAAW,CAAC,KAAa,EAAE,GAAS;IAClD,OAAO,IAAI,WAAW,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type IntercomAPI } from './intercom';
|
|
2
|
-
type
|
|
2
|
+
export type TrackingProps = {
|
|
3
3
|
name: string;
|
|
4
4
|
dataset: string;
|
|
5
5
|
env: {
|
|
@@ -9,7 +9,7 @@ type Props = {
|
|
|
9
9
|
};
|
|
10
10
|
};
|
|
11
11
|
export type Tracking = ReturnType<typeof useTracking>;
|
|
12
|
-
export declare const useTracking: ({ dataset, env, name }:
|
|
12
|
+
export declare const useTracking: ({ dataset, env, name }: TrackingProps, isDev?: boolean) => {
|
|
13
13
|
track: ({ userId, event, tenantId, properties, }: {
|
|
14
14
|
userId: string;
|
|
15
15
|
event: string;
|
|
@@ -30,11 +30,6 @@ export declare const useTracking: ({ dataset, env, name }: Props, isDev?: boolea
|
|
|
30
30
|
alias: string;
|
|
31
31
|
}) => void;
|
|
32
32
|
shutdown: () => Promise<void>;
|
|
33
|
-
|
|
34
|
-
tenantId: string;
|
|
35
|
-
customerId: string;
|
|
36
|
-
data: Parameters<IntercomAPI["updateCompany"]>[0]["data"];
|
|
37
|
-
}) => Promise<void>;
|
|
33
|
+
intercom: IntercomAPI;
|
|
38
34
|
log: import("../log").WorkerLog;
|
|
39
35
|
};
|
|
40
|
-
export {};
|
|
@@ -3,13 +3,14 @@ import { PostHog } from 'posthog-node';
|
|
|
3
3
|
import { useLog } from '../log';
|
|
4
4
|
import { useIntercom } from './intercom';
|
|
5
5
|
export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
6
|
-
const
|
|
7
|
-
const intercom = useIntercom(env.INTERCOM_TOKEN,
|
|
6
|
+
const axiom = useLog({ name, dataset, env });
|
|
7
|
+
const intercom = useIntercom(env.INTERCOM_TOKEN, axiom);
|
|
8
8
|
const juneso = new Analytics(env.JUNESO_ANALYTICS_WRITE_KEY);
|
|
9
9
|
const posthog = new PostHog(env.POSTHOG_API_KEY, {
|
|
10
10
|
host: 'https://app.posthog.com',
|
|
11
11
|
});
|
|
12
12
|
return {
|
|
13
|
+
// General
|
|
13
14
|
track: async ({ userId, event, tenantId, properties, }) => {
|
|
14
15
|
if (isDev)
|
|
15
16
|
return;
|
|
@@ -61,28 +62,15 @@ export const useTracking = ({ dataset, env, name }, isDev) => {
|
|
|
61
62
|
traits: properties,
|
|
62
63
|
});
|
|
63
64
|
},
|
|
65
|
+
// Posthog specific
|
|
64
66
|
alias: ({ distinctId, alias }) => posthog.alias({ distinctId, alias }),
|
|
65
67
|
shutdown: async () => {
|
|
66
68
|
await posthog.shutdown();
|
|
67
69
|
await juneso.closeAndFlush();
|
|
68
70
|
},
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (!intercomCompany)
|
|
73
|
-
return;
|
|
74
|
-
await intercom.updateCompany({
|
|
75
|
-
companyId: intercomCompany.id,
|
|
76
|
-
data: {
|
|
77
|
-
...data,
|
|
78
|
-
custom_attributes: {
|
|
79
|
-
...data.custom_attributes,
|
|
80
|
-
billingPortalUrl: `https://billing.vrplatform.app/portal/${customerId}`,
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
});
|
|
84
|
-
},
|
|
85
|
-
log,
|
|
71
|
+
// export intercom and axiom
|
|
72
|
+
intercom,
|
|
73
|
+
log: axiom,
|
|
86
74
|
};
|
|
87
75
|
};
|
|
88
76
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"src/","sources":["tracking/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA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aAAa,EAAE,CAAC;QAC/B,CAAC;QACD,4BAA4B;QAC5B,QAAQ;QACR,GAAG,EAAE,KAAK;KACX,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,25 +1,22 @@
|
|
|
1
1
|
import type { Log } from '../log';
|
|
2
2
|
export declare class IntercomAPI {
|
|
3
|
-
constructor(token: string, log
|
|
3
|
+
constructor(token: string, log?: Log);
|
|
4
4
|
private headers;
|
|
5
|
-
private log
|
|
5
|
+
private log?;
|
|
6
6
|
private fetch;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
data: {
|
|
10
|
-
email: string | undefined;
|
|
11
|
-
name: string;
|
|
12
|
-
custom_attributes: Record<string, any>;
|
|
13
|
-
};
|
|
14
|
-
}): Promise<unknown>;
|
|
15
|
-
updateCompany({ data, companyId, }: {
|
|
16
|
-
companyId: string;
|
|
7
|
+
getUserByUserId(userId: string): Promise<{
|
|
8
|
+
type: "list";
|
|
17
9
|
data: {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
10
|
+
id: string;
|
|
11
|
+
}[] | undefined;
|
|
12
|
+
total_count: 0;
|
|
13
|
+
pages: {
|
|
14
|
+
type: "pages";
|
|
15
|
+
page: 1;
|
|
16
|
+
per_page: 10;
|
|
17
|
+
total_pages: 0;
|
|
21
18
|
};
|
|
22
|
-
}
|
|
19
|
+
} | undefined>;
|
|
23
20
|
createUser({ data, userId, }: {
|
|
24
21
|
userId: string;
|
|
25
22
|
data: {
|
|
@@ -28,18 +25,35 @@ export declare class IntercomAPI {
|
|
|
28
25
|
custom_attributes: Record<string, any>;
|
|
29
26
|
};
|
|
30
27
|
}): Promise<unknown>;
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
updateUser({ data, contactId, }: {
|
|
29
|
+
contactId: string;
|
|
33
30
|
data: {
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
email?: string;
|
|
32
|
+
name?: string;
|
|
36
33
|
custom_attributes: Record<string, any>;
|
|
37
34
|
};
|
|
38
35
|
}): Promise<unknown>;
|
|
36
|
+
deleteUser(intercom_contact_id: string): Promise<{
|
|
37
|
+
id: "6657adf56abd0167d9419d1c";
|
|
38
|
+
external_id: "70";
|
|
39
|
+
type: "contact";
|
|
40
|
+
deleted: true;
|
|
41
|
+
} | undefined>;
|
|
39
42
|
createCompanyUser(data: {
|
|
40
43
|
intercom_contact_id: string;
|
|
41
44
|
intercom_company_id: string;
|
|
42
45
|
}): Promise<unknown>;
|
|
46
|
+
deleteCompanyUser({ intercom_company_id, intercom_contact_id, }: {
|
|
47
|
+
intercom_contact_id: string;
|
|
48
|
+
intercom_company_id: string;
|
|
49
|
+
}): Promise<{
|
|
50
|
+
id: "6657adc96abd0167d9419ca7";
|
|
51
|
+
} | undefined>;
|
|
52
|
+
trackEvent(data: {
|
|
53
|
+
user_id: string;
|
|
54
|
+
event_name: string;
|
|
55
|
+
metadata: Record<string, any>;
|
|
56
|
+
}): Promise<unknown>;
|
|
43
57
|
getCompanyByTenantId(tenantId: string): Promise<{
|
|
44
58
|
type: "list";
|
|
45
59
|
data: [{
|
|
@@ -73,40 +87,22 @@ export declare class IntercomAPI {
|
|
|
73
87
|
};
|
|
74
88
|
total_count: 1;
|
|
75
89
|
} | undefined>;
|
|
76
|
-
|
|
77
|
-
|
|
90
|
+
createCompany({ data, tenantId, }: {
|
|
91
|
+
tenantId: string;
|
|
78
92
|
data: {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
pages: {
|
|
83
|
-
type: "pages";
|
|
84
|
-
page: 1;
|
|
85
|
-
per_page: 10;
|
|
86
|
-
total_pages: 0;
|
|
93
|
+
name: string;
|
|
94
|
+
remote_created_at: string;
|
|
95
|
+
custom_attributes: Record<string, any>;
|
|
87
96
|
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
deleted: true;
|
|
94
|
-
} | undefined>;
|
|
97
|
+
}): Promise<unknown>;
|
|
98
|
+
updateCompany({ data, companyId, }: {
|
|
99
|
+
companyId: string;
|
|
100
|
+
data: Record<string, any>;
|
|
101
|
+
}): Promise<unknown>;
|
|
95
102
|
deleteCompany(intercom_company_id: string): Promise<{
|
|
96
103
|
id: "6657adc96abd0167d9419ca7";
|
|
97
104
|
object: "company";
|
|
98
105
|
deleted: true;
|
|
99
106
|
} | undefined>;
|
|
100
|
-
deleteCompanyUser({ intercom_company_id, intercom_contact_id, }: {
|
|
101
|
-
intercom_contact_id: string;
|
|
102
|
-
intercom_company_id: string;
|
|
103
|
-
}): Promise<{
|
|
104
|
-
id: "6657adc96abd0167d9419ca7";
|
|
105
|
-
} | undefined>;
|
|
106
|
-
trackEvent(data: {
|
|
107
|
-
user_id: string;
|
|
108
|
-
event_name: string;
|
|
109
|
-
metadata: Record<string, any>;
|
|
110
|
-
}): Promise<unknown>;
|
|
111
107
|
}
|
|
112
|
-
export declare function useIntercom(token: string, log
|
|
108
|
+
export declare function useIntercom(token: string, log?: Log): IntercomAPI;
|
|
@@ -18,36 +18,50 @@ export class IntercomAPI {
|
|
|
18
18
|
try {
|
|
19
19
|
const result = await fetch(`https://api.intercom.io${path}`, {
|
|
20
20
|
method: 'GET',
|
|
21
|
-
...options,
|
|
22
21
|
headers: this.headers,
|
|
22
|
+
...options,
|
|
23
23
|
});
|
|
24
24
|
if (!result.ok) {
|
|
25
|
-
this.log
|
|
25
|
+
this.log?.info('Failed to fetch from Intercom', {
|
|
26
26
|
result: {
|
|
27
27
|
status: result.status,
|
|
28
28
|
statusText: result.statusText,
|
|
29
29
|
body: await result.text(),
|
|
30
30
|
},
|
|
31
31
|
});
|
|
32
|
-
|
|
32
|
+
if (result.status === 404)
|
|
33
|
+
return undefined;
|
|
34
|
+
if (result.status === 429) {
|
|
35
|
+
console.log('Intercom rate limit exceeded, waiting for 1 minute');
|
|
36
|
+
// X-RateLimit-Reset: 1487332520 => timestamp from header response when resetting
|
|
37
|
+
const reset = Number(result.headers.get('X-RateLimit-Reset'));
|
|
38
|
+
if (reset) {
|
|
39
|
+
const now = Math.floor(Date.now() / 1000);
|
|
40
|
+
const wait = reset - now + 1;
|
|
41
|
+
console.log(`Waiting for ${wait} seconds`);
|
|
42
|
+
await new Promise((resolve) => setTimeout(resolve, wait * 1000));
|
|
43
|
+
return await this.fetch(path, options);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
33
46
|
}
|
|
34
47
|
return result.status === 202 ? {} : (await result.json());
|
|
35
48
|
}
|
|
36
49
|
catch (error) {
|
|
37
|
-
this.log
|
|
50
|
+
this.log?.error(error);
|
|
38
51
|
return undefined;
|
|
39
52
|
}
|
|
40
53
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
54
|
+
// User
|
|
55
|
+
async getUserByUserId(userId) {
|
|
56
|
+
return await this.fetch('/contacts/search', {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
data: {
|
|
59
|
+
query: {
|
|
60
|
+
field: 'external_id',
|
|
61
|
+
operator: '=',
|
|
62
|
+
value: userId,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
51
65
|
});
|
|
52
66
|
}
|
|
53
67
|
async createUser({ data, userId, }) {
|
|
@@ -56,13 +70,15 @@ export class IntercomAPI {
|
|
|
56
70
|
data: { ...data, external_id: userId },
|
|
57
71
|
});
|
|
58
72
|
}
|
|
59
|
-
async
|
|
60
|
-
return await this.fetch(
|
|
61
|
-
method: '
|
|
62
|
-
data
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
73
|
+
async updateUser({ data, contactId, }) {
|
|
74
|
+
return await this.fetch(`/contacts/${contactId}`, {
|
|
75
|
+
method: 'PUT',
|
|
76
|
+
data,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
async deleteUser(intercom_contact_id) {
|
|
80
|
+
return await this.fetch(`/contacts/${intercom_contact_id}`, {
|
|
81
|
+
method: 'DELETE',
|
|
66
82
|
});
|
|
67
83
|
}
|
|
68
84
|
async createCompanyUser(data) {
|
|
@@ -73,26 +89,39 @@ export class IntercomAPI {
|
|
|
73
89
|
},
|
|
74
90
|
});
|
|
75
91
|
}
|
|
92
|
+
async deleteCompanyUser({ intercom_company_id, intercom_contact_id, }) {
|
|
93
|
+
return await this.fetch(`/contacts/${intercom_contact_id}/companies/${intercom_company_id}`, {
|
|
94
|
+
method: 'DELETE',
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async trackEvent(data) {
|
|
98
|
+
return await this.fetch('/events', {
|
|
99
|
+
method: 'POST',
|
|
100
|
+
data: {
|
|
101
|
+
created_at: Math.floor(new Date().getTime() / 1000), // to seconds
|
|
102
|
+
...data,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
// Company
|
|
76
107
|
async getCompanyByTenantId(tenantId) {
|
|
77
108
|
return await this.fetch(`/companies?company_id=${tenantId}`, {
|
|
78
109
|
method: 'GET',
|
|
79
110
|
});
|
|
80
111
|
}
|
|
81
|
-
async
|
|
82
|
-
return await this.fetch('/
|
|
112
|
+
async createCompany({ data, tenantId, }) {
|
|
113
|
+
return await this.fetch('/companies', {
|
|
83
114
|
method: 'POST',
|
|
84
115
|
data: {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
operator: '=',
|
|
88
|
-
value: userId,
|
|
89
|
-
},
|
|
116
|
+
...data,
|
|
117
|
+
company_id: tenantId,
|
|
90
118
|
},
|
|
91
119
|
});
|
|
92
120
|
}
|
|
93
|
-
async
|
|
94
|
-
return await this.fetch(`/
|
|
95
|
-
method: '
|
|
121
|
+
async updateCompany({ data, companyId, }) {
|
|
122
|
+
return await this.fetch(`/companies/${companyId}`, {
|
|
123
|
+
method: 'PUT',
|
|
124
|
+
data,
|
|
96
125
|
});
|
|
97
126
|
}
|
|
98
127
|
async deleteCompany(intercom_company_id) {
|
|
@@ -100,20 +129,6 @@ export class IntercomAPI {
|
|
|
100
129
|
method: 'DELETE',
|
|
101
130
|
});
|
|
102
131
|
}
|
|
103
|
-
async deleteCompanyUser({ intercom_company_id, intercom_contact_id, }) {
|
|
104
|
-
return await this.fetch(`/contacts/${intercom_contact_id}/companies/${intercom_company_id}`, {
|
|
105
|
-
method: 'DELETE',
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
async trackEvent(data) {
|
|
109
|
-
return await this.fetch('/events', {
|
|
110
|
-
method: 'POST',
|
|
111
|
-
data: {
|
|
112
|
-
created_at: Math.floor(new Date().getTime() / 1000), // to seconds
|
|
113
|
-
...data,
|
|
114
|
-
},
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
132
|
}
|
|
118
133
|
export function useIntercom(token, log) {
|
|
119
134
|
return new IntercomAPI(token, log);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intercom.js","sourceRoot":"src/","sources":["tracking/intercom.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAW;IACtB,YAAY,KAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"intercom.js","sourceRoot":"src/","sources":["tracking/intercom.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAW;IACtB,YAAY,KAAa,EAAE,GAAS;QAClC,IAAI,CAAC,OAAO,GAAG;YACb,cAAc,EAAE,kBAAkB;YAClC,MAAM,EAAE,kBAAkB;YAC1B,kBAAkB,EAAE,MAAM;YAC1B,aAAa,EAAE,UAAU,KAAK,EAAE;SACjC,CAAC;QAEF,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAEO,OAAO,CAAyB;IAChC,GAAG,CAAO;IAEV,KAAK,CAAC,KAAK,CACjB,IAAY,EACZ,OAAsC;QAEtC,IAAI,OAAO,EAAE,IAAI,EAAE,CAAC;YAClB,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YAC5C,OAAO,CAAC,IAAI,GAAG,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ACL,CAAC,CAAC;IACL,CAAC;IACD,KAAK,CAAC,aAAa,CAAC,mBAA2B;QAC7C,OAAO,MAAM,IAAI,CAAC,KAAK,CAIpB,cAAc,mBAAmB,EAAE,EAAE;YACtC,MAAM,EAAE,QAAQ;SACjB,CAAC,CAAC;IACL,CAAC;CACF;AAED,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,GAAS;IAClD,OAAO,IAAI,WAAW,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AACrC,CAAC"}
|
package/package.json
CHANGED
package/src/tracking/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { PostHog } from 'posthog-node';
|
|
|
3
3
|
import { useLog } from '../log';
|
|
4
4
|
import { type IntercomAPI, useIntercom } from './intercom';
|
|
5
5
|
|
|
6
|
-
type
|
|
6
|
+
export type TrackingProps = {
|
|
7
7
|
name: string;
|
|
8
8
|
dataset: string;
|
|
9
9
|
env: {
|
|
@@ -15,15 +15,19 @@ type Props = {
|
|
|
15
15
|
|
|
16
16
|
export type Tracking = ReturnType<typeof useTracking>;
|
|
17
17
|
|
|
18
|
-
export const useTracking = (
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
export const useTracking = (
|
|
19
|
+
{ dataset, env, name }: TrackingProps,
|
|
20
|
+
isDev?: boolean
|
|
21
|
+
) => {
|
|
22
|
+
const axiom = useLog({ name, dataset, env });
|
|
23
|
+
const intercom = useIntercom(env.INTERCOM_TOKEN, axiom);
|
|
21
24
|
const juneso = new Analytics(env.JUNESO_ANALYTICS_WRITE_KEY);
|
|
22
25
|
const posthog = new PostHog(env.POSTHOG_API_KEY, {
|
|
23
26
|
host: 'https://app.posthog.com',
|
|
24
27
|
});
|
|
25
28
|
|
|
26
29
|
return {
|
|
30
|
+
// General
|
|
27
31
|
track: async ({
|
|
28
32
|
userId,
|
|
29
33
|
event,
|
|
@@ -96,37 +100,15 @@ export const useTracking = ({ dataset, env, name }: Props, isDev?: boolean) => {
|
|
|
96
100
|
traits: properties,
|
|
97
101
|
});
|
|
98
102
|
},
|
|
103
|
+
// Posthog specific
|
|
99
104
|
alias: ({ distinctId, alias }: { distinctId: string; alias: string }) =>
|
|
100
105
|
posthog.alias({ distinctId, alias }),
|
|
101
106
|
shutdown: async () => {
|
|
102
107
|
await posthog.shutdown();
|
|
103
108
|
await juneso.closeAndFlush();
|
|
104
109
|
},
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
data,
|
|
109
|
-
}: {
|
|
110
|
-
tenantId: string;
|
|
111
|
-
customerId: string;
|
|
112
|
-
data: Parameters<IntercomAPI['updateCompany']>[0]['data'];
|
|
113
|
-
}) => {
|
|
114
|
-
const intercomCompanies = await intercom.getCompanyByTenantId(tenantId);
|
|
115
|
-
const intercomCompany = intercomCompanies?.data?.[0];
|
|
116
|
-
|
|
117
|
-
if (!intercomCompany) return;
|
|
118
|
-
|
|
119
|
-
await intercom.updateCompany({
|
|
120
|
-
companyId: intercomCompany.id,
|
|
121
|
-
data: {
|
|
122
|
-
...data,
|
|
123
|
-
custom_attributes: {
|
|
124
|
-
...data.custom_attributes,
|
|
125
|
-
billingPortalUrl: `https://billing.vrplatform.app/portal/${customerId}`,
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
|
-
});
|
|
129
|
-
},
|
|
130
|
-
log,
|
|
110
|
+
// export intercom and axiom
|
|
111
|
+
intercom,
|
|
112
|
+
log: axiom,
|
|
131
113
|
};
|
|
132
114
|
};
|
package/src/tracking/intercom.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Log } from '../log';
|
|
2
2
|
|
|
3
3
|
export class IntercomAPI {
|
|
4
|
-
constructor(token: string, log
|
|
4
|
+
constructor(token: string, log?: Log) {
|
|
5
5
|
this.headers = {
|
|
6
6
|
'Content-Type': 'application/json',
|
|
7
7
|
Accept: 'application/json',
|
|
@@ -13,7 +13,7 @@ export class IntercomAPI {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
private headers: Record<string, string>;
|
|
16
|
-
private log
|
|
16
|
+
private log?: Log;
|
|
17
17
|
|
|
18
18
|
private async fetch<T>(
|
|
19
19
|
path: string,
|
|
@@ -27,61 +27,67 @@ export class IntercomAPI {
|
|
|
27
27
|
try {
|
|
28
28
|
const result = await fetch(`https://api.intercom.io${path}`, {
|
|
29
29
|
method: 'GET',
|
|
30
|
-
...options,
|
|
31
30
|
headers: this.headers,
|
|
31
|
+
...options,
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
if (!result.ok) {
|
|
35
|
-
this.log
|
|
35
|
+
this.log?.info('Failed to fetch from Intercom', {
|
|
36
36
|
result: {
|
|
37
37
|
status: result.status,
|
|
38
38
|
statusText: result.statusText,
|
|
39
39
|
body: await result.text(),
|
|
40
40
|
},
|
|
41
41
|
});
|
|
42
|
-
|
|
42
|
+
|
|
43
|
+
if (result.status === 404) return undefined;
|
|
44
|
+
|
|
45
|
+
if (result.status === 429) {
|
|
46
|
+
console.log('Intercom rate limit exceeded, waiting for 1 minute');
|
|
47
|
+
// X-RateLimit-Reset: 1487332520 => timestamp from header response when resetting
|
|
48
|
+
|
|
49
|
+
const reset = Number(result.headers.get('X-RateLimit-Reset'));
|
|
50
|
+
|
|
51
|
+
if (reset) {
|
|
52
|
+
const now = Math.floor(Date.now() / 1000);
|
|
53
|
+
const wait = reset - now + 1;
|
|
54
|
+
console.log(`Waiting for ${wait} seconds`);
|
|
55
|
+
await new Promise((resolve) => setTimeout(resolve, wait * 1000));
|
|
56
|
+
|
|
57
|
+
return await this.fetch(path, options);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
43
60
|
}
|
|
44
61
|
return result.status === 202 ? ({} as T) : ((await result.json()) as T);
|
|
45
62
|
} catch (error: any) {
|
|
46
|
-
this.log
|
|
63
|
+
this.log?.error(error);
|
|
47
64
|
return undefined;
|
|
48
65
|
}
|
|
49
66
|
}
|
|
50
67
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
companyId: string;
|
|
73
|
-
data: {
|
|
74
|
-
name: string;
|
|
75
|
-
remote_created_at: string;
|
|
76
|
-
custom_attributes: Record<string, any>;
|
|
77
|
-
};
|
|
78
|
-
}) {
|
|
79
|
-
return await this.fetch(`/companies/${companyId}`, {
|
|
80
|
-
method: 'PUT',
|
|
81
|
-
data,
|
|
68
|
+
// User
|
|
69
|
+
async getUserByUserId(userId: string) {
|
|
70
|
+
return await this.fetch<{
|
|
71
|
+
type: 'list';
|
|
72
|
+
data: { id: string }[] | undefined;
|
|
73
|
+
total_count: 0;
|
|
74
|
+
pages: {
|
|
75
|
+
type: 'pages';
|
|
76
|
+
page: 1;
|
|
77
|
+
per_page: 10;
|
|
78
|
+
total_pages: 0;
|
|
79
|
+
};
|
|
80
|
+
}>('/contacts/search', {
|
|
81
|
+
method: 'POST',
|
|
82
|
+
data: {
|
|
83
|
+
query: {
|
|
84
|
+
field: 'external_id',
|
|
85
|
+
operator: '=',
|
|
86
|
+
value: userId,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
82
89
|
});
|
|
83
90
|
}
|
|
84
|
-
|
|
85
91
|
async createUser({
|
|
86
92
|
data,
|
|
87
93
|
userId,
|
|
@@ -98,27 +104,32 @@ export class IntercomAPI {
|
|
|
98
104
|
data: { ...data, external_id: userId },
|
|
99
105
|
});
|
|
100
106
|
}
|
|
101
|
-
|
|
102
|
-
async createCompany({
|
|
107
|
+
async updateUser({
|
|
103
108
|
data,
|
|
104
|
-
|
|
109
|
+
contactId,
|
|
105
110
|
}: {
|
|
106
|
-
|
|
111
|
+
contactId: string;
|
|
107
112
|
data: {
|
|
108
|
-
|
|
109
|
-
|
|
113
|
+
email?: string;
|
|
114
|
+
name?: string;
|
|
110
115
|
custom_attributes: Record<string, any>;
|
|
111
116
|
};
|
|
112
117
|
}) {
|
|
113
|
-
return await this.fetch(
|
|
114
|
-
method: '
|
|
115
|
-
data
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
return await this.fetch(`/contacts/${contactId}`, {
|
|
119
|
+
method: 'PUT',
|
|
120
|
+
data,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
async deleteUser(intercom_contact_id: string) {
|
|
124
|
+
return await this.fetch<{
|
|
125
|
+
id: '6657adf56abd0167d9419d1c';
|
|
126
|
+
external_id: '70';
|
|
127
|
+
type: 'contact';
|
|
128
|
+
deleted: true;
|
|
129
|
+
}>(`/contacts/${intercom_contact_id}`, {
|
|
130
|
+
method: 'DELETE',
|
|
119
131
|
});
|
|
120
132
|
}
|
|
121
|
-
|
|
122
133
|
async createCompanyUser(data: {
|
|
123
134
|
intercom_contact_id: string;
|
|
124
135
|
intercom_company_id: string;
|
|
@@ -130,7 +141,34 @@ export class IntercomAPI {
|
|
|
130
141
|
},
|
|
131
142
|
});
|
|
132
143
|
}
|
|
144
|
+
async deleteCompanyUser({
|
|
145
|
+
intercom_company_id,
|
|
146
|
+
intercom_contact_id,
|
|
147
|
+
}: {
|
|
148
|
+
intercom_contact_id: string;
|
|
149
|
+
intercom_company_id: string;
|
|
150
|
+
}) {
|
|
151
|
+
return await this.fetch<{
|
|
152
|
+
id: '6657adc96abd0167d9419ca7';
|
|
153
|
+
}>(`/contacts/${intercom_contact_id}/companies/${intercom_company_id}`, {
|
|
154
|
+
method: 'DELETE',
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
async trackEvent(data: {
|
|
158
|
+
user_id: string;
|
|
159
|
+
event_name: string;
|
|
160
|
+
metadata: Record<string, any>;
|
|
161
|
+
}) {
|
|
162
|
+
return await this.fetch('/events', {
|
|
163
|
+
method: 'POST',
|
|
164
|
+
data: {
|
|
165
|
+
created_at: Math.floor(new Date().getTime() / 1000), // to seconds
|
|
166
|
+
...data,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
}
|
|
133
170
|
|
|
171
|
+
// Company
|
|
134
172
|
async getCompanyByTenantId(tenantId: string) {
|
|
135
173
|
return await this.fetch<{
|
|
136
174
|
type: 'list';
|
|
@@ -172,41 +210,37 @@ export class IntercomAPI {
|
|
|
172
210
|
method: 'GET',
|
|
173
211
|
});
|
|
174
212
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}>('/contacts/search', {
|
|
213
|
+
async createCompany({
|
|
214
|
+
data,
|
|
215
|
+
tenantId,
|
|
216
|
+
}: {
|
|
217
|
+
tenantId: string;
|
|
218
|
+
data: {
|
|
219
|
+
name: string;
|
|
220
|
+
remote_created_at: string;
|
|
221
|
+
custom_attributes: Record<string, any>;
|
|
222
|
+
};
|
|
223
|
+
}) {
|
|
224
|
+
return await this.fetch('/companies', {
|
|
188
225
|
method: 'POST',
|
|
189
226
|
data: {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
operator: '=',
|
|
193
|
-
value: userId,
|
|
194
|
-
},
|
|
227
|
+
...data,
|
|
228
|
+
company_id: tenantId,
|
|
195
229
|
},
|
|
196
230
|
});
|
|
197
231
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
method: '
|
|
232
|
+
async updateCompany({
|
|
233
|
+
data,
|
|
234
|
+
companyId,
|
|
235
|
+
}: {
|
|
236
|
+
companyId: string;
|
|
237
|
+
data: Record<string, any>;
|
|
238
|
+
}) {
|
|
239
|
+
return await this.fetch(`/companies/${companyId}`, {
|
|
240
|
+
method: 'PUT',
|
|
241
|
+
data,
|
|
207
242
|
});
|
|
208
243
|
}
|
|
209
|
-
|
|
210
244
|
async deleteCompany(intercom_company_id: string) {
|
|
211
245
|
return await this.fetch<{
|
|
212
246
|
id: '6657adc96abd0167d9419ca7';
|
|
@@ -216,35 +250,8 @@ export class IntercomAPI {
|
|
|
216
250
|
method: 'DELETE',
|
|
217
251
|
});
|
|
218
252
|
}
|
|
219
|
-
|
|
220
|
-
async deleteCompanyUser({
|
|
221
|
-
intercom_company_id,
|
|
222
|
-
intercom_contact_id,
|
|
223
|
-
}: {
|
|
224
|
-
intercom_contact_id: string;
|
|
225
|
-
intercom_company_id: string;
|
|
226
|
-
}) {
|
|
227
|
-
return await this.fetch<{
|
|
228
|
-
id: '6657adc96abd0167d9419ca7';
|
|
229
|
-
}>(`/contacts/${intercom_contact_id}/companies/${intercom_company_id}`, {
|
|
230
|
-
method: 'DELETE',
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async trackEvent(data: {
|
|
235
|
-
user_id: string;
|
|
236
|
-
event_name: string;
|
|
237
|
-
metadata: Record<string, any>;
|
|
238
|
-
}) {
|
|
239
|
-
return await this.fetch('/events', {
|
|
240
|
-
method: 'POST',
|
|
241
|
-
data: {
|
|
242
|
-
created_at: Math.floor(new Date().getTime() / 1000), // to seconds
|
|
243
|
-
...data,
|
|
244
|
-
},
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
253
|
}
|
|
248
|
-
|
|
254
|
+
|
|
255
|
+
export function useIntercom(token: string, log?: Log) {
|
|
249
256
|
return new IntercomAPI(token, log);
|
|
250
257
|
}
|