Skip to content

Commit c39557e

Browse files
Merge pull request #519 from Countly/session_user_props
Save user props before session calls
2 parents e442f6f + b830d17 commit c39557e

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 25.4.6
2+
* Updated user properties caching mechanism according to sessions.
3+
14
## 25.4.5
25
* Added a new config flag `setUseSerialExecutor(boolean useSerial)` for selecting immediate request executor type.
36
* Added a new config option `setWebviewDisplayOption(WebViewDisplayOption)` to control how Content and Feedback Widgets are displayed.

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,95 @@ public void eventSaveScenario_changeDeviceIDWithoutMerge() throws JSONException
120120
ModuleUserProfileTests.validateUserProfileRequest(2, 3, TestUtils.map(), TestUtils.map("theme", "light_mode"));
121121
}
122122

123+
/**
124+
* Related user properties should not be saved with session calls,
125+
* call order, user property before session, begin session, user property after begin session, update session, user property after update session, end session
126+
* generated request order begin_session + update_session + user properties + end_session
127+
* manual sessions are enabled
128+
* UPDATE: session calls now trigger saving properties like events as well
129+
* UPDATED_REQUEST_ORDER: user property before session + begin_session + user property after begin session + update_session + user property after update session + end_session
130+
*/
131+
@Test
132+
public void eventSaveScenario_sessionCallsTriggersSave_M() throws JSONException, InterruptedException {
133+
Countly countly = new Countly().init(TestUtils.createBaseConfig().enableManualSessionControl().setTrackOrientationChanges(false));
134+
135+
TestUtils.assertRQSize(0);
136+
countly.userProfile().setProperty("before_session", true);
137+
138+
countly.sessions().beginSession();
139+
TestUtils.assertRQSize(2); // only begin session request, UPDATE: user property request before session
140+
ModuleUserProfileTests.validateUserProfileRequest(0, 2, TestUtils.map(), TestUtils.map("before_session", true));
141+
ModuleSessionsTests.validateSessionBeginRequest(1, TestUtils.commonDeviceId);
142+
143+
countly.userProfile().setProperty("after_begin_session", true);
144+
TestUtils.assertRQSize(2); // still begin session, UPDATE: user property request after begin session
145+
// no new user property request because it is validated along with begin session
146+
147+
Thread.sleep(2000);
148+
149+
countly.sessions().updateSession();
150+
TestUtils.assertRQSize(4); // only begin session and update session requests, UPDATE: user property request after begin and update session
151+
152+
ModuleUserProfileTests.validateUserProfileRequest(2, 4, TestUtils.map(), TestUtils.map("after_begin_session", true));
153+
ModuleSessionsTests.validateSessionUpdateRequest(3, 2, TestUtils.commonDeviceId);
154+
155+
countly.userProfile().setProperty("after_update_session", true);
156+
TestUtils.assertRQSize(4); // still begin session and update session requests
157+
// no new user property request because it is validated along with update session
158+
Thread.sleep(2000);
159+
160+
countly.sessions().endSession();
161+
TestUtils.assertRQSize(6); // begin, update, user properties and end session requests
162+
ModuleUserProfileTests.validateUserProfileRequest(4, 6, TestUtils.map(), TestUtils.map("after_update_session", true));
163+
ModuleSessionsTests.validateSessionEndRequest(5, 2, TestUtils.commonDeviceId);
164+
}
165+
166+
/**
167+
* Related user properties should be saved with automatic session calls,
168+
* call order, user property before session, user property after begin session, session, user property after update session
169+
* generated request order user_properties + begin_session + user_properties + update_session + user properties + end_session
170+
*/
171+
@Test
172+
public void eventSaveScenario_sessionCallsTriggersSave_A() throws JSONException, InterruptedException {
173+
CountlyConfig config = TestUtils.createBaseConfig(TestUtils.getContext()).setTrackOrientationChanges(false);
174+
TestLifecycleObserver testLifecycleObserver = new TestLifecycleObserver();
175+
config.lifecycleObserver = testLifecycleObserver;
176+
config.setUpdateSessionTimerDelay(3);
177+
Countly countly = new Countly().init(config);
178+
179+
TestUtils.assertRQSize(0);
180+
countly.userProfile().setProperty("before_session", true);
181+
182+
testLifecycleObserver.bringToForeground();
183+
countly.onStart(null);
184+
185+
TestUtils.assertRQSize(2);
186+
ModuleUserProfileTests.validateUserProfileRequest(0, 2, TestUtils.map(), TestUtils.map("before_session", true));
187+
ModuleSessionsTests.validateSessionBeginRequest(1, TestUtils.commonDeviceId);
188+
189+
countly.userProfile().setProperty("after_begin_session", true);
190+
TestUtils.assertRQSize(2);
191+
192+
Thread.sleep(3000);
193+
194+
TestUtils.assertRQSize(4);
195+
196+
ModuleUserProfileTests.validateUserProfileRequest(2, 4, TestUtils.map(), TestUtils.map("after_begin_session", true));
197+
ModuleSessionsTests.validateSessionUpdateRequest(3, 3, TestUtils.commonDeviceId);
198+
199+
countly.userProfile().setProperty("after_update_session", true);
200+
TestUtils.assertRQSize(4);
201+
202+
Thread.sleep(2000);
203+
204+
testLifecycleObserver.goToBackground();
205+
countly.onStop();
206+
207+
TestUtils.assertRQSize(6);
208+
ModuleUserProfileTests.validateUserProfileRequest(4, 6, TestUtils.map(), TestUtils.map("after_update_session", true));
209+
ModuleSessionsTests.validateSessionEndRequest(5, 2, TestUtils.commonDeviceId);
210+
}
211+
123212
/**
124213
* 1. 200_CNR_A
125214
* Init SDK

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void beginSessionInternal() {
6060
String preparedMetrics = deviceInfo.getMetrics(_cly.context_, metricOverride, L);
6161
sessionRunning = true;
6262
prevSessionDurationStartTime_ = System.currentTimeMillis();
63+
_cly.moduleUserProfile.saveInternal();
64+
6365
requestQueueProvider.beginSession(_cly.moduleLocation.locationDisabled, _cly.moduleLocation.locationCountryCode, _cly.moduleLocation.locationCity, _cly.moduleLocation.locationGpsCoordinates, _cly.moduleLocation.locationIpAddress, preparedMetrics);
6466

6567
if (_cly.moduleViews.trackOrientationChanges) {
@@ -85,6 +87,8 @@ void updateSessionInternal() {
8587
}
8688

8789
if (!_cly.disableUpdateSessionRequests_) {
90+
_cly.moduleUserProfile.saveInternal();
91+
8892
requestQueueProvider.updateSession(roundedSecondsSinceLastSessionDurationUpdate());
8993
}
9094
}

0 commit comments

Comments
 (0)