This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
event_factory.js
121 lines (113 loc) · 2.93 KB
/
event_factory.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
function isExperiment(flag, reason) {
if (reason) {
// If the reason says we're in an experiment, we are. Otherwise, apply
// the legacy rule exclusion logic.
if (reason.inExperiment) {
return true;
}
switch (reason.kind) {
case 'RULE_MATCH': {
const index = reason.ruleIndex;
if (index !== undefined) {
const rules = flag.rules || [];
return index >= 0 && index < rules.length && !!rules[index].trackEvents;
}
break;
}
case 'FALLTHROUGH':
return !!flag.trackEventsFallthrough;
}
}
return false;
}
function EventFactory(withReasons) {
const ef = {};
ef.newEvalEvent = (flag, context, detail, defaultVal, prereqOfFlag) => {
const addExperimentData = isExperiment(flag, detail.reason);
const e = {
kind: 'feature',
creationDate: new Date().getTime(),
key: flag.key,
context,
value: detail.value,
variation: detail.variationIndex,
default: defaultVal,
version: flag.version,
};
// the following properties are handled separately so we don't waste bandwidth on unused keys
if (addExperimentData || flag.trackEvents) {
e.trackEvents = true;
}
if (flag.debugEventsUntilDate) {
e.debugEventsUntilDate = flag.debugEventsUntilDate;
}
if (prereqOfFlag) {
e.prereqOf = prereqOfFlag.key;
}
if (addExperimentData || withReasons) {
e.reason = detail.reason;
}
return e;
};
ef.newDefaultEvent = (flag, context, detail) => {
const e = {
kind: 'feature',
creationDate: new Date().getTime(),
key: flag.key,
context,
value: detail.value,
default: detail.value,
version: flag.version,
};
// the following properties are handled separately so we don't waste bandwidth on unused keys
if (flag.trackEvents) {
e.trackEvents = true;
}
if (flag.debugEventsUntilDate) {
e.debugEventsUntilDate = flag.debugEventsUntilDate;
}
if (withReasons) {
e.reason = detail.reason;
}
return e;
};
ef.newUnknownFlagEvent = (key, context, detail) => {
const e = {
kind: 'feature',
creationDate: new Date().getTime(),
key: key,
context,
value: detail.value,
default: detail.value,
};
if (withReasons) {
e.reason = detail.reason;
}
return e;
};
ef.newIdentifyEvent = context => ({
kind: 'identify',
creationDate: new Date().getTime(),
context,
});
ef.newCustomEvent = (eventName, context, data, metricValue) => {
const e = {
kind: 'custom',
creationDate: new Date().getTime(),
key: eventName,
context,
};
if (data !== null && data !== undefined) {
e.data = data;
}
if (metricValue !== null && metricValue !== undefined) {
e.metricValue = metricValue;
}
return e;
};
return ef;
}
module.exports = {
EventFactory,
isExperiment,
};