Skip to content

Commit ad33661

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: the second app start event's sessionId (#70)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent f2f80fd commit ad33661

File tree

6 files changed

+22
-48
lines changed

6 files changed

+22
-48
lines changed

.github/workflows/integration_test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ jobs:
2929
cache: gradle
3030
- name: Build SDK release aar file
3131
run: |
32-
echo ${{ github.event.pull_request.title }}
3332
chmod +x gradlew
3433
./gradlew assembleRelease
3534
- name: Set up JDK 17

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Life
149149
} else if (event == Lifecycle.Event.ON_START) {
150150
LOG.debug("Application entered the foreground.");
151151
isFromForeground = true;
152+
boolean isNewSession = sessionClient.initialSession();
152153
autoRecordEventClient.handleAppStart();
153154
autoRecordEventClient.updateStartEngageTimestamp();
154-
boolean isNewSession = sessionClient.initialSession();
155155
if (isNewSession) {
156+
autoRecordEventClient.handleSessionStart();
156157
autoRecordEventClient.setIsEntrances();
157158
recordScreenViewAfterSessionStart();
158159
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ public void handleAppStart() {
259259
isFirstTime = false;
260260
}
261261

262+
/**
263+
* handle session start events.
264+
*/
265+
public void handleSessionStart() {
266+
final AnalyticsEvent event =
267+
this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SESSION_START);
268+
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
269+
}
270+
262271
/**
263272
* handle the app end event.
264273
*/

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ public SessionClient(@NonNull final ClickstreamContext clickstreamContext) {
6060
public synchronized boolean initialSession() {
6161
session = Session.getInstance(clickstreamContext, session);
6262
this.clickstreamContext.getAnalyticsClient().setSession(session);
63-
if (session.isNewSession()) {
64-
final AnalyticsEvent event =
65-
this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SESSION_START);
66-
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
67-
}
6863
return session.isNewSession();
6964
}
7065

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,15 @@ public void testSessionTimeoutAfterReopenTheApp() throws Exception {
10191019
JSONObject jsonObject2 = new JSONObject(eventString2);
10201020
String eventName2 = jsonObject2.getString("event_type");
10211021
assertEquals(Event.PresetEvent.SESSION_START, eventName2);
1022+
1023+
// assert that the second app start event will have the same session id
1024+
cursor.moveToPrevious();
1025+
String eventString3 = cursor.getString(2);
1026+
JSONObject jsonObject3 = new JSONObject(eventString3);
1027+
String eventName3 = jsonObject3.getString("event_type");
1028+
assertEquals(Event.PresetEvent.APP_START, eventName3);
1029+
assertEquals(jsonObject3.getJSONObject("attributes").getString("_session_id"),
1030+
jsonObject2.getJSONObject("attributes").getString("_session_id"));
10221031
}
10231032
}
10241033

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

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
package software.aws.solution.clickstream;
1717

1818
import android.content.Context;
19-
import android.database.Cursor;
2019
import androidx.test.core.app.ApplicationProvider;
2120

22-
import org.json.JSONObject;
23-
import org.junit.After;
2421
import org.junit.Assert;
2522
import org.junit.Before;
2623
import org.junit.Test;
@@ -31,7 +28,6 @@
3128
import software.aws.solution.clickstream.client.ClickstreamManager;
3229
import software.aws.solution.clickstream.client.Session;
3330
import software.aws.solution.clickstream.client.SessionClient;
34-
import software.aws.solution.clickstream.client.db.ClickstreamDBUtil;
3531
import software.aws.solution.clickstream.util.ReflectUtil;
3632

