-
Notifications
You must be signed in to change notification settings - Fork 28
/
handler.js
66 lines (53 loc) · 1.91 KB
/
handler.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
// Supported Objects: n/a
// Supported Events: Email lifecycle events: https://docs.leanplum.com/docs/webhooks
const isBlank = (str) => {
return (!str || /^\s*$/.test(str));
};
const mapToSegment = (eventParams) => {
try {
eventParams.properties = {};
// LP sends Unix epoch time stamps in ms
eventParams.timestamp = new Date(parseInt(eventParams.timestamp)).toISOString();
if(eventParams.abTestID) {
eventParams.event = "AB Test";
eventParams.properties.abTestID = eventParams.abTestID;
eventParams.properties.variantID = eventParams.variantID;
} else {
eventParams.event = eventParams.channel + " " + eventParams.event;
}
if(isBlank(eventParams.userId)) {
eventParams.userId = null;
}
if(!isBlank(eventParams.device_id)) {
eventParams.context = { device: { id: eventParams.device_id }};
} else {
eventParams.context = {};
}
if(!isBlank(eventParams.parameters)) {
// LP sends URL encoded JSON for the event parameters
let params = JSON.parse(decodeURIComponent(eventParams.parameters));
eventParams.properties = Object.assign(eventParams.properties, params);
}
return { type: "track",
timestamp: eventParams.timestamp,
event: eventParams.event,
userId: eventParams.userId,
context: eventParams.context,
properties: eventParams.properties };
} catch (e) {
console.log("ERROR - Could not map event properties: ", e.message);
return null;
}
};
exports.processEvents = async (event) => {
let eventBody = event.payload.body;
let eventHeaders = event.payload.headers;
let queryParameters = event.payload.queryParameters;
console.log("Start Processing.");
let returnEvent = mapToSegment(queryParameters);
let returnValue = {
events: [{ ...returnEvent}]
};
console.log("End Processing:", returnValue);
return(returnValue)
};