Search in sources :

Example 1 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.

the class SessionTracker method sendStartSessionIfNeeded.

/**
 * Generate a new session identifier if the first time or
 * we went in background for more X seconds or
 * if enough time has elapsed since the last background usage of the API.
 * <p>
 * Indeed the API can be used for events or crashes only for example, we need to renew
 * the session even when no pages are triggered but at the same time we want to keep using
 * the same session as long as the current activity is not paused (long video for example).
 */
private void sendStartSessionIfNeeded() {
    if (mSid == null || hasSessionTimedOut()) {
        /* New session: generate a new identifier. */
        mSid = UUIDUtils.randomUUID();
        /* Update session storage. */
        SessionContext.getInstance().addSession(mSid);
        /*
             * Record queued time for the session log itself to avoid double log if resuming
             * from background after timeout and sending a log at same time we resume like a page.
             */
        mLastQueuedLogTime = SystemClock.elapsedRealtime();
        /* Enqueue a start session log. */
        StartSessionLog startSessionLog = new StartSessionLog();
        startSessionLog.setSid(mSid);
        mChannel.enqueue(startSessionLog, mGroupName);
    }
}
Also used : StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)

Example 2 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.

the class SessionTracker method onEnqueuingLog.

@Override
public void onEnqueuingLog(@NonNull Log log, @NonNull String groupName) {
    /*
         * Since we enqueue start session logs, skip them to avoid infinite loop.
         * Also skip start service log as it's always sent and should not trigger a session.
         */
    if (log instanceof StartSessionLog || log instanceof StartServiceLog) {
        return;
    }
    /*
         * If the log has already specified a timestamp, try correlating with a past session.
         * Note that it can also find the current session but that's ok: in that case that means
         * its a log that will be associated to current session but won't trigger expiration logic.
         */
    Date timestamp = log.getTimestamp();
    if (timestamp != null) {
        SessionContext.SessionInfo pastSession = SessionContext.getInstance().getSessionAt(timestamp.getTime());
        if (pastSession != null) {
            log.setSid(pastSession.getSessionId());
        }
    } else /* If the log does not have a timestamp yet, then we just correlate with current session. */
    {
        /* Send a new start session log if needed. */
        sendStartSessionIfNeeded();
        /* Set current session identifier. */
        log.setSid(mSid);
        /* Record queued time only if the log is using current session. */
        mLastQueuedLogTime = SystemClock.elapsedRealtime();
    }
}
Also used : StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) StartServiceLog(com.microsoft.appcenter.ingestion.models.StartServiceLog) SessionContext(com.microsoft.appcenter.SessionContext) Date(java.util.Date)

Example 3 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project AppCenter-SDK-Android by Microsoft.

the class AnalyticsTest method disableAutomaticPageTracking.

@Test
public void disableAutomaticPageTracking() {
    Analytics analytics = Analytics.getInstance();
    assertTrue(Analytics.isAutoPageTrackingEnabled());
    Analytics.setAutoPageTrackingEnabled(false);
    assertFalse(Analytics.isAutoPageTrackingEnabled());
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    analytics.onActivityResumed(new MyActivity());
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof StartSessionLog;
        }
    }), anyString());
    verify(channel, never()).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof PageLog;
        }
    }), anyString());
    Analytics.setAutoPageTrackingEnabled(true);
    assertTrue(Analytics.isAutoPageTrackingEnabled());
    analytics.onActivityResumed(new SomeScreen());
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return "SomeScreen".equals(pageLog.getName());
            }
            return false;
        }
    }), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) Channel(com.microsoft.appcenter.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project AppCenter-SDK-Android by Microsoft.

the class AnalyticsTest method startSessionAfterUserApproval.

@Test
public void startSessionAfterUserApproval() {
    /*
         * Disable analytics while in background to set up the initial condition
         * simulating the opt-in use case.
         */
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.setEnabled(false);
    /* App in foreground: no log yet, we are disabled. */
    analytics.onActivityResumed(new Activity());
    verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
    /* Enable: start session sent retroactively. */
    Analytics.setEnabled(true);
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof StartSessionLog;
        }
    }), eq(analytics.getGroupName()));
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof PageLog;
        }
    }), eq(analytics.getGroupName()));
    /* Go background. */
    analytics.onActivityPaused(new Activity());
    /* Disable/enable: nothing happens on background. */
    Analytics.setEnabled(false);
    Analytics.setEnabled(true);
    /* No additional log. */
    verify(channel, times(2)).enqueue(any(Log.class), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) Channel(com.microsoft.appcenter.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project AppCenter-SDK-Android by Microsoft.

the class SessionTrackerTest method longSessionStartingFromBackground.

@Test
public void longSessionStartingFromBackground() {
    /* Application is in background, send a log, verify decoration. */
    UUID firstSid;
    long firstSessionTime = mMockTime;
    UUID expectedSid;
    StartSessionLog expectedStartSessionLog = new StartSessionLog();
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotNull(log.getSid());
        firstSid = expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Verify session reused for second log. */
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* No usage from background for a long time: new session. */
    {
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotEquals(expectedSid, log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* App comes to foreground and sends a log, still session. */
    {
        mSessionTracker.onActivityResumed();
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* We are in foreground, even after timeout a log is still in session. */
    {
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Switch to another activity and send a log, still session. */
    {
        spendTime(2);
        mSessionTracker.onActivityPaused();
        spendTime(2);
        mSessionTracker.onActivityResumed();
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* We are in foreground, even after timeout a log is still in session. */
    {
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Background for a short time and send log: still in session. */
    {
        spendTime(2);
        mSessionTracker.onActivityPaused();
        spendTime(2);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Background for a long time but correlating a log to first session: should not trigger new session. */
    {
        Log log = newEvent();
        log.setTimestamp(new Date(firstSessionTime + 20));
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(firstSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Background for a long time and coming back to foreground: new session. */
    {
        spendTime(30000);
        mSessionTracker.onActivityResumed();
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotEquals(expectedSid, log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Background for a long time sending a log: new session. */
    {
        mSessionTracker.onActivityPaused();
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotEquals(expectedSid, log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
}
Also used : StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) StartServiceLog(com.microsoft.appcenter.ingestion.models.StartServiceLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) UUID(java.util.UUID) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)22 Test (org.junit.Test)17 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)12 Log (com.microsoft.appcenter.ingestion.models.Log)12 UUID (java.util.UUID)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 StartServiceLog (com.microsoft.appcenter.ingestion.models.StartServiceLog)10 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)6 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)6 Date (java.util.Date)6 ArgumentMatcher (org.mockito.ArgumentMatcher)6 Context (android.content.Context)5 Channel (com.microsoft.appcenter.channel.Channel)5 UserIdContext (com.microsoft.appcenter.utils.context.UserIdContext)3 SessionContext (com.microsoft.appcenter.SessionContext)2 EventLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory)2 PageLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory)2 StartSessionLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory)2 Device (com.microsoft.appcenter.ingestion.models.Device)2 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)2