Skip to content

Commit 5c61666

Browse files
authored
feat: support record screen view event manually (#63)
1 parent 5197b2a commit 5c61666

File tree

10 files changed

+335
-11
lines changed

10 files changed

+335
-11
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@ ClickstreamEvent event = ClickstreamEvent.builder()
204204
ClickstreamAnalytics.recordEvent(event);
205205
```
206206

207+
#### Record Screen View events manually
208+
209+
By default, SDK will automatically track the preset `_screen_view` event when Activity triggers `onResume`.
210+
211+
You can also manually record screen view events whether or not automatic screen view tracking is enabled, add the following code to record a screen view event with two attributes.
212+
213+
* `SCREEN_NAME` Required. Your screen's name.
214+
* `SCREEN_UNIQUE_ID` Optional. Set the hashcode of your Activity, Fragment, or View. If you do not set, SDK will set a default value based on the current Activity's hashcode.
215+
216+
```java
217+
import software.aws.solution.clickstream.ClickstreamAnalytcs;
218+
219+
ClickstreamEvent event = ClickstreamEvent.builder()
220+
.name(ClickstreamAnalytics.Event.SCREEN_VIEW)
221+
.add(ClickstreamAnalytics.Attr.SCREEN_NAME, "HomeFragment")
222+
.add(ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID, String.valueOf(HomeFragment.hashCode()))
223+
.build();
224+
225+
ClickstreamAnalytics.recordEvent(event);
226+
```
227+
207228
#### Log the event json in debug mode
208229

209230
```java

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public synchronized void enable() {
103103
public void recordEvent(@NonNull String eventName) {
104104
final AnalyticsEvent event = analyticsClient.createEvent(eventName);
105105
if (event != null) {
106-
analyticsClient.recordEvent(event);
106+
recordAnalyticsEvent(event);
107107
}
108108
}
109109

@@ -121,7 +121,15 @@ public void recordEvent(@NonNull AnalyticsEventBehavior analyticsEvent) {
121121
}
122122
}
123123
clickstreamEvent.addItems(event.getItems());
124-
analyticsClient.recordEvent(clickstreamEvent);
124+
recordAnalyticsEvent(clickstreamEvent);
125+
}
126+
}
127+
128+
private void recordAnalyticsEvent(AnalyticsEvent event) {
129+
if (event.getEventType().equals(Event.PresetEvent.SCREEN_VIEW)) {
130+
activityLifecycleManager.onScreenViewManually(event);
131+
} else {
132+
analyticsClient.recordEvent(event);
125133
}
126134
}
127135

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.amazonaws.logging.Log;
2828
import com.amazonaws.logging.LogFactory;
29+
import software.aws.solution.clickstream.client.AnalyticsEvent;
2930
import software.aws.solution.clickstream.client.AutoRecordEventClient;
3031
import software.aws.solution.clickstream.client.ClickstreamManager;
3132
import software.aws.solution.clickstream.client.ScreenRefererTool;
@@ -92,10 +93,18 @@ public void onActivityResumed(final Activity activity) {
9293
autoRecordEventClient.resetLastEngageTime();
9394
}
9495
}
95-
autoRecordEventClient.recordViewScreen(activity);
96+
autoRecordEventClient.recordViewScreenAutomatically(activity);
9697
isFromForeground = false;
9798
}
9899

