-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathtrack.ts
More file actions
74 lines (64 loc) · 1.96 KB
/
track.ts
File metadata and controls
74 lines (64 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { getServiceChildLogger } from '@crowd/logging'
import { Edition } from '@crowd/types'
import { API_CONFIG, IS_DEV_ENV, IS_TEST_ENV, SEGMENT_CONFIG } from '../conf'
import SequelizeRepository from '../database/repositories/sequelizeRepository'
import { CROWD_ANALYTICS_PLATORM_NAME } from './addProductDataToCrowdTenant'
import getTenatUser from './trackHelper'
const log = getServiceChildLogger('segment')
export default async function identify(
event,
properties,
options: any,
userId: any = false,
timestamp: any = false,
) {
const userEmail = SequelizeRepository.getCurrentUser({
...options,
}).email
if (
!IS_TEST_ENV &&
!IS_DEV_ENV &&
SEGMENT_CONFIG.writeKey &&
// This is only for events in the hosted version. Self-hosted has less telemetry.
(API_CONFIG.edition === Edition.CROWD_HOSTED || API_CONFIG.edition === Edition.LFX) &&
userEmail !== 'help@crowd.dev'
) {
if (
properties &&
properties?.platform &&
properties?.platform === CROWD_ANALYTICS_PLATORM_NAME
) {
// no need to track crowd analytics events in segment
// and this is also to ensure we don't get into an infinite loop
return
}
const Analytics = require('analytics-node')
const analytics = new Analytics(SEGMENT_CONFIG.writeKey)
const { userIdOut, tenantIdOut } = getTenatUser(userId, options)
if (!userIdOut) {
return
}
const payload = {
userId: userIdOut,
event,
properties,
context: {
groupId: tenantIdOut,
},
...(timestamp && { timestamp }),
}
try {
analytics.track(payload)
// send product analytics data to crowd tenant workspace
// await addProductData({
// userId: userIdOut,
// tenantId: tenantIdOut,
// event,
// timestamp,
// properties,
// })
} catch (error) {
log.error(error, { payload }, 'Could not send the following payload to Segment')
}
}
}