From 18c6b8c7d095c2ab43794c4f6f9e6e5097d0b815 Mon Sep 17 00:00:00 2001 From: Xiaowei Zhu <33129495+zhu-xiaowei@users.noreply.github.com> Date: Wed, 21 Feb 2024 20:39:14 +0800 Subject: [PATCH] fix: filter two consecutive identical scree view events (#65) --- .../clickstream/ActivityLifecycleManager.java | 6 +-- .../client/AutoRecordEventClient.java | 7 +++- .../clickstream/client/ScreenRefererTool.java | 11 ++---- .../AutoRecordEventClientTest.java | 39 +++++++++++++++++++ 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/ActivityLifecycleManager.java b/clickstream/src/main/java/software/aws/solution/clickstream/ActivityLifecycleManager.java index 46cff59..29f0009 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/ActivityLifecycleManager.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/ActivityLifecycleManager.java @@ -83,9 +83,8 @@ public void onActivityResumed(final Activity activity) { // An activity came to foreground. Application potentially entered foreground as well // if there were no other activities in the foreground. LOG.debug("Activity resumed: " + activity.getLocalClassName()); - boolean isSameScreen = - ScreenRefererTool.isSameScreen(activity.getClass().getCanonicalName(), activity.getClass().getSimpleName(), - autoRecordEventClient.getScreenUniqueId(activity)); + boolean isSameScreen = ScreenRefererTool.isSameScreen(activity.getClass().getSimpleName(), + autoRecordEventClient.getScreenUniqueId(activity)); if (ScreenRefererTool.getCurrentScreenName() != null && !isSameScreen) { if (!isFromForeground) { autoRecordEventClient.recordUserEngagement(); @@ -99,6 +98,7 @@ public void onActivityResumed(final Activity activity) { /** * Handle Screen View triggered manually. + * * @param event the screen view event */ public void onScreenViewManually(AnalyticsEvent event) { diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java index 1d9a3e5..431efda 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java @@ -85,7 +85,7 @@ public void recordViewScreenAutomatically(Activity activity) { String screenId = activity.getClass().getCanonicalName(); String screenName = activity.getClass().getSimpleName(); String screenUniqueId = getScreenUniqueId(activity); - if (ScreenRefererTool.isSameScreen(screenId, screenName, screenUniqueId)) { + if (ScreenRefererTool.isSameScreen(screenName, screenUniqueId)) { return; } ScreenRefererTool.setCurrentScreenId(screenId); @@ -104,11 +104,14 @@ public void recordViewScreenAutomatically(Activity activity) { public void recordViewScreenManually(AnalyticsEvent event) { String screenName = event.getStringAttribute(Event.ReservedAttribute.SCREEN_NAME); if (screenName != null) { + String screenUniqueId = event.getStringAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID); + if (ScreenRefererTool.isSameScreen(screenName, screenUniqueId)) { + return; + } if (ScreenRefererTool.getCurrentScreenName() != null) { recordUserEngagement(); } ScreenRefererTool.setCurrentScreenName(screenName); - String screenUniqueId = event.getStringAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID); if (screenUniqueId != null) { ScreenRefererTool.setCurrentScreenUniqueId(screenUniqueId); } diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/ScreenRefererTool.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/ScreenRefererTool.java index c6cc3fd..ddbc73f 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/ScreenRefererTool.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/ScreenRefererTool.java @@ -117,16 +117,13 @@ public static String getPreviousScreenUniqueId() { /** * Judging that the current screen is the same as the previous screen. * - * @param screenId current screen id * @param screenName current screen name * @param screenUniqueId current screen unique id * @return the boolean value for is the same screen */ - public static boolean isSameScreen(String screenId, String screenName, String screenUniqueId) { - return mCurrentScreenId != null - && mCurrentScreenName != null - && mCurrentScreenId.equals(screenId) - && mCurrentScreenName.equals(screenName) - && mCurrentScreenUniqueId.equals(screenUniqueId); + public static boolean isSameScreen(String screenName, String screenUniqueId) { + return mCurrentScreenName != null && + mCurrentScreenName.equals(screenName) && + (mCurrentScreenUniqueId == null || mCurrentScreenUniqueId.equals(screenUniqueId)); } } diff --git a/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java b/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java index 8bb8421..008b86b 100644 --- a/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java +++ b/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java @@ -54,6 +54,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -668,6 +669,44 @@ public void testRecordTwoScreenViewWhenAutoTrackIsDisabled() throws Exception { } } + /** + * test record two same screen view event manually and will not record the last screen view event. + * + * @throws Exception exception + */ + @Test + public void testRecordTwoSameScreenViewManually() throws Exception { + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + Fragment fragmentA = mock(FragmentA.class); + final AnalyticsEvent event1 = + clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SCREEN_VIEW); + event1.addAttribute(ClickstreamAnalytics.Attr.SCREEN_NAME, fragmentA.getClass().getSimpleName()); + event1.addAttribute(ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID, fragmentA.hashCode()); + client.recordViewScreenManually(event1); + + final AnalyticsEvent event2 = + clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SCREEN_VIEW); + event2.addAttribute(ClickstreamAnalytics.Attr.SCREEN_NAME, fragmentA.getClass().getSimpleName()); + event2.addAttribute(ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID, fragmentA.hashCode()); + client.recordViewScreenManually(event2); + 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.assertEquals(fragmentA.getClass().getSimpleName(), + attributes.getString(ReservedAttribute.SCREEN_NAME)); + + cursor.moveToPrevious(); + String eventString2 = cursor.getString(2); + JSONObject jsonObject2 = new JSONObject(eventString2); + String eventName2 = jsonObject2.getString("event_type"); + assertNotEquals(Event.PresetEvent.SCREEN_VIEW, eventName2); + } + } + /** * test app version not update. *