@@ -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
152153void 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
183224void 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