Skip to content

Commit 18c6b8c

Browse files
authored
fix: filter two consecutive identical scree view events (#65)
1 parent 5c61666 commit 18c6b8c

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ public void onActivityResumed(final Activity activity) {
8383
// An activity came to foreground. Application potentially entered foreground as well
8484
// if there were no other activities in the foreground.
8585
LOG.debug("Activity resumed: " + activity.getLocalClassName());
86-
boolean isSameScreen =
87-
ScreenRefererTool.isSameScreen(activity.getClass().getCanonicalName(), activity.getClass().getSimpleName(),
88-
autoRecordEventClient.getScreenUniqueId(activity));
86+
boolean isSameScreen = ScreenRefererTool.isSameScreen(activity.getClass().getSimpleName(),
87+
autoRecordEventClient.getScreenUniqueId(activity));
8988
if (ScreenRefererTool.getCurrentScreenName() != null && !isSameScreen) {
9089
if (!isFromForeground) {
9190
autoRecordEventClient.recordUserEngagement();
@@ -99,6 +98,7 @@ public void onActivityResumed(final Activity activity) {
9998

10099
/**
101100
* Handle Screen View triggered manually.
101+
*
102102
* @param event the screen view event
103103
*/
104104
public void onScreenViewManually(AnalyticsEvent event) {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void recordViewScreenAutomatically(Activity activity) {
8585
String screenId = activity.getClass().getCanonicalName();
8686
String screenName = activity.getClass().getSimpleName();
8787
String screenUniqueId = getScreenUniqueId(activity);
88-
if (ScreenRefererTool.isSameScreen(screenId, screenName, screenUniqueId)) {
88+
if (ScreenRefererTool.isSameScreen(screenName, screenUniqueId)) {
8989
return;
9090
}
9191
ScreenRefererTool.setCurrentScreenId(screenId);
@@ -104,11 +104,14 @@ public void recordViewScreenAutomatically(Activity activity) {
104104
public void recordViewScreenManually(AnalyticsEvent event) {
105105
String screenName = event.getStringAttribute(Event.ReservedAttribute.SCREEN_NAME);
106106
if (screenName != null) {
107+
String screenUniqueId = event.getStringAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID);
108+
if (ScreenRefererTool.isSameScreen(screenName, screenUniqueId)) {
109+
return;
110+
}
107111
if (ScreenRefererTool.getCurrentScreenName() != null) {
108112
recordUserEngagement();
109113
}
110114
ScreenRefererTool.setCurrentScreenName(screenName);
111-
String screenUniqueId = event.getStringAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID);
112115
if (screenUniqueId != null) {
113116
ScreenRefererTool.setCurrentScreenUniqueId(screenUniqueId);
114117
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,13 @@ public static String getPreviousScreenUniqueId() {
117117
/**
118118
* Judging that the current screen is the same as the previous screen.
119119
*
120-
* @param screenId current screen id
121120
* @param screenName current screen name
122121
* @param screenUniqueId current screen unique id
123122
* @return the boolean value for is the same screen
124123
*/
125-
public static boolean isSameScreen(String screenId, String screenName, String screenUniqueId) {
126-
return mCurrentScreenId != null
127-
&& mCurrentScreenName != null
128-
&& mCurrentScreenId.equals(screenId)
129-
&& mCurrentScreenName.equals(screenName)
130-
&& mCurrentScreenUniqueId.equals(screenUniqueId);
124+
public static boolean isSameScreen(String screenName, String screenUniqueId) {
125+
return mCurrentScreenName != null &&
126+
mCurrentScreenName.equals(screenName) &&
127+
(mCurrentScreenUniqueId == null || mCurrentScreenUniqueId.equals(screenUniqueId));
131128
}
132129
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
import static org.junit.Assert.assertEquals;
5656
import static org.junit.Assert.assertFalse;
57+
import static org.junit.Assert.assertNotEquals;
5758
import static org.junit.Assert.assertNotNull;
5859
import static org.junit.Assert.assertTrue;
5960
import static org.mockito.Mockito.mock;
@@ -668,6 +669,44 @@ public void testRecordTwoScreenViewWhenAutoTrackIsDisabled() throws Exception {
668669
}
669670
}
670671

672+
/**
673+
* test record two same screen view event manually and will not record the last screen view event.
674+
*
675+
* @throws Exception exception
676+
*/
677+
@Test
678+
public void testRecordTwoSameScreenViewManually() throws Exception {
679+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
680+
Fragment fragmentA = mock(FragmentA.class);
681+
final AnalyticsEvent event1 =
682+
clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SCREEN_VIEW);
683+
event1.addAttribute(ClickstreamAnalytics.Attr.SCREEN_NAME, fragmentA.getClass().getSimpleName());
684+
event1.addAttribute(ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID, fragmentA.hashCode());
685+
client.recordViewScreenManually(event1);
686+
687+
final AnalyticsEvent event2 =
688+
clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SCREEN_VIEW);
689+
event2.addAttribute(ClickstreamAnalytics.Attr.SCREEN_NAME, fragmentA.getClass().getSimpleName());
690+
event2.addAttribute(ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID, fragmentA.hashCode());
691+
client.recordViewScreenManually(event2);
692+
try (Cursor cursor = dbUtil.queryAllEvents()) {
693+
cursor.moveToLast();
694+
String eventString = cursor.getString(2);
695+
JSONObject jsonObject = new JSONObject(eventString);
696+
String eventName = jsonObject.getString("event_type");
697+
assertEquals(Event.PresetEvent.SCREEN_VIEW, eventName);
698+
JSONObject attributes = jsonObject.getJSONObject("attributes");
699+
Assert.assertEquals(fragmentA.getClass().getSimpleName(),
700+
attributes.getString(ReservedAttribute.SCREEN_NAME));
701+
702+
cursor.moveToPrevious();
703+
String eventString2 = cursor.getString(2);
704+
JSONObject jsonObject2 = new JSONObject(eventString2);
705+
String eventName2 = jsonObject2.getString("event_type");
706+
assertNotEquals(Event.PresetEvent.SCREEN_VIEW, eventName2);
707+
}
708+
}
709+
671710
/**
672711
* test app version not update.
673712
*

0 commit comments

Comments
 (0)