3733
import static org.mockito.Mockito.mock;
@@ -43,7 +39,6 @@
4339
public class SessionClientTest {
4440
private SessionClient client;
4541
private AnalyticsClient analyticsClient;
46-
private ClickstreamDBUtil dbUtil;
4742
private ClickstreamContext clickstreamContext;
4843

4944
/**
@@ -53,7 +48,6 @@ public class SessionClientTest {
5348
public void setup() {
5449
Context context = ApplicationProvider.getApplicationContext();
5550

56-
dbUtil = new ClickstreamDBUtil(context);
5751
AWSClickstreamPluginConfiguration.Builder configurationBuilder = AWSClickstreamPluginConfiguration.builder();
5852
configurationBuilder.withAppId("demo-app")
5953
.withEndpoint("http://cs-se-serve-1qtj719j88vwn-1291141553.ap-southeast-1.elb.amazonaws.com/collect")
@@ -74,22 +68,12 @@ public void setup() {
7468
*/
7569
@Test
7670
public void testExecuteStart() throws Exception {
77-
client.initialSession();
71+
boolean isNewSession = client.initialSession();
7872
Session session = (Session) ReflectUtil.getFiled(client, "session");
7973
Assert.assertNotNull(session);
8074
Session clientSession = (Session) ReflectUtil.getFiled(analyticsClient, "session");
8175
Assert.assertNotNull(clientSession);
82-
Assert.assertEquals(1, dbUtil.getTotalNumber());
83-
Cursor cursor = dbUtil.queryAllEvents();
84-
cursor.moveToFirst();
85-
String eventString = cursor.getString(2);
86-
JSONObject jsonObject = new JSONObject(eventString);
87-
Assert.assertEquals("_session_start", jsonObject.getString("event_type"));
88-
JSONObject attributes = jsonObject.getJSONObject("attributes");
89-
Assert.assertNotNull(attributes.getString("_session_id"));
90-
Assert.assertNotNull(attributes.getString("_session_start_timestamp"));
91-
Assert.assertNotNull(attributes.getString("_session_duration"));
92-
cursor.close();
76+
Assert.assertTrue(isNewSession);
9377
}
9478

9579
/**
@@ -106,17 +90,6 @@ public void testExecuteStartAndStore() throws Exception {
10690
client.storeSession();
10791
Session storedSession = (Session) ReflectUtil.getFiled(client, "session");
10892
Assert.assertFalse(storedSession.isNewSession());
109-
110-
Assert.assertEquals(1, dbUtil.getTotalNumber());
111-
Cursor cursor = dbUtil.queryAllEvents();
112-
cursor.moveToFirst();
113-
String eventString = cursor.getString(2);
114-
JSONObject jsonObject = new JSONObject(eventString);
115-
JSONObject attributes = jsonObject.getJSONObject("attributes");
116-
Assert.assertNotNull(attributes.getString("_session_id"));
117-
Assert.assertNotNull(attributes.getString("_session_start_timestamp"));
118-
Assert.assertNotNull(attributes.getString("_session_duration"));
119-
cursor.close();
12093
}
12194

12295

@@ -143,8 +116,6 @@ public void testExecuteStartTwiceWithoutSessionTimeout() throws Exception {
143116
Assert.assertEquals(session.getSessionID(), newSession.getSessionID());
144117
Assert.assertEquals(session.getStartTime(), newSession.getStartTime());
145118
Assert.assertEquals(1, newSession.getSessionIndex());
146-
147-
Assert.assertEquals(1, dbUtil.getTotalNumber());
148119
}
149120

150121

@@ -172,8 +143,6 @@ public void testExecuteStartTwiceWithSessionTimeout() throws Exception {
172143
Assert.assertNotEquals(session.getSessionID(), newSession.getSessionID());
173144
Assert.assertNotEquals(session.getStartTime(), newSession.getStartTime());
174145
Assert.assertEquals(2, newSession.getSessionIndex());
175-
176-
Assert.assertEquals(2, dbUtil.getTotalNumber());
177146
}
178147

179148

@@ -185,12 +154,4 @@ public void testInitSessionClientWithNullAnalyticsClient() {
185154
ClickstreamContext context = mock(ClickstreamContext.class);
186155
new SessionClient(context);
187156
}
188-
189-
/**
190-
* close db.
191-
*/
192-
@After
193-
public void tearDown() {
194-
dbUtil.closeDB();
195-
}
196157
}

0 commit comments

Comments
 (0)