Skip to content

Commit d8716db

Browse files
[SDK 646] Added new public method to change device id (#29)
* device Id change with and without merge. * Change log added. * PR Changes * PR Changes * code styling * Change lod entry added changing device id logic updated Co-authored-by: ArtursKadikis <[email protected]>
1 parent 7b9c462 commit d8716db

File tree

3 files changed

+69
-20
lines changed

3 files changed

+69
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* ! Minor breaking change ! Default automatic session update duration changed to 60 seconds.
44
* Added a call to change the automatic session update duration.
55
* Fixed a bug where session duration was reported wrong.
6+
* Fixed bug when changing device id with server merge.
7+
* Fixed bug when device id was changed without server merge. Previously the new session was started with the old device ID and not the new one.
68
* Fixed a bug that was a typo ('COUNTLY_CUSTOM_HTTP' instead of 'COUNTLY_USE_CUSTOM_HTTP') in the cmake file that cause the SDK to be misconfigured.
79
* Fixed a bug that caused POST requests to fail on Windows.
810

include/countly.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ class Countly {
217217

218218
HTTPResponse sendHTTP(std::string path, std::string data);
219219

220+
void _changeDeviceIdWithMerge(const std::string& value);
221+
222+
void _changeDeviceIdWithoutMerge(const std::string& value);
223+
220224
std::chrono::system_clock::duration getSessionDuration(std::chrono::system_clock::time_point now);
221225

222226
std::chrono::system_clock::duration getSessionDuration();

src/countly.cpp

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,46 +149,89 @@ void Countly::setLocation(double lattitude, double longitude) {
149149
session_params["location"] = location_stream.str();
150150
}
151151

152+
#pragma region Device Id
152153
void Countly::setDeviceID(const std::string& value, bool same_user) {
153154
mutex.lock();
154-
if (session_params.find("device_id") == session_params.end() || (session_params["device_id"].is_string() && session_params["device_id"].get<std::string>() == value)) {
155+
log(Countly::LogLevel::INFO, "[Countly][changeDeviceIdWithMerge] setDeviceID = '" + value + "'");
156+
157+
//Checking old and new devices ids are same
158+
if (session_params.contains("device_id") && session_params["device_id"].get<std::string>() == value) {
159+
log(Countly::LogLevel::DEBUG, "[Countly][setDeviceID] new device id and old device id are same.");
160+
mutex.unlock();
161+
return;
162+
}
163+
164+
if (!session_params.contains("device_id")) {
155165
session_params["device_id"] = value;
166+
log(Countly::LogLevel::DEBUG, "[Countly][setDeviceID] no device was set, setting device id");
156167
mutex.unlock();
157168
return;
158169
}
159170

171+
//If code does reach here without sdk init, it will throw an exception.
172+
mutex.unlock();
160173
if (same_user) {
161-
session_params["old_device_id"] = session_params["device_id"];
174+
_changeDeviceIdWithMerge(value);
175+
}
176+
else {
177+
_changeDeviceIdWithoutMerge(value);
178+
}
179+
}
180+
181+
/* Change device ID with merge after SDK has been initialized.*/
182+
void Countly::_changeDeviceIdWithMerge(const std::string& value) {
183+
mutex.lock();
184+
log(Countly::LogLevel::DEBUG, "[Countly][changeDeviceIdWithMerge] deviceId = '" + value + "'");
185+
186+
session_params["old_device_id"] = session_params["device_id"];
187+
session_params["device_id"] = value;
188+
189+
const std::chrono::system_clock::time_point now = Countly::getTimestamp();
190+
const auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
191+
std::map<std::string, std::string> data = {
192+
{"app_key", session_params["app_key"].get<std::string>()},
193+
{"device_id", session_params["device_id"].get<std::string>()},
194+
{"old_device_id", session_params["old_device_id"].get<std::string>()},
195+
{"timestamp", std::to_string(timestamp.count())},
196+
};
197+
sendHTTP("/i", Countly::serializeForm(data));
198+
199+
session_params.erase("old_device_id");
200+
mutex.unlock();
201+
}
202+
203+
/* Change device ID without merge after SDK has been initialized.*/
204+
void Countly::_changeDeviceIdWithoutMerge(const std::string& value) {
205+
log(Countly::LogLevel::DEBUG, "[Countly][changeDeviceIdWithoutMerge] deviceId = '" + value + "'");
206+
207+
//send all event to server and end current session of old user
208+
flushEvents();
209+
if (began_session) {
210+
endSession();
211+
mutex.lock();
162212
session_params["device_id"] = value;
163-
if (began_session) {
164-
mutex.unlock();
165-
endSession();
166-
beginSession();
167-
mutex.lock();
168-
}
169-
session_params.erase("old_device_id");
170-
} else {
171-
if (began_session) {
172-
mutex.unlock();
173-
flushEvents();
174-
endSession();
175-
beginSession();
176-
mutex.lock();
177-
}
213+
mutex.unlock();
214+
beginSession();
215+
}
216+
else {
217+
mutex.lock();
178218
session_params["device_id"] = value;
219+
mutex.unlock();
179220
}
180-
mutex.unlock();
181221
}
222+
#pragma endregion Device Id
182223

183224
void Countly::start(const std::string& app_key, const std::string& host, int port, bool start_thread) {
184225
mutex.lock();
185226
log(Countly::LogLevel::INFO, "[Countly][start]");
186227
this->host = host;
187228
if (host.find("http://") == 0) {
188229
use_https = false;
189-
} else if (host.find("https://") == 0) {
230+
}
231+
else if (host.find("https://") == 0) {
190232
use_https = true;
191-
} else {
233+
}
234+
else {
192235
use_https = false;
193236
this->host.insert(0, "http://");
194237
}

0 commit comments

Comments
 (0)