100+
/**
101+
* Handle Screen View triggered manually.
102+
* @param event the screen view event
103+
*/
104+
public void onScreenViewManually(AnalyticsEvent event) {
105+
autoRecordEventClient.recordViewScreenManually(event);
106+
}
107+
99108
@Override
100109
public void onActivityPaused(final Activity activity) {
101110
// onPause is always followed by onStop except when the app is interrupted by an event such

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
import com.amazonaws.logging.LogFactory;
2626
import software.aws.solution.clickstream.client.AnalyticsClient;
2727
import software.aws.solution.clickstream.client.ClickstreamConfiguration;
28-
import software.aws.solution.clickstream.client.Event;
28+
import software.aws.solution.clickstream.client.Event.PresetEvent;
29+
import software.aws.solution.clickstream.client.Event.ReservedAttribute;
2930
import software.aws.solution.clickstream.client.util.ThreadUtil;
3031

3132
/**
@@ -101,7 +102,7 @@ public static void deleteGlobalAttributes(@NonNull String... attributeName) {
101102
* @param userProfile user
102103
*/
103104
public static void addUserAttributes(ClickstreamUserAttribute userProfile) {
104-
Amplify.Analytics.identifyUser(Event.ReservedAttribute.USER_ID_UNSET, userProfile);
105+
Amplify.Analytics.identifyUser(ReservedAttribute.USER_ID_UNSET, userProfile);
105106
}
106107

107108
/**
@@ -213,4 +214,31 @@ public static class Item {
213214
*/
214215
public static final String ITEM_CATEGORY5 = "item_category5";
215216
}
217+
218+
/**
219+
* Preset Event.
220+
*/
221+
public static class Event {
222+
223+
/**
224+
* screen view.
225+
*/
226+
public static final String SCREEN_VIEW = PresetEvent.SCREEN_VIEW;
227+
}
228+
229+
/**
230+
* Preset Attributes.
231+
*/
232+
public static class Attr {
233+
234+
/**
235+
* screen name.
236+
*/
237+
public static final String SCREEN_NAME = ReservedAttribute.SCREEN_NAME;
238+
239+
/**
240+
* screen unique id.
241+
*/
242+
public static final String SCREEN_UNIQUE_ID = ReservedAttribute.SCREEN_UNIQUE_ID;
243+
}
216244
}

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public AutoRecordEventClient(@NonNull final ClickstreamContext clickstreamContex
7878
*
7979
* @param activity the activity to record.
8080
*/
81-
public void recordViewScreen(Activity activity) {
81+
public void recordViewScreenAutomatically(Activity activity) {
8282
if (!clickstreamContext.getClickstreamConfiguration().isTrackScreenViewEvents()) {
8383
return;
8484
}
@@ -93,6 +93,39 @@ public void recordViewScreen(Activity activity) {
9393
ScreenRefererTool.setCurrentScreenUniqueId(screenUniqueId);
9494
final AnalyticsEvent event =
9595
this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SCREEN_VIEW);
96+
recordScreenViewEvent(event);
97+
}
98+
99+
/**
100+
* record view screen event manually.
101+
*
102+
* @param event the screen view event to be recorded.
103+
*/
104+
public void recordViewScreenManually(AnalyticsEvent event) {
105+
String screenName = event.getStringAttribute(Event.ReservedAttribute.SCREEN_NAME);
106+
if (screenName != null) {
107+
if (ScreenRefererTool.getCurrentScreenName() != null) {
108+
recordUserEngagement();
109+
}
110+
ScreenRefererTool.setCurrentScreenName(screenName);
111+
String screenUniqueId = event.getStringAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID);
112+
if (screenUniqueId != null) {
113+
ScreenRefererTool.setCurrentScreenUniqueId(screenUniqueId);
114+
}
115+
recordScreenViewEvent(event);
116+
} else {
117+
LOG.error("record an _screen_view event without the required screen name attribute");
118+
final AnalyticsEvent errorEvent =
119+
this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.CLICKSTREAM_ERROR);
120+
errorEvent.addAttribute(Event.ReservedAttribute.ERROR_CODE,
121+
Event.ErrorCode.SCREEN_VIEW_MISSING_SCREEN_NAME);
122+
errorEvent.addAttribute(Event.ReservedAttribute.ERROR_MESSAGE,
123+
"record an _screen_view event without the required screen name attribute");
124+
this.clickstreamContext.getAnalyticsClient().recordEvent(errorEvent);
125+
}
126+
}
127+
128+
private void recordScreenViewEvent(AnalyticsEvent event) {
96129
long currentTimestamp = event.getEventTimestamp();
97130
startEngageTimestamp = currentTimestamp;
98131
event.addAttribute(Event.ReservedAttribute.SCREEN_ID, ScreenRefererTool.getCurrentScreenId());
@@ -111,8 +144,6 @@ public void recordViewScreen(Activity activity) {
111144
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
112145
PreferencesUtil.savePreviousScreenViewTimestamp(preferences, currentTimestamp);
113146
isEntrances = false;
114-
LOG.debug("record an _screen_view event, screenId:" + screenId + "lastScreenId:" +
115-
ScreenRefererTool.getPreviousScreenId());
116147
}
117148

118149
/**

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
@@ -144,6 +144,11 @@ public static final class ErrorCode {
144144
*/
145145
public static final int ITEM_CUSTOM_ATTRIBUTE_KEY_INVALID = 4005;
146146

147+
/**
148+
* screen view event missing screen name attribute.
149+
*/
150+
public static final int SCREEN_VIEW_MISSING_SCREEN_NAME = 5001;
151+
147152
private ErrorCode() {
148153
}
149154
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,6 @@ public void testScreenView() {
150150
callbacks.onActivityCreated(activity, bundle);
151151
callbacks.onActivityStarted(activity);
152152
callbacks.onActivityResumed(activity);
153-
verify(autoRecordEventClient).recordViewScreen(activity);
153+
verify(autoRecordEventClient).recordViewScreenAutomatically(activity);
154154
}
155155
}

0 commit comments

Comments
 (0)