-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
252 lines (211 loc) · 7.91 KB
/
index.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const alias = {
userId: 'properties.alias',
previousId: ['properties.distinct_id'],
}
const page = {
name: 'properties.name',
'properties.category': 'properties.category',
'properties.host': 'properties.$host',
'properties.url': 'properties.$current_url',
'properties.path': 'properties.$pathname',
'properties.referrer': 'properties.$referrer',
'properties.initial_referrer': 'properties.$initial_referrer',
'properties.referring_domain': 'properties.$referring_domain',
'properties.initial_referring_domain': 'properties.$initial_referring_domain',
}
const identify = {
'context.traits': '$set',
traits: '$set',
}
const group = {
groupId: 'groupId',
traits: 'traits',
}
const track = {
event: 'event',
}
const constants = {
'context.app.name': 'PostHogPlugin',
channel: 's2s',
}
const generic = {
'context.os.name': 'properties.$os',
'context.browser': 'properties.$browser',
'context.page.host': 'properties.$host',
'context.page.url': 'properties.$current_url',
'context.page.path': 'properties.$pathname',
'context.page.referrer': 'properties.$referrer',
'context.page.initial_referrer': 'properties.$initial_referrer',
'context.page.referring_domain': 'properties.$referring_domain',
'context.app.version': 'properties.posthog_version',
'context.page.initial_referring_domain': 'properties.$initial_referring_domain',
'context.browser_version': 'properties.$browser_version',
'context.screen.height': 'properties.$screen_height',
'context.screen.width': 'properties.$screen_width',
'context.library.name': 'properties.$lib',
'context.library.version': 'properties.$lib_version',
'context.ip': 'ip',
messageId: '$insert_id',
originalTimestamp: 'timestamp',
userId: ['$user_id', 'distinct_id'],
anonymousId: ['properties.$anon_distinct_id', 'properties.$device_id', 'properties.distinct_id'],
'context.active_feature_flags': 'properties.$active_feature_flags',
'context.posthog_version': 'properties.posthog_version',
'context.has_slack_webhook': 'properties.has_slack_webhook',
'context.token': 'properties.token',
}
const autoCapture = {
event: 'properties.$event_type',
'properties.elements': 'properties.$elements',
}
const eventToMapping = {
$identify: { type: 'identify', mapping: identify },
$create_alias: { type: 'alias', mapping: alias },
$pageview: { type: 'page', mapping: page },
$page: { type: 'page', mapping: page },
$group: { type: 'group', mapping: group },
$autocapture: { type: 'track', mapping: autoCapture },
default: { type: 'track', mapping: track },
}
function set(target, path, value) {
let keys = path.split('.')
let len = keys.length
for (let i = 0; i < len; i++) {
let prop = keys[i]
if (!isObject(target[prop])) {
target[prop] = {}
}
if (i === len - 1) {
target[prop] = value
break
}
target = target[prop]
}
}
function isObject(val) {
return val !== null && (typeof val === 'object' || typeof val === 'function')
}
function get(target, path) {
let keys = path.split('.')
let len = keys.length
for (let i = 0; i < len; i++) {
let prop = keys[i]
if (target[prop] !== undefined) {
target = target[prop]
} else {
return undefined
}
}
return target
}
export async function setupPlugin({ config, global }) {
global.laudAuthHeader = {
headers: {
authorization: `Basic ${config.writeKey}`,
PublicWriteKey: `Basic ${config.writeKey}`,
},
}
global.writeKey = config.writeKey
global.dataPlaneUrl = config.dataPlaneUrl
}
function getElementByOrderZero(json) {
if (!json.elements || !Array.isArray(json.elements)) {
return null
}
return json.elements.find(x => x.order === 0) || null
}
export async function composeWebhook(event, { config, global }) {
let dittoFeedPayload = {
userId: '', // userId should be outside traits
messageId: '', // messageId should be outside traits
event: '', // event should be outside traits
traits: {} // all other properties go inside traits
};
// Initialize plugin configuration
await setupPlugin({ config, global });
// Add constants to traits
constructPayload(dittoFeedPayload.traits, event, constants, true);
// Add generic properties to traits
constructPayload(dittoFeedPayload.traits, event, generic);
// Get specific event properties
const eventName = get(event, 'event');
const { type, mapping } = eventToMapping[eventName] ? eventToMapping[eventName] : eventToMapping['default'];
// Set DittoFeed payload type (event)
dittoFeedPayload.event = type || eventName;
// Set event properties in traits
constructPayload(dittoFeedPayload.traits, event, mapping);
// Add all PostHog properties not starting with $ to traits
Object.keys(event.properties).forEach((propKey) => {
if (propKey.slice(0, 1) != '$' && event.properties[propKey] != undefined && event.properties[propKey] != null) {
set(dittoFeedPayload.traits, propKey, event.properties[propKey]);
}
});
// Check for userId and set it if found
if (!dittoFeedPayload.userId && ("user_id" in event.properties)) {
dittoFeedPayload.userId = event.properties["user_id"];
} else if (!dittoFeedPayload.userId && ("distinct_id" in event.properties)) {
dittoFeedPayload.userId = event.properties["distinct_id"];
}
// Check for messageId and set it if found
if (!dittoFeedPayload.messageId && ("$insert_id" in event.properties)) {
dittoFeedPayload.messageId = event.properties["$insert_id"];
}
// Set the event name if not set
if (!dittoFeedPayload.event && "event" in event) {
dittoFeedPayload.event = event["event"];
}
// // Add top-level element if there is a click, change, or submit event
// if (['click', 'change', 'submit'].includes(dittoFeedPayload.event)) {
// dittoFeedPayload.traits['elements'] = [getElementByOrderZero(event)];
// }
if (dittoFeedPayload.traits.event){dittoFeedPayload.event = dittoFeedPayload.traits.event}
// Handle user set traits
const userSet = get(event, 'properties.$set');
if (userSet) {
if (config.phEmail) {
set(dittoFeedPayload.traits, 'phEmail', userSet[config.phEmail]);
}
if (config.phPhoneNumber) {
set(dittoFeedPayload.traits, 'phPhoneNumber', userSet[config.phPhoneNumber]);
}
if (config.phDeviceToken) {
set(dittoFeedPayload.traits, 'phDeviceToken', userSet[config.phDeviceToken]);
}
if (config.phCustom) {
set(dittoFeedPayload.traits, 'phCustom', userSet[config.phCustom]);
}
}
// Add timestamp for sentAt
dittoFeedPayload["sentAt"] = new Date().toISOString();
// Return the constructed request
return {
url: global.dataPlaneUrl,
headers: {
'Content-Type': 'application/json',
...global.laudAuthHeader.headers,
},
body: JSON.stringify(dittoFeedPayload),
method: 'POST',
};
}
function constructPayload(outPayload, inPayload, mapping, direct = false) {
Object.keys(mapping).forEach((dittoFeedKeyPath) => {
let pHKeyPath = mapping[dittoFeedKeyPath]
let pHKeyVal = undefined
if (direct) {
pHKeyVal = pHKeyPath
} else if (Array.isArray(pHKeyPath)) {
for (let i = 0; i < pHKeyPath.length; i++) {
pHKeyVal = get(inPayload, pHKeyPath[i])
if (pHKeyVal) {
break
}
}
} else {
pHKeyVal = get(inPayload, pHKeyPath)
}
if (pHKeyVal != undefined && pHKeyVal != null) {
set(outPayload, dittoFeedKeyPath, pHKeyVal)
}
})
}