Skip to content

fix: filter two consecutive identical scree view events #65

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

Merged
merged 1 commit into from
Feb 21, 2024
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 @@ -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