-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLastVisitedDetails.js
74 lines (65 loc) · 3.05 KB
/
getLastVisitedDetails.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
const getVisitsWhereEventsNameContains = (visitResults, eventNameMatch) => {
return visitResults.filter((visit) => findEventByEventNameMatch(visit.actionDetails, eventNameMatch));
}
const findEventByEventNameMatch = (actions, eventNameMatch) => {
const regex = new RegExp(eventNameMatch,"gm");
return actions.find((action) => action.type == "event" && action.eventName.match(regex))
}
const findEarlierVisitLogs = (visitLogs, sourceVisitLogs) => {
return visitLogs.map((visitLog) => {
const earlierActionDetails= sourceVisitLogs.filter((sourceVisitLog) => {
return sourceVisitLog.visitorId === visitLog.visitorId &&
sourceVisitLog.lastActionTimestamp <= visitLog.firstActionTimestamp
})
.map((earlierVisitLog) => earlierVisitLog.actionDetails)
.flat();
return {
...visitLog,
actionDetails: earlierActionDetails.concat(visitLog.actionDetails)
}
})
}
const getClosestEarlierAction = (actions, targetAction) => {
return actions.reduce((closestAction, action) => {
return (action.timestamp > closestAction.timestamp) && (action.timestamp <= targetAction.timestamp)
&& targetAction.pageIdAction !== action.pageIdAction ?
action :
closestAction;
}, {timestamp:0})
}
const getPageActionForEvent = (actions, eventAction) => {
const actionsMatchingUrl = actions.filter((action) => action.type === 'action' && action.url && action.url === eventAction.url)
return getClosestEarlierAction(actionsMatchingUrl, eventAction)
}
const getPreviousPageActionForAction = (actions, eventPageAction) => {
const pageActionsWithTimeSpentProp = actions.filter((action) => action.url && action.type === "action" && action.timeSpent)
return getClosestEarlierAction(pageActionsWithTimeSpentProp, eventPageAction)
}
const convertToMinimalAction = (action) => {
const props = ["type","url", "pageTitle', timeSpent", "timeSpentPretty", "eventCategory", "eventAction", "eventName", "eventValue", "timestamp"];
return props.reduce((obj, prop) => {
return action[prop] ?
{...obj, [prop]: action[prop]} :
obj;
}, {});
}
const getPreviousPageWithTimeSpentForVisitLog = (visitLog, eventNameMatch) => {
const eventAction = findEventByEventNameMatch(visitLog.actionDetails, eventNameMatch)
const eventPageAction = getPageActionForEvent(visitLog.actionDetails, eventAction)
const eventPreviousPageAction = getPreviousPageActionForAction(visitLog.actionDetails, eventPageAction)
return {
visitorId: visitLog.visitorId,
eventAction: convertToMinimalAction(eventAction),
eventPageAction: convertToMinimalAction(eventPageAction),
eventPreviousPageAction: convertToMinimalAction(eventPreviousPageAction),
}
}
module.exports = {
getVisitsWhereEventsNameContains,
findEarlierVisitLogs,
getPreviousPageWithTimeSpentForVisitLog,
getClosestEarlierAction,
getPageActionForEvent,
getPreviousPageActionForAction,
convertToMinimalAction
}