Search in sources :

Example 1 with EventLog

use of com.microsoft.appcenter.analytics.ingestion.models.EventLog 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("appcenter.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<>();
    {
        StartSessionLog startSessionLog = new StartSessionLog();
        startSessionLog.setTimestamp(new Date());
        logs.add(startSessionLog);
    }
    expectedContainer.setLogs(logs);
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("home");
        logs.add(pageLog);
    }
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("settings");
        pageLog.setProperties(new HashMap<String, String>() {

            {
                put("from", "home_menu");
                put("orientation", "portrait");
            }
        });
        logs.add(pageLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setTimestamp(new Date());
        eventLog.setId(UUIDUtils.randomUUID());
        eventLog.setName("subscribe");
        logs.add(eventLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setTimestamp(new Date());
        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);
}
Also used : PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) HashMap(java.util.HashMap) Device(com.microsoft.appcenter.ingestion.models.Device) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) ArrayList(java.util.ArrayList) PageLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Date(java.util.Date) StartSessionLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) EventLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 2 with EventLog

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

the class AnalyticsTest method testTrackEvent.

@Test
public void testTrackEvent() {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.trackEvent(null, null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent("", null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent(" ", null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    final String maxName = generateString(Analytics.MAX_NAME_LENGTH, '*');
    Analytics.trackEvent(maxName + "*", null);
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                return eventLog.getName().equals(maxName) && eventLog.getProperties() == null;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    Analytics.trackEvent(maxName, null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent("eventName", new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH + 1, '*'), null);
            put("1", null);
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                return eventLog.getProperties().size() == 0;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    final String validMapItem = "valid";
    Analytics.trackEvent("eventName", new HashMap<String, String>() {

        {
            for (int i = 0; i < 10; i++) {
                put(validMapItem + i, validMapItem);
            }
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                return eventLog.getProperties().size() == 5;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    final String longerMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    Analytics.trackEvent("eventName", new HashMap<String, String>() {

        {
            put(longerMapItem, longerMapItem);
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                if (eventLog.getProperties().size() == 1) {
                    Map.Entry<String, String> entry = eventLog.getProperties().entrySet().iterator().next();
                    String truncatedMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH, '*');
                    return entry.getKey().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH;
                }
            }
            return false;
        }
    }), anyString());
}
Also used : Context(android.content.Context) 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) Channel(com.microsoft.appcenter.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) Map(java.util.Map) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with EventLog

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

the class SessionTrackerTest method newEvent.

@NonNull
private static EventLog newEvent() {
    EventLog eventLog = new EventLog();
    eventLog.setId(UUID.randomUUID());
    eventLog.setName("test");
    return eventLog;
}
Also used : EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) NonNull(android.support.annotation.NonNull)

Example 4 with EventLog

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

the class EventFilter method applyEnabledState.

@Override
protected synchronized void applyEnabledState(boolean enabled) {
    /* Enable filtering logs when this module is enabled. */
    if (enabled) {
        mChannelListener = new AbstractChannelListener() {

            @Override
            public boolean shouldFilter(@NonNull Log log) {
                /* Filter out events. */
                if (log instanceof EventLog) {
                    AppCenterLog.info(LOG_TAG, "Filtered an event out.");
                    return true;
                }
                return false;
            }
        };
        mChannel.addListener(mChannelListener);
    } else /* On applying disabled state, let's make sure we remove listener. */
    if (mChannel != null) {
        mChannel.removeListener(mChannelListener);
    }
}
Also used : AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) AbstractChannelListener(com.microsoft.appcenter.channel.AbstractChannelListener)

Example 5 with EventLog

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

the class SasquatchAnalyticsListener method onSendingSucceeded.

@Override
public void onSendingSucceeded(com.microsoft.appcenter.ingestion.models.Log log) {
    String message = null;
    if (log instanceof EventLog) {
        message = String.format("%s\nName: %s", mContext.getString(R.string.event_sent_succeeded), ((EventLog) log).getName());
    } else if (log instanceof PageLog) {
        message = String.format("%s\nName: %s", mContext.getString(R.string.page_sent_succeeded), ((PageLog) log).getName());
    }
    if (message != null) {
        if (((LogWithProperties) log).getProperties() != null) {
            message += String.format("\nProperties: %s", new JSONObject(((LogWithProperties) log).getProperties()).toString());
        }
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
    }
    analyticsIdlingResource.decrement();
}
Also used : JSONObject(org.json.JSONObject) LogWithProperties(com.microsoft.appcenter.ingestion.models.LogWithProperties) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog)

Aggregations

EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)6 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)3 Log (com.microsoft.appcenter.ingestion.models.Log)3 Test (org.junit.Test)3 StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)2 Channel (com.microsoft.appcenter.channel.Channel)2 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)2 HashMap (java.util.HashMap)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Context (android.content.Context)1 NonNull (android.support.annotation.NonNull)1 AnalyticsListener (com.microsoft.appcenter.analytics.channel.AnalyticsListener)1 EventLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory)1 PageLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory)1 StartSessionLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory)1 AbstractChannelListener (com.microsoft.appcenter.channel.AbstractChannelListener)1 Device (com.microsoft.appcenter.ingestion.models.Device)1 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)1 LogWithProperties (com.microsoft.appcenter.ingestion.models.LogWithProperties)1 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)1