From cf8b7d78ff56a9fa82af119b3f13384ba69f42d4 Mon Sep 17 00:00:00 2001 From: xiaoweii Date: Thu, 29 Feb 2024 16:15:44 +0800 Subject: [PATCH] feat: add screen view event after session start --- .../clickstream/ActivityLifecycleManager.java | 21 +++++++++ .../clickstream/client/ScreenRefererTool.java | 12 +++++ .../client/util/PreferencesUtil.java | 2 +- .../AutoRecordEventClientTest.java | 46 ++++++++++++++++--- 4 files changed, 74 insertions(+), 7 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 29f0009..6bc877a 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/ActivityLifecycleManager.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/ActivityLifecycleManager.java @@ -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. @@ -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. @@ -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(); } @@ -148,8 +153,24 @@ public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Life 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); + } + } } 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 ddbc73f..6b08526 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 @@ -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; + } } diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/util/PreferencesUtil.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/util/PreferencesUtil.java index 7d46aab..7233545 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/util/PreferencesUtil.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/util/PreferencesUtil.java @@ -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")); 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 008b86b..94be76f 100644 --- a/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java +++ b/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java @@ -946,6 +946,45 @@ 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)); + + 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. */ @@ -960,12 +999,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(); }