Skip to content

Commit 99145be

Browse files
Merge pull request #555 from Countly/fix_up_events_save
Fix up events save
2 parents 5ac9cd6 + 4ca6af1 commit 99145be

4 files changed

Lines changed: 31 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## XX.XX.XX
2+
* Improved user properties auto-save conditions to flush event queue with every user property call.
3+
14
## 26.1.2
25
* Added `CountlyInitProvider` ContentProvider to register activity lifecycle callbacks before `Application.onCreate()`. This ensures the SDK captures the current activity in single-activity frameworks (Flutter, React Native) and apps with deferred initialization.
36
* Added `CountlyConfig.setInitialActivity(Activity)` as an explicit way for wrapper SDKs to provide the host activity during initialization.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ public void sessionDurationScenario_1() throws InterruptedException, JSONExcepti
308308
countly.deviceId().changeWithoutMerge("ff"); // this will generate a request with "end_session", "session_duration" fields and reset duration + begin_session
309309
assertEquals(6, TestUtils.getCurrentRQ().length); // not 5 anymore, it will send orientation event as well
310310

311-
TestUtils.validateRequest("ff_merge", TestUtils.map("old_device_id", "1234"), 1);
312-
ModuleEventsTests.validateEventInRQ("ff_merge", "[CLY]_orientation", null, 1, 0.0d, 0.0d, "_CLY_", "_CLY_", "_CLY_", "_CLY_", 2, -1, 0, 1);
311+
ModuleEventsTests.validateEventInRQ(TestUtils.commonDeviceId, "[CLY]_orientation", null, 1, 0.0d, 0.0d, "_CLY_", "_CLY_", "_CLY_", "_CLY_", 1, -1, 0, 1);
312+
TestUtils.validateRequest("ff_merge", TestUtils.map("old_device_id", "1234"), 2);
313313
ModuleUserProfileTests.validateUserProfileRequest("ff_merge", 3, 6, TestUtils.map(), TestUtils.map("prop2", 123, "prop1", "string", "prop3", false));
314314
ModuleSessionsTests.validateSessionEndRequest(4, 3, "ff_merge");
315315

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public void eventSaveScenario_manualSessions() throws JSONException {
5353
countly.events().recordEvent("test_event3");
5454

5555
countly.userProfile().setProperty("theme", "light_mode");
56-
TestUtils.assertRQSize(3); // no request is generated on the way, not 2 anymore because of orientation event
56+
// UPDATE: setProperty will trigger saving events
57+
TestUtils.assertRQSize(4); // no request is generated on the way, not 2 anymore because of orientation event
5758

5859
countly.sessions().endSession();
5960
// begin_session + first user property request + 3 events + user property request with light_mode + end_session
@@ -84,7 +85,7 @@ public void eventSaveScenario_onTimer() throws InterruptedException, JSONExcepti
8485
countly.events().recordEvent("test_event3");
8586

8687
countly.userProfile().setProperty("theme", "light_mode");
87-
TestUtils.assertRQSize(1); // no request is generated on the way
88+
TestUtils.assertRQSize(2); // no request is generated on the way + UPDATE: user properties will trigger saving events
8889

8990
Thread.sleep(3000);
9091

@@ -112,7 +113,8 @@ public void eventSaveScenario_changeDeviceIDWithoutMerge() throws JSONException
112113
countly.events().recordEvent("test_event3");
113114

114115
countly.userProfile().setProperty("theme", "light_mode");
115-
TestUtils.assertRQSize(1); // no request is generated on the way
116+
// UPDATE: this will trigger saving events
117+
TestUtils.assertRQSize(2); // no request is generated on the way
116118

117119
countly.deviceId().changeWithoutMerge("new_device_id"); // this will begin a new session
118120

@@ -287,12 +289,12 @@ public void UP_203_CNR_A_events() throws JSONException {
287289

288290
countly.events().recordEvent("A");
289291
countly.events().recordEvent("B");
290-
sendSameData(countly);
291-
countly.events().recordEvent("C");
292-
sendSameData(countly);
293-
countly.events().recordEvent("D");
294-
sendSameData(countly);
295-
countly.events().recordEvent("E");
292+
sendSameData(countly); // UPDATE: this will trigger A&B
293+
countly.events().recordEvent("C"); // remaining UP
294+
sendSameData(countly); // UPDATE: this will trigger C
295+
countly.events().recordEvent("D"); // remaining UP
296+
sendSameData(countly); // UPDATE: this will trigger D
297+
countly.events().recordEvent("E"); // remaining UP
296298

297299
TestUtils.assertRQSize(6);
298300

@@ -402,10 +404,10 @@ public void UP_207_CNR_M() throws JSONException {
402404
countly.sessions().beginSession();
403405
countly.events().recordEvent("A");
404406
countly.events().recordEvent("B");
405-
sendSameData(countly);
407+
sendSameData(countly); // UPDATE: this will trigger A&B
406408
countly.sessions().endSession();
407409
countly.events().recordEvent("C");
408-
sendUserData(countly);
410+
sendUserData(countly); // UPDATE: this will trigger C
409411
countly.sessions().endSession();
410412
countly.deviceId().changeWithMerge("merge_id");
411413
sendSameData(countly);
@@ -424,9 +426,9 @@ public void UP_207_CNR_M() throws JSONException {
424426

425427
ModuleSessionsTests.validateSessionEndRequest(3, null, TestUtils.commonDeviceId);
426428

427-
TestUtils.validateRequest("merge_id", TestUtils.map("old_device_id", TestUtils.commonDeviceId), 4);
429+
ModuleEventsTests.validateEventInRQ("C", 4, 0, 1);
428430

429-
ModuleEventsTests.validateEventInRQ("merge_id", "C", 5, 0, 1);
431+
TestUtils.validateRequest("merge_id", TestUtils.map("old_device_id", TestUtils.commonDeviceId), 5);
430432

431433
validateUserDataRequest(6, 8, "4", "merge_id");
432434

@@ -477,9 +479,10 @@ public void UP_208_CR_CG_M() throws JSONException {
477479
ModuleUserProfileTests.validateUserProfileRequest(3, 9, TestUtils.map(), TestUtils.map("a12345", "4"));
478480

479481
ModuleSessionsTests.validateSessionEndRequest(4, null, TestUtils.commonDeviceId);
480-
TestUtils.validateRequest("merge_id", TestUtils.map("old_device_id", TestUtils.commonDeviceId), 5);
481482

482-
ModuleEventsTests.validateEventInRQ("merge_id", "C", 6, 0, 1);
483+
ModuleEventsTests.validateEventInRQ("C", 5, 0, 1);
484+
485+
TestUtils.validateRequest("merge_id", TestUtils.map("old_device_id", TestUtils.commonDeviceId), 6);
483486

484487
validateUserDataRequest(7, 9, "4", "merge_id");
485488

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,7 @@ void modifyCustomData(String key, Object value, String mod) {
254254
}
255255

256256
customMods.put(truncatedKey, ob);
257-
applyUserPropertyCacheLimit(customMods);
258-
259-
isSynced = false;
257+
onUserPropertiesChanged(customMods);
260258
} catch (JSONException e) {
261259
e.printStackTrace();
262260
}
@@ -332,9 +330,15 @@ void setPropertiesInternal(@NonNull Map<String, Object> data) {
332330
}
333331

334332
custom.putAll(dataCustomFields);
335-
applyUserPropertyCacheLimit(custom);
333+
onUserPropertiesChanged(custom);
334+
}
336335

336+
private void onUserPropertiesChanged(Map<String, ?> sourceMap) {
337+
applyUserPropertyCacheLimit(sourceMap);
337338
isSynced = false;
339+
if (storageProvider.getEventQueueSize() > 0) {
340+
_cly.moduleRequestQueue.sendEventsIfNeeded(true);
341+
}
338342
}
339343

340344
private void applyUserPropertyCacheLimit(Map<String, ?> map) {

0 commit comments

Comments
 (0)