Skip to content

Commit

Permalink
feat: add screen view event after session start (#67)
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 Mar 1, 2024
1 parent a828261 commit f9d9e57
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@

import com.amazonaws.logging.Log;
import com.amazonaws.logging.LogFactory;
import software.aws.solution.clickstream.client.AnalyticsClient;
import software.aws.solution.clickstream.client.AnalyticsEvent;
import software.aws.solution.clickstream.client.AutoRecordEventClient;
import software.aws.solution.clickstream.client.ClickstreamManager;
import software.aws.solution.clickstream.client.Event;
import software.aws.solution.clickstream.client.ScreenRefererTool;
import software.aws.solution.clickstream.client.SessionClient;
import software.aws.solution.clickstream.client.util.StringUtil;

/**
* Tracks when the host application enters or leaves foreground.
Expand All @@ -41,6 +44,7 @@ final class ActivityLifecycleManager implements Application.ActivityLifecycleCal
private static boolean isFromForeground;
private final SessionClient sessionClient;
private final AutoRecordEventClient autoRecordEventClient;
private final AnalyticsClient analyticsClient;

/**
* Constructor. Registers to receive activity lifecycle events.
Expand All @@ -49,6 +53,7 @@ final class ActivityLifecycleManager implements Application.ActivityLifecycleCal
*/
ActivityLifecycleManager(final ClickstreamManager clickstreamManager) {
this.sessionClient = clickstreamManager.getSessionClient();
this.analyticsClient = clickstreamManager.getAnalyticsClient();
this.autoRecordEventClient = clickstreamManager.getAutoRecordEventClient();
}

Expand Down Expand Up @@ -145,11 +150,27 @@ public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Life
LOG.debug("Application entered the foreground.");
isFromForeground = true;
autoRecordEventClient.handleAppStart();
autoRecordEventClient.updateStartEngageTimestamp();
boolean isNewSession = sessionClient.initialSession();
if (isNewSession) {
autoRecordEventClient.setIsEntrances();
recordScreenViewAfterSessionStart();
}
autoRecordEventClient.updateStartEngageTimestamp();
}
}

private void recordScreenViewAfterSessionStart() {
if (!StringUtil.isNullOrEmpty(ScreenRefererTool.getCurrentScreenName())) {
String screenName = ScreenRefererTool.getCurrentScreenName();
String screenId = ScreenRefererTool.getCurrentScreenId();
String screenUniqueId = ScreenRefererTool.getCurrentScreenUniqueId();
ScreenRefererTool.clear();
AnalyticsEvent clickstreamEvent =
analyticsClient.createEvent(Event.PresetEvent.SCREEN_VIEW);
clickstreamEvent.addAttribute(Event.ReservedAttribute.SCREEN_NAME, screenName);
clickstreamEvent.addAttribute(Event.ReservedAttribute.SCREEN_ID, screenId);
clickstreamEvent.addAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID, screenUniqueId);
autoRecordEventClient.recordViewScreenManually(clickstreamEvent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,16 @@ public static boolean isSameScreen(String screenName, String screenUniqueId) {
mCurrentScreenName.equals(screenName) &&
(mCurrentScreenUniqueId == null || mCurrentScreenUniqueId.equals(screenUniqueId));
}

/**
* method for clear cached screen information.
*/
public static void clear() {
mCurrentScreenId = null;
mCurrentScreenName = null;
mCurrentScreenUniqueId = null;
mPreviousScreenId = null;
mPreviousScreenName = null;
mPreviousScreenUniqueId = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static JSONObject getNewUserInfo(final AndroidPreferences preferences, St
userInfo.put("user_first_touch_timestamp", getCurrentUserFirstTouchTimestamp(preferences));
userUniqueIdObject.put(userId, userInfo);
preferences.putString(USER_UNIQUE_ID_MAP, userUniqueIdObject.toString());
} else if (userUniqueIdJsonString.contains(userId)) {
} else if (userUniqueIdObject.has(userId)) {
// switch to old user.
userInfo = userUniqueIdObject.getJSONObject(userId);
setCurrentUserUniqueId(preferences, userInfo.getString("user_unique_id"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,46 @@ public void testBackgroundRequest() throws Exception {
assertEquals(1, ((ThreadPoolExecutor) executorService).getActiveCount());
}


/**
* test hide page and reopen page after session timeout and will record page view event.
*
* @throws Exception exception.
*/
@Test
public void testSessionTimeoutAfterReopenTheApp() throws Exception {
clickstreamContext.getClickstreamConfiguration().withSessionTimeoutDuration(0);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
Activity activityA = mock(ActivityA.class);
Bundle bundle = mock(Bundle.class);
// Record activityA screen view
callbacks.onActivityCreated(activityA, bundle);
callbacks.onActivityStarted(activityA);
callbacks.onActivityResumed(activityA);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
Thread.sleep(100);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
try (Cursor cursor = dbUtil.queryAllEvents()) {
cursor.moveToLast();
String eventString = cursor.getString(2);
JSONObject jsonObject = new JSONObject(eventString);
String eventType = jsonObject.getString("event_type");
JSONObject attributes = jsonObject.getJSONObject("attributes");
assertEquals(Event.PresetEvent.SCREEN_VIEW, eventType);
assertTrue(attributes.has(ReservedAttribute.SCREEN_NAME));
assertTrue(attributes.has(ReservedAttribute.SCREEN_UNIQUE_ID));
assertFalse(attributes.has(ReservedAttribute.PREVIOUS_SCREEN_NAME));
assertFalse(attributes.has(ReservedAttribute.PREVIOUS_SCREEN_UNIQUE_ID));
assertEquals(1, attributes.getInt(ReservedAttribute.ENTRANCES));

cursor.moveToPrevious();
String eventString2 = cursor.getString(2);
JSONObject jsonObject2 = new JSONObject(eventString2);
String eventName2 = jsonObject2.getString("event_type");
assertEquals(Event.PresetEvent.SESSION_START, eventName2);
}
}

/**
* test init autoRecordEventClient with null analyticsClient.
*/
Expand All @@ -960,12 +1000,7 @@ 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);
ScreenRefererTool.clear();
dbUtil.closeDB();
}

Expand Down
1 change: 0 additions & 1 deletion integrationtest/devicefarm/logcat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def test_screen_view(self, path):
(event for event in self.recorded_events if '_screen_view' in event.get('event_name', '')),
None)
assert screen_view_event['event_json'].get('attributes')['_entrances'] == 1
assert '_screen_id' in screen_view_event['event_json'].get('attributes')
assert '_screen_name' in screen_view_event['event_json'].get('attributes')
assert '_screen_unique_id' in screen_view_event['event_json'].get('attributes')

Expand Down

0 comments on commit f9d9e57

Please sign in to comment.