-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathamplitude.js
More file actions
197 lines (177 loc) · 3.88 KB
/
amplitude.js
File metadata and controls
197 lines (177 loc) · 3.88 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"use strict";
const Log = require('./log');
const Amplitude = require('amplitude');
const nanoid = require('nanoid');
const TRACKED_MESSAGES = [
// General
'Workspace Install',
'Fight End',
'Profile',
'Equipment Purchased',
'Premium Purchase',
'Train Stat',
'Train Skill',
'Train Mastery',
'Improve Mastery',
'Purchase Scales',
'Cursed Chest',
'Crier Message Purchased',
'Newsletter Signup',
// Tyrose
'Find Apprentice',
'Discover Lair',
'Find Pet Collar',
'Find Ringmakers Tools',
'Discover Cave',
'Green Dragon Killed',
// Scatterslide
'Explode Quarry Entrance',
'Repair Mine Elevator',
'Unlock Underdrift Door',
'Rebuild Scatterslide Blacksmith',
'Rebuild Scatterslide Artificer',
'Brown Dragon Killed',
// Watermoon - Rumble
'Shadow Lesser Defeated',
'Drunken Master Defeated',
'Jackie Mann Defeated',
'Shadow Greater Defeated',
'Red Dragon Killed',
// Watermoon - Scholar
'Empusa Defeated',
'Gorgon Defeated',
'Minotaur Defeated',
'Black Dragon Killed',
// Watermoon - Mystic
'Portal Opened',
'Open Catacombs',
'Blackpool Prime Defeated',
'Tentacled Demigod Defeated',
'Grand Lich Defeated',
'Necrodragon Killed',
];
const CUSTOM_EVENTS = {
'Profile': 'formatProfileEventData',
'Workspace Install': 'formatWorkspaceInstallEventData',
};
const CUSTOM_ACTIONS = {
'Profile': 'identify',
};
class Tracker
{
constructor(apiToken, trackedMessages = TRACKED_MESSAGES, customEvents = CUSTOM_EVENTS, customActions = CUSTOM_ACTIONS)
{
this.amplitude = new Amplitude(apiToken);
this.trackedMessages = trackedMessages;
this.customEvents = customEvents;
this.customActions = customActions;
}
/**
* Only certain events are reported.
*
* @param {string} event - The event to check.
*
* @return {boolean}
*/
isEventTracked(event)
{
return this.trackedMessages.includes(event);
}
/**
* Certain events generate their own custom options.
*
* @param {string} event - The event to check.
*
* @return {boolean}
*/
hasCustomOptions(event)
{
return ! _.isUndefined(this.customEvents[event]);
}
/**
* Certain events aren't tracked, but have a custom action.
*
* @param {string} even - The even to check.
*
* @return {boolean}
*/
hasCustomAction(event)
{
return ! _.isUndefined(this.customActions[event]);
}
/**
* Track a message with Calc.io.
*
* @param {object} message - The message to track.
*/
async track(message)
{
if ( ! this.isEventTracked(message.event)) {
return;
}
try {
const data = this.hasCustomOptions(message.event)
? this[this.customEvents[message.event]](message)
: this.formatEventData(message);
if (this.hasCustomAction(message.event)) {
await this.amplitude[this.customActions[message.event]](data);
}
else {
await this.amplitude.track(data);
}
}
catch (e) {
Log.error(e);
}
}
/**
* Format event data in a way that Amplitude is expecting.
*
* @param {object} message - The message to convert into Amplitude data format.
*
* @return {object}
*/
formatEventData(message)
{
return {
event_type: message.event,
user_id: message.character_id,
event_properties: message.fields
};
}
/**
* Format profile event data in a way that Amplitude is expecting.
*
* @param {object} message - The message to convert into request options.
*
* @return {object}
*/
formatProfileEventData(message)
{
return {
user_id: message.character_id,
user_properties: {
name: message.fields.name,
email: message.fields.email,
},
};
}
/**
* Format Workspace Install event data in a way that Amplitude is expecting.
*
* @param {object} message - The message to convert into request options.
*
* @return {object}
*/
formatWorkspaceInstallEventData(message)
{
return {
event_type: message.event,
user_id: nanoid(),
event_properties: {
team_name: message.fields.name,
},
};
}
}
module.exports = Tracker;