Skip to content

Commit

Permalink
fix: remove the engagement time attribute in first screen view (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoweii <[email protected]>
  • Loading branch information
zhu-xiaowei and xiaoweii authored Sep 21, 2023
1 parent ba43cfb commit 41ff6e1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void onActivityResumed(final Activity activity) {
boolean isSameScreen =
ScreenRefererTool.isSameScreen(activity.getClass().getCanonicalName(), activity.getClass().getSimpleName(),
autoRecordEventClient.getScreenUniqueId(activity));
if (!isSameScreen) {
if (ScreenRefererTool.getCurrentScreenName() != null && !isSameScreen) {
autoRecordEventClient.recordUserEngagement();
}
autoRecordEventClient.recordViewScreen(activity);
Expand All @@ -96,7 +96,6 @@ public void onActivityPaused(final Activity activity) {
// resumed if app regains focus.In either case, app foreground status does not change for the
// purpose of session tracking.
LOG.debug("Activity paused: " + activity.getLocalClassName());
autoRecordEventClient.updateEndEngageTimestamp();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class AutoRecordEventClient {
*/
private static final Log LOG = LogFactory.getLog(AutoRecordEventClient.class);
private static final int MIN_ENGAGEMENT_TIME = 1000;
private static final int DEVICE_ID_CLIP_LENGTH = 8;
/**
* The context object wraps all the essential information from the app
* that are required.
Expand All @@ -56,7 +55,6 @@ public class AutoRecordEventClient {
private boolean isEntrances;

private long startEngageTimestamp;
private long endEngageTimestamp;
private long lastEngageTime;
private final AndroidPreferences preferences;

Expand Down Expand Up @@ -107,7 +105,9 @@ public void recordViewScreen(Activity activity) {
event.addAttribute(Event.ReservedAttribute.PREVIOUS_TIMESTAMP, lastScreenViewEventTimestamp);
}
event.addAttribute(Event.ReservedAttribute.ENTRANCES, isEntrances ? 1 : 0);
event.addAttribute(Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP, lastEngageTime);
if (lastEngageTime > 0) {
event.addAttribute(Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP, lastEngageTime);
}
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
PreferencesUtil.savePreviousScreenViewTimestamp(preferences, currentTimestamp);
isEntrances = false;
Expand All @@ -130,7 +130,10 @@ public String getScreenUniqueId(Activity activity) {
* record user engagement event.
*/
public void recordUserEngagement() {
lastEngageTime = endEngageTimestamp - startEngageTimestamp;
if (startEngageTimestamp == 0) {
return;
}
lastEngageTime = System.currentTimeMillis() - startEngageTimestamp;
if (clickstreamContext.getClickstreamConfiguration().isTrackUserEngagementEvents() &&
lastEngageTime > MIN_ENGAGEMENT_TIME) {
final AnalyticsEvent event =
Expand All @@ -154,13 +157,6 @@ public void updateStartEngageTimestamp() {
startEngageTimestamp = System.currentTimeMillis();
}

/**
* update end engage timestamp.
*/
public void updateEndEngageTimestamp() {
endEngageTimestamp = System.currentTimeMillis();
}

/**
* check and record _app_update event.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public void setup() {
public void testUserEngagementSuccess() throws Exception {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
Thread.sleep(1100);
client.updateEndEngageTimestamp();
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
try (Cursor cursor = dbUtil.queryAllEvents()) {
List<String> eventList = new ArrayList<>();
Expand Down Expand Up @@ -198,7 +197,7 @@ public void testViewOneScreenEvent() throws Exception {
Assert.assertFalse(attributes.has(ReservedAttribute.PREVIOUS_SCREEN_NAME));
Assert.assertFalse(attributes.has(ReservedAttribute.PREVIOUS_SCREEN_ID));
Assert.assertEquals(1, attributes.getInt(ReservedAttribute.ENTRANCES));
Assert.assertTrue(attributes.has(ReservedAttribute.ENGAGEMENT_TIMESTAMP));
Assert.assertFalse(attributes.has(ReservedAttribute.ENGAGEMENT_TIMESTAMP));
Assert.assertFalse(attributes.has(ReservedAttribute.PREVIOUS_TIMESTAMP));
}
}
Expand Down Expand Up @@ -350,7 +349,8 @@ public void testSameScreenViewAndRecordLastEngagementTime() throws Exception {
callbacks.onActivityResumed(activityA);
Thread.sleep(200);
callbacks.onActivityPaused(activityA);

lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
callbacks.onActivityCreated(activityA, bundle);
callbacks.onActivityStarted(activityA);
callbacks.onActivityResumed(activityA);
Expand Down Expand Up @@ -700,8 +700,11 @@ public void testInitAutoRecordEventClientWithNullAnalyticsClient() {
@After
public void tearDown() {
ScreenRefererTool.setCurrentScreenName(null);
ScreenRefererTool.setCurrentScreenName(null);
ScreenRefererTool.setCurrentScreenId(null);
ScreenRefererTool.setCurrentScreenId(null);
ScreenRefererTool.setCurrentScreenUniqueId(null);
ScreenRefererTool.setCurrentScreenUniqueId(null);
dbUtil.closeDB();
}

Expand Down

0 comments on commit 41ff6e1

Please sign in to comment.