Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the second app start event's sessionId #70

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jobs:
cache: gradle
- name: Build SDK release aar file
run: |
echo ${{ github.event.pull_request.title }}
chmod +x gradlew
./gradlew assembleRelease
- name: Set up JDK 17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,11 @@ public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Life
} else if (event == Lifecycle.Event.ON_START) {
LOG.debug("Application entered the foreground.");
isFromForeground = true;
boolean isNewSession = sessionClient.initialSession();
autoRecordEventClient.handleAppStart();
autoRecordEventClient.updateStartEngageTimestamp();
boolean isNewSession = sessionClient.initialSession();
if (isNewSession) {
autoRecordEventClient.handleSessionStart();
autoRecordEventClient.setIsEntrances();
recordScreenViewAfterSessionStart();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ public void handleAppStart() {
isFirstTime = false;
}

/**
* handle session start events.
*/
public void handleSessionStart() {
final AnalyticsEvent event =
this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SESSION_START);
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
}

/**
* handle the app end event.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public SessionClient(@NonNull final ClickstreamContext clickstreamContext) {
public synchronized boolean initialSession() {
session = Session.getInstance(clickstreamContext, session);
this.clickstreamContext.getAnalyticsClient().setSession(session);
if (session.isNewSession()) {
final AnalyticsEvent event =
this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.SESSION_START);
this.clickstreamContext.getAnalyticsClient().recordEvent(event);
}
return session.isNewSession();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,15 @@ public void testSessionTimeoutAfterReopenTheApp() throws Exception {
JSONObject jsonObject2 = new JSONObject(eventString2);
String eventName2 = jsonObject2.getString("event_type");
assertEquals(Event.PresetEvent.SESSION_START, eventName2);

// assert that the second app start event will have the same session id
cursor.moveToPrevious();
String eventString3 = cursor.getString(2);
JSONObject jsonObject3 = new JSONObject(eventString3);
String eventName3 = jsonObject3.getString("event_type");
assertEquals(Event.PresetEvent.APP_START, eventName3);
assertEquals(jsonObject3.getJSONObject("attributes").getString("_session_id"),
jsonObject2.getJSONObject("attributes").getString("_session_id"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
package software.aws.solution.clickstream;

import android.content.Context;
import android.database.Cursor;
import androidx.test.core.app.ApplicationProvider;

import org.json.JSONObject;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -31,7 +28,6 @@
import software.aws.solution.clickstream.client.ClickstreamManager;
import software.aws.solution.clickstream.client.Session;
import software.aws.solution.clickstream.client.SessionClient;
import software.aws.solution.clickstream.client.db.ClickstreamDBUtil;
import software.aws.solution.clickstream.util.ReflectUtil;

import static org.mockito.Mockito.mock;
Expand All @@ -43,7 +39,6 @@
public class SessionClientTest {
private SessionClient client;
private AnalyticsClient analyticsClient;
private ClickstreamDBUtil dbUtil;
private ClickstreamContext clickstreamContext;

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

dbUtil = new ClickstreamDBUtil(context);
AWSClickstreamPluginConfiguration.Builder configurationBuilder = AWSClickstreamPluginConfiguration.builder();
configurationBuilder.withAppId("demo-app")
.withEndpoint("http://cs-se-serve-1qtj719j88vwn-1291141553.ap-southeast-1.elb.amazonaws.com/collect")
Expand All @@ -74,22 +68,12 @@ public void setup() {
*/
@Test
public void testExecuteStart() throws Exception {
client.initialSession();
boolean isNewSession = client.initialSession();
Session session = (Session) ReflectUtil.getFiled(client, "session");
Assert.assertNotNull(session);
Session clientSession = (Session) ReflectUtil.getFiled(analyticsClient, "session");
Assert.assertNotNull(clientSession);
Assert.assertEquals(1, dbUtil.getTotalNumber());
Cursor cursor = dbUtil.queryAllEvents();
cursor.moveToFirst();
String eventString = cursor.getString(2);
JSONObject jsonObject = new JSONObject(eventString);
Assert.assertEquals("_session_start", jsonObject.getString("event_type"));
JSONObject attributes = jsonObject.getJSONObject("attributes");
Assert.assertNotNull(attributes.getString("_session_id"));
Assert.assertNotNull(attributes.getString("_session_start_timestamp"));
Assert.assertNotNull(attributes.getString("_session_duration"));
cursor.close();
Assert.assertTrue(isNewSession);
}

/**
Expand All @@ -106,17 +90,6 @@ public void testExecuteStartAndStore() throws Exception {
client.storeSession();
Session storedSession = (Session) ReflectUtil.getFiled(client, "session");
Assert.assertFalse(storedSession.isNewSession());

Assert.assertEquals(1, dbUtil.getTotalNumber());
Cursor cursor = dbUtil.queryAllEvents();
cursor.moveToFirst();
String eventString = cursor.getString(2);
JSONObject jsonObject = new JSONObject(eventString);
JSONObject attributes = jsonObject.getJSONObject("attributes");
Assert.assertNotNull(attributes.getString("_session_id"));
Assert.assertNotNull(attributes.getString("_session_start_timestamp"));
Assert.assertNotNull(attributes.getString("_session_duration"));
cursor.close();
}


Expand All @@ -143,8 +116,6 @@ public void testExecuteStartTwiceWithoutSessionTimeout() throws Exception {
Assert.assertEquals(session.getSessionID(), newSession.getSessionID());
Assert.assertEquals(session.getStartTime(), newSession.getStartTime());
Assert.assertEquals(1, newSession.getSessionIndex());

Assert.assertEquals(1, dbUtil.getTotalNumber());
}


Expand Down Expand Up @@ -172,8 +143,6 @@ public void testExecuteStartTwiceWithSessionTimeout() throws Exception {
Assert.assertNotEquals(session.getSessionID(), newSession.getSessionID());
Assert.assertNotEquals(session.getStartTime(), newSession.getStartTime());
Assert.assertEquals(2, newSession.getSessionIndex());

Assert.assertEquals(2, dbUtil.getTotalNumber());
}


Expand All @@ -185,12 +154,4 @@ public void testInitSessionClientWithNullAnalyticsClient() {
ClickstreamContext context = mock(ClickstreamContext.class);
new SessionClient(context);
}

/**
* close db.
*/
@After
public void tearDown() {
dbUtil.closeDB();
}
}
Loading