Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add screen view event after session start
Browse files Browse the repository at this point in the history
xiaoweii committed Feb 29, 2024
1 parent a828261 commit cf8b7d7
Showing 4 changed files with 74 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
@@ -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"));
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit cf8b7d7

Please sign in to comment.