forked from Countly/countly-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountly.hpp
290 lines (203 loc) · 7.74 KB
/
countly.hpp
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
#ifndef COUNTLY_HPP_
#define COUNTLY_HPP_
#include <iterator>
#include <chrono>
#include <string>
#include <thread>
#include <mutex>
#include <map>
#ifndef COUNTLY_USE_SQLITE
#include <deque>
#endif
#include "nlohmann/json.hpp"
using json = nlohmann::json;
#ifdef _WIN32
#undef ERROR
#endif
#define COUNTLY_SDK_NAME "c-native-unknown"
#define COUNTLY_SDK_VERSION "0.1.0"
#define COUNTLY_API_VERSION "21.11.2"
#define COUNTLY_POST_THRESHOLD 2000
#define COUNTLY_KEEPALIVE_INTERVAL 3000
#define COUNTLY_MAX_EVENTS_DEFAULT 200
class Countly {
public:
Countly();
~Countly();
static Countly& getInstance();
// Do not implicitly generate the copy constructor, this is a singleton.
Countly(const Countly&) = delete;
// Do not implicitly generate the copy assignment operator, this is a singleton.
void operator=(const Countly&) = delete;
void alwaysUsePost(bool value);
//void setSalt(const std::string& value);
enum LogLevel {DEBUG, INFO, WARNING, ERROR, FATAL};
void setLogger(void (*fun)(LogLevel level, const std::string& message));
struct HTTPResponse {
bool success;
json data;
};
void setHTTPClient(HTTPResponse (*fun)(bool use_post, const std::string& url, const std::string& data));
void setMetrics(const std::string& os, const std::string& os_version, const std::string& device, const std::string& resolution, const std::string& carrier, const std::string& app_version);
void setUserDetails(const std::map<std::string, std::string>& value);
void setCustomUserDetails(const std::map<std::string, std::string>& value);
/*
setCountry is deprecated, please use void `setLocation(const std::string& countryCode, const std::string& city, const std::string& gpsCoordinates, const std::string& ipAddress)` function instead.
*/
void setCountry(const std::string& country_code);
/*
setCity is deprecated, please use void `setLocation(const std::string& countryCode, const std::string& city, const std::string& gpsCoordinates, const std::string& ipAddress)` function instead.
*/
void setCity(const std::string& city_name);
/*
setLocation is deprecated, please use void `setLocation(const std::string& countryCode, const std::string& city, const std::string& gpsCoordinates, const std::string& ipAddress)` function instead.
*/
void setLocation(double lattitude, double longitude);
/*
Set Country code(ISO Country code), City, Location and IP address to be used for future requests.
@param[in] countryCode: ISO Country code for the user's country
@param[in] city: Name of the user's city.
@param[in] gpsCoordinates: Comma separate latitude and longitude values.For example, `56.42345,123.45325`.
@param[in] ipAddress: IpAddress like `192.168.88.33`
*/
void setLocation(const std::string& countryCode, const std::string& city, const std::string& gpsCoordinates, const std::string& ipAddress);
void setDeviceID(const std::string& value, bool same_user = false);
void start(const std::string& app_key, const std::string& host, int port = -1, bool start_thread = false);
void startOnCloud(const std::string& app_key);
void stop();
void setUpdateInterval(size_t milliseconds);
class Event;
void addEvent(const Event& event);
void setMaxEvents(size_t value);
void flushEvents(std::chrono::seconds timeout = std::chrono::seconds(30));
bool beginSession();
bool updateSession();
bool endSession();
void enableRemoteConfig();
void updateRemoteConfig();
json getRemoteConfigValue(const std::string& key);
void updateRemoteConfigFor(std::string *keys, size_t key_count);
void updateRemoteConfigExcept(std::string *keys, size_t key_count);
static std::chrono::system_clock::time_point getTimestamp();
static std::string encodeURL(const std::string& data);
static std::string serializeForm(const std::map<std::string, std::string> data);
//static std::string calculateChecksum(const std::string& salt, const std::string& data);
#ifdef COUNTLY_USE_SQLITE
void setDatabasePath(const std::string& path);
#endif
class Event {
public:
Event(const std::string& key, size_t count = 1);
Event(const std::string& key, size_t count, double sum);
Event(const std::string& key, size_t count, double sum, double duration);
void setTimestamp();
void startTimer();
void stopTimer();
template<typename T>
void addSegmentation(const std::string& key, T value) {
if (object.find("segmentation") == object.end()) {
object["segmentation"] = json::object();
}
object["segmentation"][key] = value;
}
std::string serialize() const;
private:
json object;
bool timer_running;
std::chrono::system_clock::time_point timestamp;
};
void SetPath(const std::string& path) {
#ifdef COUNTLY_USE_SQLITE
setDatabasePath(path);
#endif
}
void SetMetrics(const std::string& os, const std::string& os_version, const std::string& device, const std::string& resolution, const std::string& carrier, const std::string& app_version) {
setMetrics(os, os_version, device, resolution, carrier, app_version);
}
void SetMaxEventsPerMessage(int maxEvents) {
setMaxEvents(maxEvents);
}
void SetMinUpdatePeriod(int minUpdateMillis) {
setUpdateInterval(minUpdateMillis);
}
void Start(const std::string& appKey, const std::string& host, int port) {
start(appKey, host, port);
}
void StartOnCloud(const std::string& appKey) {
startOnCloud(appKey);
}
void Stop() {
stop();
}
void RecordEvent(const std::string key, int count) {
Event ev(key, count);
ev.setTimestamp();
addEvent(ev);
}
void RecordEvent(const std::string key, int count, double sum) {
Event ev(key, count, sum);
addEvent(ev);
}
void RecordEvent(const std::string key, std::map<std::string, std::string> segmentation, int count) {
Event event(key, count);
for (auto key_value: segmentation) {
event.addSegmentation(key_value.first, key_value.second);
}
addEvent(event);
}
void RecordEvent(const std::string key, std::map<std::string, std::string> segmentation, int count, double sum) {
Event event(key, count, sum);
for (auto key_value: segmentation) {
event.addSegmentation(key_value.first, key_value.second);
}
addEvent(event);
}
void RecordEvent(const std::string key, std::map<std::string, std::string> segmentation, int count, double sum, double duration) {
Event event(key, count, sum, duration);
for (auto key_value: segmentation) {
event.addSegmentation(key_value.first, key_value.second);
}
addEvent(event);
}
/* Provide 'updateInterval' in seconds. */
inline void setAutomaticSessionUpdateInterval(unsigned short updateInterval) {
_auto_session_update_interval = updateInterval;
}
private:
void _deleteThread();
void _sendIndependantLocationRequest();
void log(LogLevel level, const std::string& message);
HTTPResponse sendHTTP(std::string path, std::string data);
void _changeDeviceIdWithMerge(const std::string& value);
void _changeDeviceIdWithoutMerge(const std::string& value);
std::chrono::system_clock::duration getSessionDuration(std::chrono::system_clock::time_point now);
std::chrono::system_clock::duration getSessionDuration();
void updateLoop();
void (*logger_function)(LogLevel level, const std::string& message);
HTTPResponse (*http_client_function)(bool is_post, const std::string& url, const std::string& data);
std::string host;
int port;
bool use_https;
bool always_use_post;
bool began_session;
bool is_being_disposed;
bool is_sdk_initialized;
std::chrono::system_clock::time_point last_sent_session_request;
json session_params;
//std::string salt;
std::thread *thread;
std::mutex mutex;
bool stop_thread;
bool running;
size_t wait_milliseconds;
unsigned short _auto_session_update_interval = 60; // value is in seconds;
size_t max_events;
#ifndef COUNTLY_USE_SQLITE
std::deque<std::string> event_queue;
#else
std::string database_path;
#endif
bool remote_config_enabled;
json remote_config;
};
#endif