Skip to content

Commit c808543

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: add preset event app end (#35)
Co-authored-by: xiaoweii <[email protected]>
1 parent d810af5 commit c808543

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Life
123123
if (event == Lifecycle.Event.ON_STOP) {
124124
LOG.debug("Application entered the background.");
125125
autoRecordEventClient.recordUserEngagement();
126+
autoRecordEventClient.handleAppEnd();
126127
sessionClient.storeSession();
127128
autoRecordEventClient.flushEvents();
128129
} else if (event == Lifecycle.Event.ON_START) {

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ private void checkOSVersionUpdate() {
206206
}
207207

208208
/**
209-
* handle the first open event.
209+
* handle the app start event.
210210
*/
211211
public void handleAppStart() {
212-
checkAppVersionUpdate();
213-
checkOSVersionUpdate();
212+
if (isFirstTime) {
213+
checkAppVersionUpdate();
214+
checkOSVersionUpdate();
215+
}
214216
if (isFirstOpen) {
215217
final AnalyticsEvent event =
216218
this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.FIRST_OPEN);
@@ -223,10 +225,23 @@ public void handleAppStart() {
223225
event.addAttribute(Event.ReservedAttribute.IS_FIRST_TIME, isFirstTime);
224226
event.addAttribute(Event.ReservedAttribute.SCREEN_NAME, ScreenRefererTool.getCurrentScreenName());
225227
event.addAttribute(Event.ReservedAttribute.SCREEN_ID, ScreenRefererTool.getCurrentScreenId());
228+
event.addAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID, ScreenRefererTool.getCurrentScreenUniqueId());
226229
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
227230
isFirstTime = false;
228231
}
229232

233+
/**
234+
* handle the app end event.
235+
*/
236+
public void handleAppEnd() {
237+
final AnalyticsEvent event =
238+
this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.APP_END);
239+
event.addAttribute(Event.ReservedAttribute.SCREEN_NAME, ScreenRefererTool.getCurrentScreenName());
240+
event.addAttribute(Event.ReservedAttribute.SCREEN_ID, ScreenRefererTool.getCurrentScreenId());
241+
event.addAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID, ScreenRefererTool.getCurrentScreenUniqueId());
242+
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
243+
}
244+
230245
/**
231246
* setter for isEntrances.
232247
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ public static final class PresetEvent {
311311
*/
312312
public static final String APP_START = "_app_start";
313313

314+
/**
315+
* The eventType recorded for app end when app move to background.
316+
*/
317+
public static final String APP_END = "_app_end";
318+
314319
/**
315320
* The user engagement event when the app is in the foreground at least one second.
316321
*/

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public void testUserEngagementSuccess() throws Exception {
125125
assertEquals(Event.PresetEvent.APP_START, eventList.get(1));
126126
assertEquals(Event.PresetEvent.SESSION_START, eventList.get(2));
127127
assertEquals(Event.PresetEvent.USER_ENGAGEMENT, eventList.get(3));
128+
assertEquals(Event.PresetEvent.APP_END, eventList.get(4));
128129
}
129130
}
130131

@@ -617,6 +618,32 @@ public void testHandleAppStart() throws Exception {
617618
}
618619
}
619620

621+
/**
622+
* test app end event.
623+
*
624+
* @throws Exception exception.
625+
*/
626+
@Test
627+
public void testAppEnd() throws Exception {
628+
Activity activity = mock(Activity.class);
629+
Bundle bundle = mock(Bundle.class);
630+
callbacks.onActivityCreated(activity, bundle);
631+
callbacks.onActivityStarted(activity);
632+
callbacks.onActivityResumed(activity);
633+
client.handleAppEnd();
634+
try (Cursor cursor = dbUtil.queryAllEvents()) {
635+
cursor.moveToLast();
636+
String eventString = cursor.getString(2);
637+
JSONObject jsonObject = new JSONObject(eventString);
638+
String eventType = jsonObject.getString("event_type");
639+
JSONObject attributes = jsonObject.getJSONObject("attributes");
640+
assertEquals(Event.PresetEvent.APP_END, eventType);
641+
assertTrue(attributes.has(ReservedAttribute.SCREEN_NAME));
642+
assertTrue(attributes.has(ReservedAttribute.SCREEN_ID));
643+
assertTrue(attributes.has(ReservedAttribute.SCREEN_UNIQUE_ID));
644+
}
645+
}
646+
620647
/**
621648
* test case for app move to background with flush event.
622649
*

0 commit comments

Comments
 (0)