Skip to content

Commit c1ac692

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: engagement time in first screen view when app warm starts (#44)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 41ff6e1 commit c1ac692

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

clickstream/src/main/java/software/aws/solution/clickstream/ActivityLifecycleManager.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
**/
3838
final class ActivityLifecycleManager implements Application.ActivityLifecycleCallbacks, LifecycleEventObserver {
3939
private static final Log LOG = LogFactory.getLog(ActivityLifecycleManager.class);
40+
private static boolean isFromForeground;
4041
private final SessionClient sessionClient;
4142
private final AutoRecordEventClient autoRecordEventClient;
4243

@@ -84,9 +85,14 @@ public void onActivityResumed(final Activity activity) {
8485
ScreenRefererTool.isSameScreen(activity.getClass().getCanonicalName(), activity.getClass().getSimpleName(),
8586
autoRecordEventClient.getScreenUniqueId(activity));
8687
if (ScreenRefererTool.getCurrentScreenName() != null && !isSameScreen) {
87-
autoRecordEventClient.recordUserEngagement();
88+
if (!isFromForeground) {
89+
autoRecordEventClient.recordUserEngagement();
90+
} else {
91+
autoRecordEventClient.resetLastEngageTime();
92+
}
8893
}
8994
autoRecordEventClient.recordViewScreen(activity);
95+
isFromForeground = false;
9096
}
9197

9298
@Override
@@ -127,6 +133,7 @@ public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Life
127133
autoRecordEventClient.flushEvents();
128134
} else if (event == Lifecycle.Event.ON_START) {
129135
LOG.debug("Application entered the foreground.");
136+
isFromForeground = true;
130137
autoRecordEventClient.handleAppStart();
131138
boolean isNewSession = sessionClient.initialSession();
132139
if (isNewSession) {

clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ public void updateStartEngageTimestamp() {
157157
startEngageTimestamp = System.currentTimeMillis();
158158
}
159159

160+
/**
161+
* reset last engagement time when app warm start.
162+
*/
163+
public void resetLastEngageTime() {
164+
lastEngageTime = 0;
165+
}
166+
160167
/**
161168
* check and record _app_update event.
162169
*/

clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,41 @@ public void testPreviousTimestampInTwoScreenViewEvent() throws Exception {
450450
}
451451
}
452452

453+
/**
454+
* test app warm start without engagement_mesc attribute.
455+
*
456+
* @throws Exception exception.
457+
*/
458+
@Test
459+
public void testAppWarmStartWithoutEngagementTime() throws Exception {
460+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
461+
Activity activityA = mock(ActivityA.class);
462+
463+
Bundle bundle = mock(Bundle.class);
464+
callbacks.onActivityCreated(activityA, bundle);
465+
callbacks.onActivityStarted(activityA);
466+
callbacks.onActivityResumed(activityA);
467+
callbacks.onActivityPaused(activityA);
468+
callbacks.onActivityDestroyed(activityA);
469+
470+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
471+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
472+
473+
Activity activityA1 = mock(ActivityA.class);
474+
callbacks.onActivityCreated(activityA1, bundle);
475+
callbacks.onActivityStarted(activityA1);
476+
callbacks.onActivityResumed(activityA1);
477+
478+
try (Cursor cursor = dbUtil.queryAllEvents()) {
479+
cursor.moveToLast();
480+
String eventString = cursor.getString(2);
481+
JSONObject jsonObject = new JSONObject(eventString);
482+
String eventName = jsonObject.getString("event_type");
483+
assertEquals(Event.PresetEvent.SCREEN_VIEW, eventName);
484+
JSONObject attributes = jsonObject.getJSONObject("attributes");
485+
Assert.assertFalse(attributes.has(ReservedAttribute.ENGAGEMENT_TIMESTAMP));
486+
}
487+
}
453488

454489
/**
455490
* test app version not update.

0 commit comments

Comments
 (0)