Skip to content

Commit 4329ba3

Browse files
authored
Merge pull request #347 from Countly/fix_sessions_without_merge
Fix sessions without merge
2 parents ac2f14f + 4eb418e commit 4329ba3

File tree

9 files changed

+52
-30
lines changed

9 files changed

+52
-30
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## XX.XX.XX
2+
* Mitigated issues where:
3+
* session was ending regardless of manual control after without merge, not anymore.
4+
* session was not starting even if consent is not required and automatic sessions are enabled after without merge, not anymore.
5+
16
## 24.7.0
27
* ! Minor breaking change ! User properties will now be automatically saved under the following conditions:
38
* When an event is recorded

sdk/src/androidTest/java/ly/count/android/sdk/DeviceIdTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,11 @@ public void legacyFallbackType_null() {
260260
* ---
261261
* set a couple of user properties
262262
* sleep 1 second
263-
* change device ID with merge (creates a session duration request with the duration of 1 second)
263+
* change device ID with merge
264264
* save user profiles
265265
* sleep 2 seconds
266-
* change device ID without merge (creates a session duration request with the duration of 2 seconds)
267-
* -- at this point 4 requests are generated (firs begin, 1 merge, 1 user profile, 1 end session for without merge)
266+
* change device ID without merge (creates a session duration request with the duration of 3 seconds) and will generate new begin session because consent not required
267+
* -- at this point 4 requests are generated (firs begin, 1 merge, 1 user profile, 1 end session for without merge, 1 begin session for the new user)
268268
* sleep 1 second
269269
* set user property and save it
270270
* change device ID without merge with same device id (no session duration request)
@@ -277,7 +277,7 @@ public void legacyFallbackType_null() {
277277
* set user properties and save them
278278
* sleep 1 second
279279
* change device ID with merge (creates a session duration request with the duration of 4 seconds)
280-
* -- at this point 8 requests are generated (firs begin, 1 merge, 1 user profile, 1 end session for without merge, 1 user profile, 1 user profile, 1 user profile, 1 merge)
280+
* -- at this point 9 requests are generated (firs begin, 1 merge, 1 user profile, 1 end session for without merge, 1 begin session for the new user, 1 user profile, 1 user profile, 1 user profile, 1 merge)
281281
*/
282282
@Test
283283
public void sessionDurationScenario_1() throws InterruptedException {
@@ -294,13 +294,13 @@ public void sessionDurationScenario_1() throws InterruptedException {
294294

295295
Thread.sleep(1000);
296296

297-
countly.deviceId().changeWithMerge("ff_merge"); // this will generate a request with "session_duration" field and reset duration
297+
countly.deviceId().changeWithMerge("ff_merge");
298298
countly.userProfile().save();
299299

300300
Thread.sleep(2000);
301301

302-
countly.deviceId().changeWithoutMerge("ff"); // this will generate a request with "end_session", "session_duration" fields and reset duration
303-
assertEquals(4, TestUtils.getCurrentRQ().length);
302+
countly.deviceId().changeWithoutMerge("ff"); // this will generate a request with "end_session", "session_duration" fields and reset duration + begin_session
303+
assertEquals(5, TestUtils.getCurrentRQ().length);
304304

305305
TestUtils.validateRequest("ff_merge", TestUtils.map("old_device_id", "1234"), 1);
306306
TestUtils.validateRequest("ff_merge", TestUtils.map("user_details", "{\"custom\":{\"prop2\":\"123\",\"prop1\":\"string\",\"prop3\":\"false\"}}"), 2);
@@ -332,12 +332,12 @@ public void sessionDurationScenario_1() throws InterruptedException {
332332

333333
countly.deviceId().changeWithMerge("ff_merge"); // this will generate a request with "session_duration" field and reset duration
334334

335-
assertEquals(8, TestUtils.getCurrentRQ().length);
335+
assertEquals(9, TestUtils.getCurrentRQ().length);
336336

337-
TestUtils.validateRequest("ff", TestUtils.map("user_details", "{\"custom\":{\"prop4\":\"[sd]\"}}"), 4);
338-
TestUtils.validateRequest("ff", TestUtils.map("user_details", "{\"custom\":{\"prop6\":\"{key=123}\",\"prop5\":\"{key=value}\",\"prop7\":\"{key=false}\"}}"), 5);
339-
TestUtils.validateRequest("ff", TestUtils.map("user_details", "{\"custom\":{\"prop2\":\"456\",\"prop1\":\"string_a\",\"prop3\":\"true\"}}"), 6);
337+
TestUtils.validateRequest("ff", TestUtils.map("user_details", "{\"custom\":{\"prop4\":\"[sd]\"}}"), 5);
338+
TestUtils.validateRequest("ff", TestUtils.map("user_details", "{\"custom\":{\"prop6\":\"{key=123}\",\"prop5\":\"{key=value}\",\"prop7\":\"{key=false}\"}}"), 6);
339+
TestUtils.validateRequest("ff", TestUtils.map("user_details", "{\"custom\":{\"prop2\":\"456\",\"prop1\":\"string_a\",\"prop3\":\"true\"}}"), 7);
340340

341-
TestUtils.validateRequest("ff_merge", TestUtils.map("old_device_id", "ff"), 7);
341+
TestUtils.validateRequest("ff_merge", TestUtils.map("old_device_id", "ff"), 8);
342342
}
343343
}

sdk/src/androidTest/java/ly/count/android/sdk/scSE_SessionsTests.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ public void SE_203_CR_CG_M_id_change() throws InterruptedException {
202202
* *back to foreground*
203203
* --- Expected Requests ---
204204
* Check request queue and verify:
205-
* 1. too lazy to calculate just let us know and lets verify together
206205
*/
207206
@Test
208207
public void SE_204_CNR_A_id_change() throws InterruptedException {
@@ -211,15 +210,22 @@ public void SE_204_CNR_A_id_change() throws InterruptedException {
211210

212211
flowAutomaticSessions(countly);
213212

214-
Assert.assertEquals(8, TestUtils.getCurrentRQ().length);
213+
Assert.assertEquals(12, TestUtils.getCurrentRQ().length);
215214
validateSessionBeginRequest(0, TestUtils.commonDeviceId);
216215
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", TestUtils.commonDeviceId), 1);
217216
validateSessionEndRequest(2, 2, "newID");
218-
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 3);
219-
validateSessionBeginRequest(4, "newID_2");
220-
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 5);
221-
validateSessionEndRequest(6, null, "newID");
222-
validateSessionBeginRequest(7, "newID");
217+
218+
validateSessionBeginRequest(3, "newID_2");
219+
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 4);
220+
validateSessionEndRequest(5, 2, "newID");
221+
222+
validateSessionBeginRequest(6, "newID_2");
223+
validateSessionEndRequest(7, 1, "newID_2");
224+
225+
validateSessionBeginRequest(8, "newID_2");
226+
TestUtils.validateRequest("newID", TestUtils.map("old_device_id", "newID_2"), 9);
227+
validateSessionEndRequest(10, null, "newID");
228+
validateSessionBeginRequest(11, "newID");
223229
}
224230

225231
/**

sdk/src/androidTest/java/ly/count/android/sdk/scUP_UserProfileTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void eventSaveScenario_onTimer() throws InterruptedException, JSONExcepti
9494
/**
9595
* Related user properties should be saved before event recordings
9696
* call order, user property with "dark_mode", event, user property with "light_mode"
97-
* generated request order first user property request + 3 events + user property request with light_mode
97+
* generated request order first user property request + 3 events + user property request with light_mode + begin session
9898
*/
9999
@Test
100100
public void eventSaveScenario_changeDeviceIDWithoutMerge() throws JSONException {
@@ -111,11 +111,11 @@ public void eventSaveScenario_changeDeviceIDWithoutMerge() throws JSONException
111111
countly.userProfile().setProperty("theme", "light_mode");
112112
TestUtils.assertRQSize(1); // no request is generated on the way
113113

114-
countly.deviceId().changeWithoutMerge("new_device_id");
114+
countly.deviceId().changeWithoutMerge("new_device_id"); // this will begin a new session
115115

116116
// first user property request + 3 events + user property request with light_mode
117-
ModuleUserProfileTests.validateUserProfileRequest(0, 3, TestUtils.map(), TestUtils.map("theme", "dark_mode"));
118-
ModuleUserProfileTests.validateUserProfileRequest(2, 3, TestUtils.map(), TestUtils.map("theme", "light_mode"));
117+
ModuleUserProfileTests.validateUserProfileRequest(0, 4, TestUtils.map(), TestUtils.map("theme", "dark_mode"));
118+
ModuleUserProfileTests.validateUserProfileRequest(2, 4, TestUtils.map(), TestUtils.map("theme", "light_mode"));
119119
}
120120

121121
/**

sdk/src/main/java/ly/count/android/sdk/Countly.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,11 +878,11 @@ public synchronized void halt() {
878878
staticInit();
879879
}
880880

881-
synchronized void notifyDeviceIdChange() {
881+
synchronized void notifyDeviceIdChange(boolean withoutMerge) {
882882
L.d("Notifying modules that device ID changed");
883883

884884
for (ModuleBase module : modules) {
885-
module.deviceIdChanged();
885+
module.deviceIdChanged(withoutMerge);
886886
}
887887
}
888888

sdk/src/main/java/ly/count/android/sdk/ModuleBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void callbackOnActivityStopped(Activity activity) {
8888
//}
8989

9090
//notify the SDK modules that the device ID has changed
91-
void deviceIdChanged() {
91+
void deviceIdChanged(boolean withoutMerge) {
9292
}
9393

9494
//notify the SDK modules that consent was updated

sdk/src/main/java/ly/count/android/sdk/ModuleDeviceId.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ void changeDeviceIdWithoutMergeInternal(@NonNull String deviceId) {
131131
//update remote config_ values after id change if automatic update is enabled
132132
_cly.moduleRemoteConfig.clearAndDownloadAfterIdChange();
133133

134-
_cly.moduleSessions.endSessionInternal();
134+
if (!_cly.moduleSessions.manualSessionControlEnabled) {
135+
//if manual session control is not enabled, end the current session
136+
_cly.moduleSessions.endSessionInternal(); // this will check consent
137+
}
135138

136139
//remove all consent
137140
_cly.moduleConsent.removeConsentAllInternal(ModuleConsent.ConsentChangeSource.DeviceIDChangedNotMerged);
@@ -146,7 +149,7 @@ void changeDeviceIdWithoutMergeInternal(@NonNull String deviceId) {
146149

147150
//clear automated star rating session values because now we have a new user
148151
_cly.moduleRatings.clearAutomaticStarRatingSessionCountInternal();
149-
_cly.notifyDeviceIdChange();
152+
_cly.notifyDeviceIdChange(true);
150153
}
151154

152155
/**
@@ -192,7 +195,7 @@ void changeDeviceIdWithMergeInternal(@NonNull String deviceId) {
192195
_cly.moduleRemoteConfig.clearAndDownloadAfterIdChange();
193196
requestQueueProvider.changeDeviceId(deviceId, deviceIdInstance.getCurrentId());
194197
deviceIdInstance.changeToCustomId(deviceId);
195-
_cly.notifyDeviceIdChange();
198+
_cly.notifyDeviceIdChange(false);
196199
}
197200
}
198201

sdk/src/main/java/ly/count/android/sdk/ModuleRemoteConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ void onConsentChanged(@NonNull final List<String> consentChangeDelta, final bool
433433
}
434434

435435
@Override
436-
void deviceIdChanged() {
436+
void deviceIdChanged(boolean withoutMerge) {
437437
L.v("[RemoteConfig] Device ID changed will update values: [" + updateRemoteConfigAfterIdChange + "]");
438438

439439
if (updateRemoteConfigAfterIdChange) {

sdk/src/main/java/ly/count/android/sdk/ModuleSessions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ void halt() {
171171
sessionRunning = false;
172172
}
173173

174+
@Override
175+
void deviceIdChanged(boolean withoutMerge) {
176+
if (!manualSessionControlEnabled && withoutMerge) {
177+
L.d("[ModuleSessions] deviceIdChanged, automatic session control enabled and device id changed without merge, starting a new session");
178+
beginSessionInternal();
179+
}
180+
}
181+
174182
public class Sessions {
175183
public void beginSession() {
176184
synchronized (_cly) {

0 commit comments

Comments
 (0)