Skip to content

Commit

Permalink
fix: filter two consecutive identical scree view events (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei authored Feb 21, 2024
1 parent 5c61666 commit 18c6b8c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 18c6b8c

Please sign in to comment.