Skip to content

Commit f9d9e57

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: add screen view event after session start (#67)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent a828261 commit f9d9e57

File tree

5 files changed

+76
-9
lines changed

5 files changed

+76
-9
lines changed

clickstream/src/main/java/software/aws/solution/clickstream/ActivityLifecycleManager.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626

2727
import com.amazonaws.logging.Log;
2828
import com.amazonaws.logging.LogFactory;
29+
import software.aws.solution.clickstream.client.AnalyticsClient;
2930
import software.aws.solution.clickstream.client.AnalyticsEvent;
3031
import software.aws.solution.clickstream.client.AutoRecordEventClient;
3132
import software.aws.solution.clickstream.client.ClickstreamManager;
33+
import software.aws.solution.clickstream.client.Event;
3234
import software.aws.solution.clickstream.client.ScreenRefererTool;
3335
import software.aws.solution.clickstream.client.SessionClient;
36+
import software.aws.solution.clickstream.client.util.StringUtil;
3437

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

4549
/**
4650
* Constructor. Registers to receive activity lifecycle events.
@@ -49,6 +53,7 @@ final class ActivityLifecycleManager implements Application.ActivityLifecycleCal
4953
*/
5054
ActivityLifecycleManager(final ClickstreamManager clickstreamManager) {
5155
this.sessionClient = clickstreamManager.getSessionClient();
56+
this.analyticsClient = clickstreamManager.getAnalyticsClient();
5257
this.autoRecordEventClient = clickstreamManager.getAutoRecordEventClient();
5358
}
5459

@@ -145,11 +150,27 @@ public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Life
145150
LOG.debug("Application entered the foreground.");
146151
isFromForeground = true;
147152
autoRecordEventClient.handleAppStart();
153+
autoRecordEventClient.updateStartEngageTimestamp();
148154
boolean isNewSession = sessionClient.initialSession();
149155
if (isNewSession) {
150156
autoRecordEventClient.setIsEntrances();
157+
recordScreenViewAfterSessionStart();
151158
}
152-
autoRecordEventClient.updateStartEngageTimestamp();
159+
}
160+
}
161+
162+
private void recordScreenViewAfterSessionStart() {
163+
if (!StringUtil.isNullOrEmpty(ScreenRefererTool.getCurrentScreenName())) {
164+
String screenName = ScreenRefererTool.getCurrentScreenName();
165+
String screenId = ScreenRefererTool.getCurrentScreenId();
166+
String screenUniqueId = ScreenRefererTool.getCurrentScreenUniqueId();
167+
ScreenRefererTool.clear();
168+
AnalyticsEvent clickstreamEvent =
169+
analyticsClient.createEvent(Event.PresetEvent.SCREEN_VIEW);
170+
clickstreamEvent.addAttribute(Event.ReservedAttribute.SCREEN_NAME, screenName);
171+
clickstreamEvent.addAttribute(Event.ReservedAttribute.SCREEN_ID, screenId);
172+
clickstreamEvent.addAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID, screenUniqueId);
173+
autoRecordEventClient.recordViewScreenManually(clickstreamEvent);
153174
}
154175
}
155176
}

clickstream/src/main/java/software/aws/solution/clickstream/client/ScreenRefererTool.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,16 @@ public static boolean isSameScreen(String screenName, String screenUniqueId) {
126126
mCurrentScreenName.equals(screenName) &&
127127
(mCurrentScreenUniqueId == null || mCurrentScreenUniqueId.equals(screenUniqueId));
128128
}
129+
130+
/**
131+
* method for clear cached screen information.
132+
*/
133+
public static void clear() {
134+
mCurrentScreenId = null;
135+
mCurrentScreenName = null;
136+
mCurrentScreenUniqueId = null;
137+
mPreviousScreenId = null;
138+
mPreviousScreenName = null;
139+
mPreviousScreenUniqueId = null;
140+
}
129141
}

