forked from rudderlabs/sample-user-transformers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelective_Event_Removal_And_Value_Aggregation_User_Transformation.js
77 lines (61 loc) · 2.61 KB
/
Selective_Event_Removal_And_Value_Aggregation_User_Transformation.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
function transform(events) {
const filterEventNames = [
// Add list of event names that you want to filter out
"game_load_time",
"lobby_fps"
];
//remove events whose name match those in above list
const filteredEvents = events.filter(event => {
const eventName = event.event;
return !(eventName && filterEventNames.includes(eventName));
});
//remove events of a certain type if related property value does not satisfy pre-defined condition
//in this example, if 'total_payment' for a 'spin' event is null or 0, then it would be removed.
//Only non-null, non-zero 'spin' events would be considered
const nonSpinAndSpinPayerEvents = filteredEvents.filter( event => {
const eventName = event.event;
// spin events
if(eventName.toLowerCase().indexOf('spin') >= 0) {
if(event.userProperties && event.userProperties.total_payments && event.userProperties.total_payments > 0) {
return true;
} else {
return false;
}
} else {
return true;
}
});
//in the below example, aggregation of three attributes - 'bet_amount', 'win_amount' and 'no_of_spin'
//for all 'spin_result' events in the batch is being performed and instead of N 'spin_result' events,
//a single 'spin_result' event with cumulative values is being provided
let spin_result_events = nonSpinAndSpinPayerEvents.filter(event => {
return event.event == "spin_result";
});
let bet_amount = 0;
let win_amount = 0;
let no_of_spin = 0;
// sum these props
spin_result_events.forEach(spEvent => {
bet_amount += spEvent.properties.bet_amount ? spEvent.properties.bet_amount : 0;
win_amount += spEvent.properties.win_amount ? spEvent.properties.win_amount : 0;
no_of_spin += spEvent.properties.no_of_spin ? spEvent.properties.no_of_spin : 0;
})
// modify the first spin_result_event
if(spin_result_events.length > 0) {
spin_result_events[0].properties.bet_amount = bet_amount;
spin_result_events[0].properties.win_amount = win_amount;
spin_result_events[0].properties.no_of_spin = no_of_spin;
}
// other than spin event
let otherEvents = nonSpinAndSpinPayerEvents.filter(event => {
return event.event != "spin_result";
});
if(otherEvents.length === 0) {
otherEvents = [];
}
// add a single spin event
if(spin_result_events.length > 0) {
otherEvents.push(spin_result_events[0]);
}
return nonSpinAndSpinPayerEvents;
}