Skip to content

Commit 41ff6e1

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: remove the engagement time attribute in first screen view (#43)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent ba43cfb commit 41ff6e1

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void onActivityResumed(final Activity activity) {
8383
boolean isSameScreen =
8484
ScreenRefererTool.isSameScreen(activity.getClass().getCanonicalName(), activity.getClass().getSimpleName(),
8585
autoRecordEventClient.getScreenUniqueId(activity));
86-
if (!isSameScreen) {
86+
if (ScreenRefererTool.getCurrentScreenName() != null && !isSameScreen) {
8787
autoRecordEventClient.recordUserEngagement();
8888
}
8989
autoRecordEventClient.recordViewScreen(activity);
@@ -96,7 +96,6 @@ public void onActivityPaused(final Activity activity) {
9696
// resumed if app regains focus.In either case, app foreground status does not change for the
9797
// purpose of session tracking.
9898
LOG.debug("Activity paused: " + activity.getLocalClassName());
99-
autoRecordEventClient.updateEndEngageTimestamp();
10099
}
101100

102101
@Override

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class AutoRecordEventClient {
3333
*/
3434
private static final Log LOG = LogFactory.getLog(AutoRecordEventClient.class);
3535
private static final int MIN_ENGAGEMENT_TIME = 1000;
36-
private static final int DEVICE_ID_CLIP_LENGTH = 8;
3736
/**
3837
* The context object wraps all the essential information from the app
3938
* that are required.
@@ -56,7 +55,6 @@ public class AutoRecordEventClient {
5655
private boolean isEntrances;
5756

5857
private long startEngageTimestamp;
59-
private long endEngageTimestamp;
6058
private long lastEngageTime;
6159
private final AndroidPreferences preferences;
6260

@@ -107,7 +105,9 @@ public void recordViewScreen(Activity activity) {
107105
event.addAttribute(Event.ReservedAttribute.PREVIOUS_TIMESTAMP, lastScreenViewEventTimestamp);
108106
}
109107
event.addAttribute(Event.ReservedAttribute.ENTRANCES, isEntrances ? 1 : 0);
110-
event.addAttribute(Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP, lastEngageTime);
108+
if (lastEngageTime > 0) {
109+
event.addAttribute(Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP, lastEngageTime);
110+
}
111111
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
112112
PreferencesUtil.savePreviousScreenViewTimestamp(preferences, currentTimestamp);
113113
isEntrances = false;
@@ -130,7 +130,10 @@ public String getScreenUniqueId(Activity activity) {
130130
* record user engagement event.
131131
*/
132132
public void recordUserEngagement() {
133-
lastEngageTime = endEngageTimestamp - startEngageTimestamp;
133+
if (startEngageTimestamp == 0) {
134+
return;
135+
}
136+
lastEngageTime = System.currentTimeMillis() - startEngageTimestamp;
134137
if (clickstreamContext.getClickstreamConfiguration().isTrackUserEngagementEvents() &&
135138
lastEngageTime > MIN_ENGAGEMENT_TIME) {
136139
final AnalyticsEvent event =
@@ -154,13 +157,6 @@ public void updateStartEngageTimestamp() {
154157
startEngageTimestamp = System.currentTimeMillis();
155158
}
156159

157-
/**
158-
* update end engage timestamp.
159-
*/
160-
public void updateEndEngageTimestamp() {
161-
endEngageTimestamp = System.currentTimeMillis();
162-
}
163-
164160
/**
165161
* check and record _app_update event.
166162
*/

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public void setup() {
103103
public void testUserEngagementSuccess() throws Exception {
104104
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
105105
Thread.sleep(1100);
106-
client.updateEndEngageTimestamp();
107106
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
108107
try (Cursor cursor = dbUtil.queryAllEvents()) {
109108
List<String> eventList = new ArrayList<>();
@@ -198,7 +197,7 @@ public void testViewOneScreenEvent() throws Exception {
198197
Assert.assertFalse(attributes.has(ReservedAttribute.PREVIOUS_SCREEN_NAME));
199198
Assert.assertFalse(attributes.has(ReservedAttribute.PREVIOUS_SCREEN_ID));
200199
Assert.assertEquals(1, attributes.getInt(ReservedAttribute.ENTRANCES));
201-
Assert.assertTrue(attributes.has(ReservedAttribute.ENGAGEMENT_TIMESTAMP));
200+
Assert.assertFalse(attributes.has(ReservedAttribute.ENGAGEMENT_TIMESTAMP));
202201
Assert.assertFalse(attributes.has(ReservedAttribute.PREVIOUS_TIMESTAMP));
203202
}
204203
}
@@ -350,7 +349,8 @@ public void testSameScreenViewAndRecordLastEngagementTime() throws Exception {
350349
callbacks.onActivityResumed(activityA);
351350
Thread.sleep(200);
352351
callbacks.onActivityPaused(activityA);
353-
352+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
353+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
354354
callbacks.onActivityCreated(activityA, bundle);
355355
callbacks.onActivityStarted(activityA);
356356
callbacks.onActivityResumed(activityA);
@@ -700,8 +700,11 @@ public void testInitAutoRecordEventClientWithNullAnalyticsClient() {
700700
@After
701701
public void tearDown() {
702702
ScreenRefererTool.setCurrentScreenName(null);
703+
ScreenRefererTool.setCurrentScreenName(null);
704+
ScreenRefererTool.setCurrentScreenId(null);
703705
ScreenRefererTool.setCurrentScreenId(null);
704706
ScreenRefererTool.setCurrentScreenUniqueId(null);
707+
ScreenRefererTool.setCurrentScreenUniqueId(null);
705708
dbUtil.closeDB();
706709
}
707710

0 commit comments

Comments
 (0)