Skip to content

Commit

Permalink
fix: verify engagement time not contains in first screen view when ap…
Browse files Browse the repository at this point in the history
…p warm started.
  • Loading branch information
xiaoweii committed Sep 26, 2023
1 parent 41ff6e1 commit 94d44c4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
**/
final class ActivityLifecycleManager implements Application.ActivityLifecycleCallbacks, LifecycleEventObserver {
private static final Log LOG = LogFactory.getLog(ActivityLifecycleManager.class);
private static boolean isFromForeground;
private final SessionClient sessionClient;
private final AutoRecordEventClient autoRecordEventClient;

Expand Down Expand Up @@ -84,9 +85,14 @@ public void onActivityResumed(final Activity activity) {
ScreenRefererTool.isSameScreen(activity.getClass().getCanonicalName(), activity.getClass().getSimpleName(),
autoRecordEventClient.getScreenUniqueId(activity));
if (ScreenRefererTool.getCurrentScreenName() != null && !isSameScreen) {
autoRecordEventClient.recordUserEngagement();
if (!isFromForeground) {
autoRecordEventClient.recordUserEngagement();
} else {
autoRecordEventClient.resetLastEngageTime();
}
}
autoRecordEventClient.recordViewScreen(activity);
isFromForeground = false;
}

@Override
Expand Down Expand Up @@ -127,6 +133,7 @@ public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Life
autoRecordEventClient.flushEvents();
} else if (event == Lifecycle.Event.ON_START) {
LOG.debug("Application entered the foreground.");
isFromForeground = true;
autoRecordEventClient.handleAppStart();
boolean isNewSession = sessionClient.initialSession();
if (isNewSession) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ public void updateStartEngageTimestamp() {
startEngageTimestamp = System.currentTimeMillis();
}

/**
* reset last engagement time when app warm start.
*/
public void resetLastEngageTime() {
lastEngageTime = 0;
}

/**
* check and record _app_update event.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,41 @@ public void testPreviousTimestampInTwoScreenViewEvent() throws Exception {
}
}

/**
* test app warm start without engagement_mesc attribute.
*
* @throws Exception exception.
*/
@Test
public void testAppWarmStartWithoutEngagementTime() throws Exception {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
Activity activityA = mock(ActivityA.class);

Bundle bundle = mock(Bundle.class);
callbacks.onActivityCreated(activityA, bundle);
callbacks.onActivityStarted(activityA);
callbacks.onActivityResumed(activityA);
callbacks.onActivityPaused(activityA);
callbacks.onActivityDestroyed(activityA);

lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);

Activity activityA1 = mock(ActivityA.class);
callbacks.onActivityCreated(activityA1, bundle);
callbacks.onActivityStarted(activityA1);
callbacks.onActivityResumed(activityA1);

try (Cursor cursor = dbUtil.queryAllEvents()) {
cursor.moveToLast();
String eventString = cursor.getString(2);
JSONObject jsonObject = new JSONObject(eventString);
String eventName = jsonObject.getString("event_type");
assertEquals(Event.PresetEvent.SCREEN_VIEW, eventName);
JSONObject attributes = jsonObject.getJSONObject("attributes");
Assert.assertFalse(attributes.has(ReservedAttribute.ENGAGEMENT_TIMESTAMP));
}
}

/**
* test app version not update.
Expand Down

0 comments on commit 94d44c4

Please sign in to comment.