clickstream/src/main/java/software/aws/solution/clickstream/client/util/PreferencesUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static JSONObject getNewUserInfo(final AndroidPreferences preferences, St
113113
userInfo.put("user_first_touch_timestamp", getCurrentUserFirstTouchTimestamp(preferences));
114114
userUniqueIdObject.put(userId, userInfo);
115115
preferences.putString(USER_UNIQUE_ID_MAP, userUniqueIdObject.toString());
116-
} else if (userUniqueIdJsonString.contains(userId)) {
116+
} else if (userUniqueIdObject.has(userId)) {
117117
// switch to old user.
118118
userInfo = userUniqueIdObject.getJSONObject(userId);
119119
setCurrentUserUniqueId(preferences, userInfo.getString("user_unique_id"));

clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,46 @@ public void testBackgroundRequest() throws Exception {
946946
assertEquals(1, ((ThreadPoolExecutor) executorService).getActiveCount());
947947
}
948948

949+
950+
/**
951+
* test hide page and reopen page after session timeout and will record page view event.
952+
*
953+
* @throws Exception exception.
954+
*/
955+
@Test
956+
public void testSessionTimeoutAfterReopenTheApp() throws Exception {
957+
clickstreamContext.getClickstreamConfiguration().withSessionTimeoutDuration(0);
958+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
959+
Activity activityA = mock(ActivityA.class);
960+
Bundle bundle = mock(Bundle.class);
961+
// Record activityA screen view
962+
callbacks.onActivityCreated(activityA, bundle);
963+
callbacks.onActivityStarted(activityA);
964+
callbacks.onActivityResumed(activityA);
965+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
966+
Thread.sleep(100);
967+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
968+
try (Cursor cursor = dbUtil.queryAllEvents()) {
969+
cursor.moveToLast();
970+
String eventString = cursor.getString(2);
971+
JSONObject jsonObject = new JSONObject(eventString);
972+
String eventType = jsonObject.getString("event_type");
973+
JSONObject attributes = jsonObject.getJSONObject("attributes");
974+
assertEquals(Event.PresetEvent.SCREEN_VIEW, eventType);
975+
assertTrue(attributes.has(ReservedAttribute.SCREEN_NAME));
976+
assertTrue(attributes.has(ReservedAttribute.SCREEN_UNIQUE_ID));
977+
assertFalse(attributes.has(ReservedAttribute.PREVIOUS_SCREEN_NAME));
978+
assertFalse(attributes.has(ReservedAttribute.PREVIOUS_SCREEN_UNIQUE_ID));
979+
assertEquals(1, attributes.getInt(ReservedAttribute.ENTRANCES));
980+
981+
cursor.moveToPrevious();
982+
String eventString2 = cursor.getString(2);
983+
JSONObject jsonObject2 = new JSONObject(eventString2);
984+
String eventName2 = jsonObject2.getString("event_type");
985+
assertEquals(Event.PresetEvent.SESSION_START, eventName2);
986+
}
987+
}
988+
949989
/**
950990
* test init autoRecordEventClient with null analyticsClient.
951991
*/
@@ -960,12 +1000,7 @@ public void testInitAutoRecordEventClientWithNullAnalyticsClient() {
9601000
*/
9611001
@After
9621002
public void tearDown() {
963-
ScreenRefererTool.setCurrentScreenName(null);
964-
ScreenRefererTool.setCurrentScreenName(null);
965-
ScreenRefererTool.setCurrentScreenId(null);
966-
ScreenRefererTool.setCurrentScreenId(null);
967-
ScreenRefererTool.setCurrentScreenUniqueId(null);
968-
ScreenRefererTool.setCurrentScreenUniqueId(null);
1003+
ScreenRefererTool.clear();
9691004
dbUtil.closeDB();
9701005
}
9711006

integrationtest/devicefarm/logcat_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def test_screen_view(self, path):
5454
(event for event in self.recorded_events if '_screen_view' in event.get('event_name', '')),
5555
None)
5656
assert screen_view_event['event_json'].get('attributes')['_entrances'] == 1
57-
assert '_screen_id' in screen_view_event['event_json'].get('attributes')
5857
assert '_screen_name' in screen_view_event['event_json'].get('attributes')
5958
assert '_screen_unique_id' in screen_view_event['event_json'].get('attributes')
6059

0 commit comments

Comments
 (0)