From 088f89bd17b2d8a451e0e3d92d91b7bf67104009 Mon Sep 17 00:00:00 2001 From: Xiaowei Zhu <33129495+zhu-xiaowei@users.noreply.github.com> Date: Tue, 12 Sep 2023 19:17:22 +0800 Subject: [PATCH] feat: add preset attributes screen name and screen unique id (#38) Co-authored-by: xiaoweii --- .../solution/clickstream/client/AnalyticsEvent.java | 11 +++++++++++ .../clickstream/client/AutoRecordEventClient.java | 11 ----------- .../clickstream/AutoRecordEventClientTest.java | 5 ++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/AnalyticsEvent.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/AnalyticsEvent.java index 6febd28..3259a6e 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/AnalyticsEvent.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/AnalyticsEvent.java @@ -482,6 +482,17 @@ public JSONObject toJSONObject() { } } + String screenName = ScreenRefererTool.getCurrentScreenName(); + String screenUniqueId = ScreenRefererTool.getCurrentScreenUniqueId(); + if (screenName != null) { + try { + attributes.put(Event.ReservedAttribute.SCREEN_NAME, screenName); + attributes.put(Event.ReservedAttribute.SCREEN_UNIQUE_ID, screenUniqueId); + } catch (final JSONException jsonException) { + LOG.error("Error serializing session information " + jsonException.getMessage()); + } + } + // **************************************************** // ====SDK Details Attributes -- Prefix with 'sdk_'==== // **************************************************** diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java index 656683a..ac3e662 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/AutoRecordEventClient.java @@ -97,9 +97,7 @@ public void recordViewScreen(Activity activity) { this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SCREEN_VIEW); long currentTimestamp = event.getEventTimestamp(); startEngageTimestamp = currentTimestamp; - event.addAttribute(Event.ReservedAttribute.SCREEN_NAME, ScreenRefererTool.getCurrentScreenName()); event.addAttribute(Event.ReservedAttribute.SCREEN_ID, ScreenRefererTool.getCurrentScreenId()); - event.addAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID, ScreenRefererTool.getCurrentScreenUniqueId()); event.addAttribute(Event.ReservedAttribute.PREVIOUS_SCREEN_NAME, ScreenRefererTool.getPreviousScreenName()); event.addAttribute(Event.ReservedAttribute.PREVIOUS_SCREEN_ID, ScreenRefererTool.getPreviousScreenId()); event.addAttribute(Event.ReservedAttribute.PREVIOUS_SCREEN_UNIQUE_ID, @@ -138,9 +136,6 @@ public void recordUserEngagement() { final AnalyticsEvent event = this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.USER_ENGAGEMENT); event.addAttribute(Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP, lastEngageTime); - event.addAttribute(Event.ReservedAttribute.SCREEN_NAME, ScreenRefererTool.getCurrentScreenName()); - event.addAttribute(Event.ReservedAttribute.SCREEN_ID, ScreenRefererTool.getCurrentScreenId()); - event.addAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID, ScreenRefererTool.getCurrentScreenUniqueId()); this.clickstreamContext.getAnalyticsClient().recordEvent(event); } } @@ -222,9 +217,6 @@ public void handleAppStart() { final AnalyticsEvent event = this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.APP_START); event.addAttribute(Event.ReservedAttribute.IS_FIRST_TIME, isFirstTime); - event.addAttribute(Event.ReservedAttribute.SCREEN_NAME, ScreenRefererTool.getCurrentScreenName()); - event.addAttribute(Event.ReservedAttribute.SCREEN_ID, ScreenRefererTool.getCurrentScreenId()); - event.addAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID, ScreenRefererTool.getCurrentScreenUniqueId()); this.clickstreamContext.getAnalyticsClient().recordEvent(event); isFirstTime = false; } @@ -235,9 +227,6 @@ public void handleAppStart() { public void handleAppEnd() { final AnalyticsEvent event = this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.APP_END); - event.addAttribute(Event.ReservedAttribute.SCREEN_NAME, ScreenRefererTool.getCurrentScreenName()); - event.addAttribute(Event.ReservedAttribute.SCREEN_ID, ScreenRefererTool.getCurrentScreenId()); - event.addAttribute(Event.ReservedAttribute.SCREEN_UNIQUE_ID, ScreenRefererTool.getCurrentScreenUniqueId()); this.clickstreamContext.getAnalyticsClient().recordEvent(event); } diff --git a/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java b/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java index 20d7179..a564164 100644 --- a/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java +++ b/clickstream/src/test/java/software/aws/solution/clickstream/AutoRecordEventClientTest.java @@ -637,9 +637,9 @@ public void testHandleAppStart() throws Exception { JSONObject appStart2 = eventList.get(3).getJSONObject("attributes"); assertFalse(appStart2.getBoolean(Event.ReservedAttribute.IS_FIRST_TIME)); assertTrue(appStart2.has(ReservedAttribute.SCREEN_NAME)); - assertTrue(appStart2.has(Event.ReservedAttribute.SCREEN_ID)); + assertTrue(appStart2.has(ReservedAttribute.SCREEN_UNIQUE_ID)); assertEquals(activity1.getClass().getSimpleName(), appStart2.getString(ReservedAttribute.SCREEN_NAME)); - assertEquals(activity1.getClass().getCanonicalName(), appStart2.getString(ReservedAttribute.SCREEN_ID)); + assertEquals(String.valueOf(activity1.hashCode()), appStart2.getString(ReservedAttribute.SCREEN_UNIQUE_ID)); } } @@ -664,7 +664,6 @@ public void testAppEnd() throws Exception { JSONObject attributes = jsonObject.getJSONObject("attributes"); assertEquals(Event.PresetEvent.APP_END, eventType); assertTrue(attributes.has(ReservedAttribute.SCREEN_NAME)); - assertTrue(attributes.has(ReservedAttribute.SCREEN_ID)); assertTrue(attributes.has(ReservedAttribute.SCREEN_UNIQUE_ID)); } }