forked from snowplow/snowplow-cpp-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.hpp
More file actions
350 lines (297 loc) · 10.9 KB
/
tracker.hpp
File metadata and controls
350 lines (297 loc) · 10.9 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
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.
*/
#ifndef TRACKER_H
#define TRACKER_H
#include <string>
#include "emitter.hpp"
#include "subject.hpp"
#include "client_session.hpp"
#include "self_describing_json.hpp"
using std::string;
using std::vector;
namespace snowplow {
/**
* @brief Singleton object that provides an interface to track Snowplow events.
*/
class Tracker {
public:
/**
* @brief Initializes and returns the singleton tracker instance.
*
* @param emitter The emitter to which events are sent (required).
* @param subject The user being tracked (optional).
* @param client_session Client session object responsible for tracking user sessions (optional). Attach a ClientSession context to each event.
* @param platform The platform the Tracker is running on, can be one of: web, mob, pc, app, srv, tv, cnsl, iot (defaults to srv).
* @param app_id Application ID (defaults to empty string).
* @param name_space The name of the tracker instance attached to every event (defaults to empty string).
* @param use_base64 Whether to enable base 64 encoding (defaults to true).
* @param desktop_context Whether to add a desktop_context, which gathers information about the device the tracker is running on, to each event (defaults to true).
* @return Tracker*
*/
static Tracker *init(Emitter &emitter, Subject *subject, ClientSession *client_session, string *platform,
string *app_id, string *name_space, bool *use_base64, bool *desktop_context);
/**
* @brief Returns the initialized singleton tracker instance.
*
* @return Tracker* Tracker instance.
*/
static Tracker *instance();
/**
* @brief Clean up the tracker to be used in main destructor.
*/
static void close();
/**
* @brief Event to capture custom consumer interactions without the need to define a custom schema.
*/
class StructuredEvent {
public:
/**
* @brief Name for the group of objects you want to track e.g. "media", "ecomm".
*/
string category; // required
/**
* @brief Defines the type of user interaction for the web object.
*
* E.g., "play-video", "add-to-basket".
*/
string action; // required
/**
* @brief Identifies the specific object being actioned.
*
* E.g., ID of the video being played, or the SKU or the product added-to-basket.
*/
string *label;
/**
* @brief Describes the object or the action performed on it.
*
* This might be the quantity of an item added to basket
*/
string *property;
/**
* @brief Quantifies or further describes the user action.
*
* This might be the price of an item added-to-basket, or the starting time of the video where play was just pressed.
*/
double *value;
/**
* @brief Unix timestamp (in ms) when the event was created. Assigned automatically.
* @deprecated Use the `true_timestamp` instead.
*/
unsigned long long timestamp;
/**
* @brief ID of the event (UUID v4) that is assigned automatically.
* @deprecated The ability to set custom event ID will be removed in the future
*/
string event_id;
/**
* @brief Optional, user-defined Unix timestamp (in ms) for the event to override the automatically assigned one.
*/
unsigned long long *true_timestamp;
/**
* @brief Context entities added to the event.
*
*/
vector<SelfDescribingJson> contexts;
/**
* @brief Construct a new Structured Event object
*
* @param category Name for the group of objects you want to track e.g. "media", "ecomm".
* @param action Defines the type of user interaction for the web object.
*/
StructuredEvent(string category, string action);
};
/**
* @brief Event to track custom information that does not fit into the out-of-the box events.
*
* Self-describing events are a [data structure based on JSON Schemas](https://docs.snowplowanalytics.com/docs/understanding-tracking-design/understanding-schemas-and-validation/)
* and can have arbitrarily many fields.
* To define your own custom self-describing event, you must create a JSON schema for that
* event and upload it to an [Iglu Schema Repository](https://github.com/snowplow/iglu) using
* [igluctl](https://docs.snowplowanalytics.com/docs/open-source-components-and-applications/iglu/)
* (or if a Snowplow BDP customer, you can use the
* [Snowplow BDP Console UI](https://docs.snowplowanalytics.com/docs/understanding-tracking-design/managing-data-structures/)
* or [Data Structures API](https://docs.snowplowanalytics.com/docs/understanding-tracking-design/managing-data-structures-via-the-api-2/)).
* Snowplow uses the schema to validate that the JSON containing the event properties is well-formed.
*/
class SelfDescribingEvent {
public:
/**
* @brief Main properties of the self-describing event including it's schema and body
*/
SelfDescribingJson event; // required
/**
* @brief Unix timestamp (in ms) when the event was created. Assigned automatically.
* @deprecated Use the `true_timestamp` instead.
*/
unsigned long long timestamp;
/**
* @brief ID of the event (UUID v4) that is assigned automatically.
* @deprecated The ability to set custom event ID will be removed in the future
*/
string event_id;
/**
* @brief Optional, user-defined Unix timestamp (in ms) for the event to override the automatically assigned one.
*/
unsigned long long *true_timestamp;
/**
* @brief Context entities added to the event.
*/
vector<SelfDescribingJson> contexts;
/**
* @brief Construct a new Self Describing Event object
*
* @param event Main properties of the self-describing event including it's schema and body
*/
SelfDescribingEvent(SelfDescribingJson event);
};
/**
* @brief Event to track user viewing a screen within the application.
*
* Schema for the event: iglu:com.snowplowanalytics.snowplow/screen_view/jsonschema/1-0-0
*/
class ScreenViewEvent {
public:
/**
* @brief The name of the screen viewed.
*/
string *name;
/**
* @brief The id of screen that was viewed.
*/
string *id;
/**
* @brief Unix timestamp (in ms) when the event was created. Assigned automatically.
* @deprecated Use the `true_timestamp` instead.
*/
unsigned long long timestamp;
/**
* @brief ID of the event (UUID v4) that is assigned automatically.
* @deprecated The ability to set custom event ID will be removed in the future
*/
string event_id;
/**
* @brief Optional, user-defined Unix timestamp (in ms) for the event to override the automatically assigned one.
*/
unsigned long long *true_timestamp;
/**
* @brief Context entities added to the event.
*/
vector<SelfDescribingJson> contexts;
/**
* @brief Construct a new Screen View Event object
*/
ScreenViewEvent();
};
/**
* @brief Event used to track user timing events such as how long resources take to load.
*
* Schema: iglu:com.snowplowanalytics.snowplow/timing/jsonschema/1-0-0
*/
class TimingEvent {
public:
/**
* @brief Defines the timing category.
*/
string category; // required
/**
* @brief Defines the timing variable measured.
*/
string variable; // required
/**
* @brief Represents the time.
*/
unsigned long long timing; // required
/**
* @brief An optional string to further identify the timing event.
*/
string *label;
/**
* @brief Unix timestamp (in ms) when the event was created. Assigned automatically.
* @deprecated Use the `true_timestamp` instead.
*/
unsigned long long timestamp;
/**
* @brief ID of the event (UUID v4) that is assigned automatically.
* @deprecated The ability to set custom event ID will be removed in the future
*/
string event_id;
/**
* @brief Optional, user-defined Unix timestamp (in ms) for the event to override the automatically assigned one.
*/
unsigned long long *true_timestamp;
/**
* @brief Context entities added to the event.
*
*/
vector<SelfDescribingJson> contexts;
/**
* @brief Construct a new Timing Event object
*
* @param category Defines the timing category.
* @param variable Defines the timing variable measured.
* @param timing Represents the time.
*/
TimingEvent(string category, string variable, unsigned long long timing);
};
void start();
void stop();
/**
* @brief Used to ensure all of your events are sent before closing your application.
* It is a blocking call that will send everything in the database and then will join the daemon thread to the calling thread.
*/
void flush();
/**
* @brief Set the optional subject object after tracker initialization.
*
* @param subject Instance of subject
*/
void set_subject(Subject *subject);
/**
* @brief Track en event with custom payload. We do not recommend using this function. Instead, track events using the predefined functions for each event.
*
* @param p Event payload
* @param event_id ID of the event
* @param contexts Vector of custom contexts
*/
void track(Payload p, const string & event_id, vector<SelfDescribingJson> &contexts);
/**
* @brief Track a Snowplow custom structured event which fits the Google Analytics-style structure of having up to five fields.
*/
void track_struct_event(StructuredEvent);
/**
* @brief Track the user viewing a screen within the application.
*/
void track_screen_view(ScreenViewEvent);
/**
* @brief Track a timing event.
*/
void track_timing(TimingEvent);
/**
* @brief Track a Snowplow custom unstructured event.
*/
void track_self_describing_event(SelfDescribingEvent);
private:
static Tracker *m_instance;
static mutex m_tracker_get;
Tracker(Emitter &emitter, Subject *subject, ClientSession *client_session, string *platform,
string *app_id, string *name_space, bool *use_base64, bool *desktop_context);
~Tracker();
Emitter &m_emitter;
Subject *m_subject;
ClientSession *m_client_session;
string m_namespace;
string m_app_id;
string m_platform;
bool m_use_base64;
bool m_desktop_context;
};
}
#endif