-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathindex.ts
144 lines (140 loc) · 4.46 KB
/
index.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import type { ActionDefinition } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { PayloadValidationError } from '@segment/actions-core'
import { API_URL } from '../config'
import { batch_size, enable_batching, country_code } from '../properties'
import { processPhoneNumber, sendBatchedTrackEvent } from '../functions'
import dayjs from '../../../lib/dayjs'
const action: ActionDefinition<Settings, Payload> = {
title: 'Track Event',
description: 'Track user events and associate it with their profile.',
defaultSubscription: 'type = "track"',
fields: {
profile: {
label: 'Profile',
description: `Properties of the profile that triggered this event.`,
type: 'object',
properties: {
email: {
label: 'Email',
type: 'string',
description: `The user's email to send to Klavio.`,
format: 'email'
},
phone_number: {
label: 'Phone Number',
type: 'string'
},
country_code: {
...country_code
},
external_id: {
label: 'External Id',
description:
'A unique identifier used by customers to associate Klaviyo profiles with profiles in an external system.',
type: 'string',
default: { '@path': '$.userId' }
},
anonymous_id: {
label: 'Anonymous Id',
description: 'Anonymous user identifier for the user.',
type: 'string',
default: { '@path': '$.anonymousId' }
}
},
additionalProperties: true,
required: true
},
metric_name: {
label: 'Metric Name',
description: 'Name of the event. Must be less than 128 characters.',
type: 'string',
default: {
'@path': '$.event'
},
required: true
},
properties: {
description: `Properties of this event.`,
label: 'Properties',
type: 'object',
default: {
'@path': '$.properties'
},
required: true
},
time: {
label: 'Time',
description: `When this event occurred. By default, the time the request was received will be used.
The time is truncated to the second. The time must be after the year 2000 and can only
be up to 1 year in the future.
`,
type: 'datetime',
default: {
'@path': '$.timestamp'
}
},
value: {
label: 'Value',
description: 'A numeric value to associate with this event. For example, the dollar amount of a purchase.',
type: 'number'
},
unique_id: {
label: 'Unique ID',
description: `A unique identifier for an event. If the unique_id is repeated for the same
profile and metric, only the first processed event will be recorded. If this is not
present, this will use the time to the second. Using the default, this limits only one
event per profile per second.
`,
type: 'string',
default: {
'@path': '$.messageId'
}
},
enable_batching: { ...enable_batching },
batch_size: { ...batch_size, default: 1000 }
},
perform: (request, { payload }) => {
const { email, phone_number: initialPhoneNumber, external_id, anonymous_id, country_code } = payload.profile
const phone_number = processPhoneNumber(initialPhoneNumber, country_code)
payload.profile.phone_number = phone_number
delete payload?.profile?.country_code
if (!email && !phone_number && !external_id && !anonymous_id) {
throw new PayloadValidationError('One of External ID, Anonymous ID, Phone Number or Email is required.')
}
const eventData = {
data: {
type: 'event',
attributes: {
properties: { ...payload.properties },
time: payload?.time ? dayjs(payload.time).toISOString() : undefined,
value: payload.value,
unique_id: payload.unique_id,
metric: {
data: {
type: 'metric',
attributes: {
name: payload.metric_name
}
}
},
profile: {
data: {
type: 'profile',
attributes: payload.profile
}
}
}
}
}
return request(`${API_URL}/events/`, {
method: 'POST',
json: eventData
})
},
performBatch: (request, { payload }) => {
return sendBatchedTrackEvent(request, payload)
}
}
export default action