use of com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.
the class AnalyticsSerializerTest method someBatch.
@Test
public void someBatch() throws JSONException {
LogContainer expectedContainer = new LogContainer();
Device device = new Device();
device.setSdkName("mobilecenter.android");
device.setSdkVersion("1.2.3");
device.setModel("S5");
device.setOemName("HTC");
device.setOsName("Android");
device.setOsVersion("4.0.3");
device.setOsBuild("LMY47X");
device.setOsApiLevel(15);
device.setLocale("en_US");
device.setTimeZoneOffset(120);
device.setScreenSize("800x600");
device.setAppVersion("3.2.1");
device.setAppBuild("42");
List<Log> logs = new ArrayList<>();
{
logs.add(new StartSessionLog());
}
expectedContainer.setLogs(logs);
{
PageLog pageLog = new PageLog();
pageLog.setName("home");
logs.add(pageLog);
}
{
PageLog pageLog = new PageLog();
pageLog.setName("settings");
pageLog.setProperties(new HashMap<String, String>() {
{
put("from", "home_menu");
put("orientation", "portrait");
}
});
logs.add(pageLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("subscribe");
logs.add(eventLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("click");
eventLog.setProperties(new HashMap<String, String>() {
{
put("x", "1");
put("y", "2");
}
});
logs.add(eventLog);
}
UUID sid = UUIDUtils.randomUUID();
for (Log log : logs) {
log.setSid(sid);
log.setDevice(device);
}
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(StartSessionLog.TYPE, new StartSessionLogFactory());
serializer.addLogFactory(PageLog.TYPE, new PageLogFactory());
serializer.addLogFactory(EventLog.TYPE, new EventLogFactory());
String payload = serializer.serializeContainer(expectedContainer);
android.util.Log.v(TAG, payload);
LogContainer actualContainer = serializer.deserializeContainer(payload);
Assert.assertEquals(expectedContainer, actualContainer);
}
use of com.microsoft.azure.mobile.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.
*
* 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 map. */
mSessions.put(System.currentTimeMillis(), mSid);
/* Remove oldest session if we reached maximum storage capacity. */
if (mSessions.size() > STORAGE_MAX_SESSIONS)
mSessions.pollFirstEntry();
/* Persist sessions. */
Set<String> sessionStorage = new HashSet<>();
for (Map.Entry<Long, UUID> session : mSessions.entrySet()) sessionStorage.add(session.getKey() + STORAGE_KEY_VALUE_SEPARATOR + session.getValue());
StorageHelper.PreferencesStorage.putStringSet(STORAGE_KEY, sessionStorage);
/* Enqueue a start session log. */
StartSessionLog startSessionLog = new StartSessionLog();
startSessionLog.setSid(mSid);
mChannel.enqueue(startSessionLog, mGroupName);
}
}
use of com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog in project mobile-center-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 optin use case.
*/
Analytics analytics = Analytics.getInstance();
Channel channel = mock(Channel.class);
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()));
}
use of com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.
the class SessionTrackerTest method startSessionWithoutLogs.
@Test
public void startSessionWithoutLogs() {
final AtomicReference<StartSessionLog> startSessionLog = new AtomicReference<>();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
startSessionLog.set((StartSessionLog) invocation.getArguments()[0]);
return null;
}
}).when(mChannel).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
/* Go foreground, start session is sent. */
mSessionTracker.onActivityResumed();
verify(mChannel, times(1)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
assertNotNull(startSessionLog.get());
UUID sid = startSessionLog.get().getSid();
assertNotNull(sid);
/* Change screen after a long time, session reused. */
spendTime(30000);
mSessionTracker.onActivityPaused();
spendTime(1);
mSessionTracker.onActivityResumed();
verify(mChannel, times(1)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
/* Go background and come back after timeout, second session. */
spendTime(1);
mSessionTracker.onActivityPaused();
spendTime(30000);
mSessionTracker.onActivityResumed();
verify(mChannel, times(2)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
assertNotEquals(sid, startSessionLog.get().getSid());
}
Aggregations