forked from snowplow/snowplow-cpp-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.cpp
More file actions
272 lines (217 loc) · 7.77 KB
/
tracker.cpp
File metadata and controls
272 lines (217 loc) · 7.77 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Copyright (c) 2022 Snowplow Analytics Ltd. All rights reserved.
This program is licensed to you under the Apache License Version 2.0,
and you may not use this file except in compliance with the Apache License Version 2.0.
You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing,
software distributed under the Apache License Version 2.0 is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
#include "tracker.hpp"
using namespace snowplow;
using std::invalid_argument;
using std::lock_guard;
using std::runtime_error;
using std::to_string;
// --- Static Singleton Access
Tracker *Tracker::m_instance = 0;
mutex Tracker::m_tracker_get;
Tracker *Tracker::init(Emitter &emitter, Subject *subject, ClientSession *client_session, string *platform, string *app_id,
string *name_space, bool *use_base64, bool *desktop_context) {
lock_guard<mutex> guard(m_tracker_get);
if (!m_instance) {
m_instance = new Tracker(emitter, subject, client_session, platform, app_id, name_space, use_base64, desktop_context);
}
return m_instance;
}
Tracker *Tracker::instance() {
lock_guard<mutex> guard(m_tracker_get);
if (!m_instance) {
throw runtime_error("FATAL: Tracker must be initialized first.");
}
return m_instance;
}
void Tracker::close() {
lock_guard<mutex> guard(m_tracker_get);
if (m_instance) {
delete (m_instance);
}
m_instance = 0;
}
// --- Constructor & Destructor
Tracker::Tracker(Emitter &emitter, Subject *subject, ClientSession *client_session, string *platform, string *app_id,
string *name_space, bool *use_base64, bool *desktop_context) : m_emitter(emitter), m_client_session(client_session) {
this->m_subject = subject;
this->m_platform = (platform != NULL ? *platform : "srv");
this->m_app_id = (app_id != NULL ? *app_id : "");
this->m_namespace = (name_space != NULL ? *name_space : "");
this->m_use_base64 = (use_base64 != NULL ? *use_base64 : true);
this->m_desktop_context = (desktop_context != NULL ? *desktop_context : true);
// Start daemon threads
this->start();
}
Tracker::~Tracker() {
this->stop();
}
// --- Controls
void Tracker::start() {
this->m_emitter.start();
}
void Tracker::stop() {
this->m_emitter.stop();
}
void Tracker::flush() {
this->m_emitter.flush();
}
// --- Setters
void Tracker::set_subject(Subject *subject) {
this->m_subject = subject;
}
// --- Event Tracking
void Tracker::track(Payload payload, const string &event_id, vector<SelfDescribingJson> &contexts) {
// Add standard KV Pairs
payload.add(SNOWPLOW_TRACKER_VERSION, SNOWPLOW_TRACKER_VERSION_LABEL);
payload.add(SNOWPLOW_PLATFORM, this->m_platform);
payload.add(SNOWPLOW_APP_ID, this->m_app_id);
payload.add(SNOWPLOW_SP_NAMESPACE, this->m_namespace);
// Add Subject KV Pairs
if (this->m_subject != NULL) {
payload.add_map(this->m_subject->get_map());
}
// Add Client Session if available
if (this->m_client_session) {
contexts.push_back(this->m_client_session->update_and_get_session_context(event_id));
}
// Add Desktop Context if available
if (this->m_desktop_context) {
contexts.push_back(Utils::get_desktop_context());
}
// Build the final context and add it to the payload
if (contexts.size() > 0) {
json context_data_array;
for (int i = 0; i < contexts.size(); ++i) {
context_data_array.push_back(contexts[i].get());
}
SelfDescribingJson context_json(SNOWPLOW_SCHEMA_CONTEXTS, context_data_array);
payload.add_json(context_json.get(), m_use_base64, SNOWPLOW_CONTEXT_ENCODED, SNOWPLOW_CONTEXT);
}
// Add the event to the Emitter
this->m_emitter.add(payload);
}
void Tracker::track_struct_event(StructuredEvent se) {
if (se.action == "") {
throw invalid_argument("Action is required");
}
if (se.category == "") {
throw invalid_argument("Category is required");
}
Payload p;
p.add(SNOWPLOW_EVENT, SNOWPLOW_EVENT_STRUCTURED);
p.add(SNOWPLOW_SE_ACTION, se.action);
p.add(SNOWPLOW_SE_CATEGORY, se.category);
if (se.label != NULL) {
p.add(SNOWPLOW_SE_LABEL, *se.label);
}
if (se.property != NULL) {
p.add(SNOWPLOW_SE_PROPERTY, *se.property);
}
if (se.value != NULL) {
p.add(SNOWPLOW_SE_VALUE, to_string(*se.value));
}
p.add(SNOWPLOW_TIMESTAMP, to_string(se.timestamp));
p.add(SNOWPLOW_EID, se.event_id);
if (se.true_timestamp != NULL) {
p.add(SNOWPLOW_TRUE_TIMESTAMP, to_string(*se.true_timestamp));
}
track(p, se.event_id, se.contexts);
}
void Tracker::track_screen_view(Tracker::ScreenViewEvent sve) {
if (sve.name == NULL && sve.id == NULL) {
throw invalid_argument("Either name or id field must be set");
}
json data;
if (sve.id != NULL) {
data[SNOWPLOW_SV_ID] = *sve.id;
}
if (sve.name != NULL) {
data[SNOWPLOW_SV_NAME] = *sve.name;
}
SelfDescribingJson sdj = SelfDescribingJson(SNOWPLOW_SCHEMA_SCREEN_VIEW, data);
SelfDescribingEvent sde(sdj);
sde.event_id = sve.event_id;
sde.timestamp = sve.timestamp;
sde.true_timestamp = sve.true_timestamp;
sde.contexts = sve.contexts;
track_self_describing_event(sde);
}
void Tracker::track_timing(TimingEvent te) {
if (te.category == "") {
throw invalid_argument("Category is required");
}
if (te.variable == "") {
throw invalid_argument("Variable is required");
}
json data;
data[SNOWPLOW_UT_CATEGORY] = te.category;
data[SNOWPLOW_UT_VARIABLE] = te.variable;
data[SNOWPLOW_UT_TIMING] = te.timing;
if (te.label != NULL) {
data[SNOWPLOW_UT_LABEL] = *te.label;
}
SelfDescribingJson sdj = SelfDescribingJson(SNOWPLOW_SCHEMA_USER_TIMINGS, data);
SelfDescribingEvent sde(sdj);
sde.event_id = te.event_id;
sde.timestamp = te.timestamp;
sde.true_timestamp = te.true_timestamp;
sde.contexts = te.contexts;
track_self_describing_event(sde);
}
void Tracker::track_self_describing_event(SelfDescribingEvent sde) {
Payload p;
p.add(SNOWPLOW_EVENT, SNOWPLOW_EVENT_UNSTRUCTURED);
p.add(SNOWPLOW_TIMESTAMP, to_string(sde.timestamp));
p.add(SNOWPLOW_EID, sde.event_id);
SelfDescribingJson sdj(SNOWPLOW_SCHEMA_UNSTRUCT_EVENT, sde.event.get());
p.add_json(sdj.get(), this->m_use_base64, SNOWPLOW_UNSTRUCTURED_ENCODED, SNOWPLOW_UNSTRUCTURED);
if (sde.true_timestamp != NULL) {
p.add(SNOWPLOW_TRUE_TIMESTAMP, to_string(*sde.true_timestamp));
}
track(p, sde.event_id, sde.contexts);
}
// --- Event Builders
Tracker::StructuredEvent::StructuredEvent(string category, string action) {
this->category = category;
this->action = action;
this->contexts = vector<SelfDescribingJson>();
this->event_id = Utils::get_uuid4();
this->timestamp = Utils::get_unix_epoch_ms();
this->true_timestamp = NULL;
this->label = NULL;
this->property = NULL;
this->value = NULL;
}
Tracker::SelfDescribingEvent::SelfDescribingEvent(SelfDescribingJson event) : event(event) {
this->event_id = Utils::get_uuid4();
this->timestamp = Utils::get_unix_epoch_ms();
this->true_timestamp = NULL;
this->contexts = vector<SelfDescribingJson>();
}
Tracker::ScreenViewEvent::ScreenViewEvent() {
this->contexts = vector<SelfDescribingJson>();
this->event_id = Utils::get_uuid4();
this->timestamp = Utils::get_unix_epoch_ms();
this->true_timestamp = NULL;
this->id = NULL;
this->name = NULL;
}
Tracker::TimingEvent::TimingEvent(string category, string variable, unsigned long long timing) {
this->category = category;
this->variable = variable;
this->timestamp = Utils::get_unix_epoch_ms();
this->true_timestamp = NULL;
this->timing = timing;
this->contexts = vector<SelfDescribingJson>();
this->event_id = Utils::get_uuid4();
this->label = NULL;
}