Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: engagement time in first screen view when app warm starts #44

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading