-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.ts
More file actions
112 lines (101 loc) · 3.25 KB
/
utils.ts
File metadata and controls
112 lines (101 loc) · 3.25 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
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
import {
Client,
ComponentSettings,
MCEvent,
Manager,
} from '@managed-components/types'
import { sendEvent } from '.'
import { Settings } from 'http2'
export const flattenKeys = (obj: { [k: string]: unknown } = {}, prefix = '') =>
Object.keys(obj).reduce((acc: { [k: string]: unknown }, k) => {
const pre = prefix.length ? `${prefix}.` : ''
const value = obj[k]
if (
typeof value === 'object' &&
!Array.isArray(obj[k]) &&
value !== null &&
Object.keys(value).length > 0
) {
Object.assign(acc, flattenKeys(value as Record<string, string>, pre + k))
} else if (Array.isArray(value) && value !== null) {
value.forEach((v: unknown, i: number) => {
if (typeof v === 'object' && v !== null) {
Object.assign(
acc,
flattenKeys(v as Record<string, string>, pre + k + '.' + i)
)
} else {
acc[pre + k + '.' + i] = v
}
})
} else {
acc[pre + k] = value
}
return acc
}, {})
/**
* @param paramKey - The key that needs to be merged into original object
* @param paramValuesToUse - fallback values that `getParamSafely` will try and retrieve
* @returns object - The return value of getParamSafely must be spread to merge into another object
* @todo add test
*/
export const getParamSafely = (
paramKey: string,
paramValuesToUse: Array<string>
) => {
for (const param of paramValuesToUse) {
if (param) {
return { [paramKey]: param }
}
}
return {}
}
// pageviews in session counter
export const countPageview = (client: Client) => {
let pageviewCounter = parseInt(client.get('pageviewCounter') || '0') || 0
if (pageviewCounter === 0) {
client.set('pageviewCounter', '1', { scope: 'session' })
} else {
pageviewCounter++
client.set('pageviewCounter', `${pageviewCounter}`, { scope: 'session' })
}
}
// conversion events in session counter
export const countConversion = (event: MCEvent) => {
const { client } = event
let conversionCounter = parseInt(client.get('conversionCounter') || '0') || 0
if (conversionCounter === 0 && event.payload.conversion) {
client.set('conversionCounter', '1', { scope: 'session' })
} else {
conversionCounter++
client.set('conversionCounter', `${conversionCounter}`, {
scope: 'session',
})
}
}
export const computeEngagementDuration = (
event: MCEvent,
settings: ComponentSettings
) => {
const SESSION_DURATION_IN_MIN = settings.sessionLength || 30 // inactivity time gap between sessions (in min)
const now = Date.now()
let engagementDuration =
parseInt(event.client.get('engagementDuration') || '0') || 0
let engagementStart =
parseInt(event.client.get('engagementStart') || '0') || now
const delaySinceLast = (now - engagementStart) / 1000 / 60
// Last interaction occured in a previous session, reset engagementStart
if (delaySinceLast > SESSION_DURATION_IN_MIN) {
engagementStart = now
}
engagementDuration += now - engagementStart
event.client.set('engagementDuration', `${engagementDuration}`)
}
export const sendUserEngagementEvent = (
event: MCEvent,
settings: Settings,
manager: Manager
) => {
computeEngagementDuration(event, settings)
sendEvent('user_engagement', event, settings, manager)
}