diff --git a/apps/sasquatch/src/projectDependency/java/com/microsoft/appcenter/sasquatch/activities/EventActivity.java b/apps/sasquatch/src/projectDependency/java/com/microsoft/appcenter/sasquatch/activities/EventActivity.java index 522a222df6..2875f3b7bb 100644 --- a/apps/sasquatch/src/projectDependency/java/com/microsoft/appcenter/sasquatch/activities/EventActivity.java +++ b/apps/sasquatch/src/projectDependency/java/com/microsoft/appcenter/sasquatch/activities/EventActivity.java @@ -224,7 +224,7 @@ public void send(@SuppressWarnings("UnusedParameters") View view) { Map properties = null; EventProperties typedProperties = null; if (mProperties.size() > 0) { - if (onlyStringProperties() && (persistenceFlag == PersistenceFlag.DEFAULT || target == null)) { + if (onlyStringProperties()) { properties = new HashMap<>(); for (TypedPropertyFragment fragment : mProperties) { fragment.set(properties); @@ -239,29 +239,36 @@ public void send(@SuppressWarnings("UnusedParameters") View view) { /* First item is always empty as it's default value which means either AppCenter, one collector or both. */ for (int i = 0; i < getNumberOfLogs(); i++) { + boolean useExplicitFlags = persistenceFlag != PersistenceFlag.DEFAULT; if (target == null) { - if (typedProperties != null) { - if (persistenceFlag != PersistenceFlag.DEFAULT) { - Analytics.trackEvent(name, typedProperties, flags); - } else { - Analytics.trackEvent(name, typedProperties); - } - } else if (properties != null) { - if (persistenceFlag != PersistenceFlag.DEFAULT) { + if (properties != null) { + if (useExplicitFlags) { Analytics.trackEvent(name, properties, flags); } else { Analytics.trackEvent(name, properties); } + } else if (typedProperties != null || useExplicitFlags) { + if (useExplicitFlags) { + Analytics.trackEvent(name, typedProperties, flags); + } else { + Analytics.trackEvent(name, typedProperties); + } } else { Analytics.trackEvent(name); } } else { - if (persistenceFlag != PersistenceFlag.DEFAULT) { - target.trackEvent(name, typedProperties, flags); - } else if (typedProperties != null) { - target.trackEvent(name, typedProperties); - } else if (properties != null) { - target.trackEvent(name, properties); + if (properties != null) { + if (useExplicitFlags) { + target.trackEvent(name, properties, flags); + } else { + target.trackEvent(name, properties); + } + } else if (typedProperties != null || useExplicitFlags) { + if (useExplicitFlags) { + target.trackEvent(name, typedProperties, flags); + } else { + target.trackEvent(name, typedProperties); + } } else { target.trackEvent(name); } diff --git a/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/Analytics.java b/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/Analytics.java index dfb4f0a10e..7e3932be03 100644 --- a/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/Analytics.java +++ b/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/Analytics.java @@ -338,8 +338,11 @@ public static void trackEvent(String name, Map properties) { * * @param name An event name. * @param properties Optional properties. - * @param flags Optional flags. Use {@link Flags#PERSISTENCE_CRITICAL} to send this event - * before events using that use default flags or {@link Flags#PERSISTENCE_NORMAL}. + * @param flags Optional flags. Events tracked with the {@link Flags#PERSISTENCE_CRITICAL} + * flag will take precedence over all other events in storage. + * An event tracked with this option will only be dropped + * if storage must make room for a newer event that is also marked with the + * {@link Flags#PERSISTENCE_CRITICAL} flag. */ public static void trackEvent(String name, Map properties, int flags) { getInstance().trackEventAsync(name, convertProperties(properties), null, flags); @@ -406,8 +409,11 @@ public static void trackEvent(String name, EventProperties properties) { * * @param name An event name. * @param properties Optional properties. - * @param flags Optional flags. Use {@link Flags#PERSISTENCE_CRITICAL} to send this event - * before events using that use default flags or {@link Flags#PERSISTENCE_NORMAL}. + * @param flags Optional flags. Events tracked with the {@link Flags#PERSISTENCE_CRITICAL} + * flag will take precedence over all other events in storage. + * An event tracked with this option will only be dropped + * if storage must make room for a newer event that is also marked with the + * {@link Flags#PERSISTENCE_CRITICAL} flag. */ public static void trackEvent(String name, EventProperties properties, int flags) { trackEvent(name, properties, null, flags); diff --git a/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/AnalyticsTransmissionTarget.java b/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/AnalyticsTransmissionTarget.java index 35764ddbf7..3c55f13c43 100644 --- a/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/AnalyticsTransmissionTarget.java +++ b/sdk/appcenter-analytics/src/main/java/com/microsoft/appcenter/analytics/AnalyticsTransmissionTarget.java @@ -154,7 +154,7 @@ private static void updateProvider(AuthenticationProvider authenticationProvider * @param name An event name. */ public void trackEvent(String name) { - trackEvent(name, null, Flags.DEFAULTS); + trackEvent(name, (EventProperties) null, Flags.DEFAULTS); } /** @@ -172,6 +172,29 @@ public void trackEvent(String name) { * @param properties Optional properties. */ public void trackEvent(String name, Map properties) { + trackEvent(name, properties, Flags.DEFAULTS); + } + + /** + * Track a custom event with name and optional string properties. + *

+ * The following rules apply: + *

    + *
  • The event name needs to match the [a-zA-Z0-9]((\.(?!(\.|$)))|[_a-zA-Z0-9]){3,99} regular expression.
  • + *
  • The property names or values cannot be null.
  • + *
  • The baseData and baseDataType properties are reserved and thus discarded.
  • + *
  • The full event size when encoded as a JSON string cannot be larger than 1.9MB.
  • + *
+ * + * @param name An event name. + * @param properties Optional properties. + * @param flags Optional flags. Events tracked with the {@link Flags#PERSISTENCE_CRITICAL} + * flag will take precedence over all other events in storage. + * An event tracked with this option will only be dropped + * if storage must make room for a newer event that is also marked with the + * {@link Flags#PERSISTENCE_CRITICAL} flag. + */ + public void trackEvent(String name, Map properties, int flags) { EventProperties eventProperties = null; if (properties != null) { eventProperties = new EventProperties(); @@ -179,7 +202,7 @@ public void trackEvent(String name, Map properties) { eventProperties.set(entry.getKey(), entry.getValue()); } } - trackEvent(name, eventProperties, Flags.DEFAULTS); + trackEvent(name, eventProperties, flags); } /** @@ -215,8 +238,11 @@ public void trackEvent(String name, EventProperties properties) { * * @param name An event name. * @param properties Optional properties. - * @param flags Optional flags. Use {@link Flags#PERSISTENCE_CRITICAL} to send this event - * before events using that use default flags or {@link Flags#PERSISTENCE_NORMAL}. + * @param flags Optional flags. Events tracked with the {@link Flags#PERSISTENCE_CRITICAL} + * flag will take precedence over all other events in storage. + * An event tracked with this option will only be dropped + * if storage must make room for a newer event that is also marked with the + * {@link Flags#PERSISTENCE_CRITICAL} flag. */ public void trackEvent(String name, EventProperties properties, int flags) { diff --git a/sdk/appcenter-analytics/src/test/java/com/microsoft/appcenter/analytics/AnalyticsTransmissionTargetTest.java b/sdk/appcenter-analytics/src/test/java/com/microsoft/appcenter/analytics/AnalyticsTransmissionTargetTest.java index 4a7245a41b..32e890aba8 100644 --- a/sdk/appcenter-analytics/src/test/java/com/microsoft/appcenter/analytics/AnalyticsTransmissionTargetTest.java +++ b/sdk/appcenter-analytics/src/test/java/com/microsoft/appcenter/analytics/AnalyticsTransmissionTargetTest.java @@ -27,6 +27,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Map; import static com.microsoft.appcenter.Flags.DEFAULTS; import static com.microsoft.appcenter.Flags.PERSISTENCE_CRITICAL; @@ -607,23 +608,26 @@ public void pauseResume() { @Test public void trackEventWithNormalPersistenceFlag() { AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("token"); - target.trackEvent("eventName", null, PERSISTENCE_NORMAL); - verify(mChannel).enqueue(isA(EventLog.class), anyString(), eq(PERSISTENCE_NORMAL)); + target.trackEvent("eventName1", (Map) null, PERSISTENCE_NORMAL); + target.trackEvent("eventName2", (EventProperties) null, PERSISTENCE_NORMAL); + verify(mChannel, times(2)).enqueue(isA(EventLog.class), anyString(), eq(PERSISTENCE_NORMAL)); } @Test public void trackEventWithNormalCriticalPersistenceFlag() { AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("token"); - target.trackEvent("eventName", null, PERSISTENCE_CRITICAL); - verify(mChannel).enqueue(isA(EventLog.class), anyString(), eq(PERSISTENCE_CRITICAL)); + target.trackEvent("eventName1", (Map) null, PERSISTENCE_CRITICAL); + target.trackEvent("eventName2", (EventProperties) null, PERSISTENCE_CRITICAL); + verify(mChannel, times(2)).enqueue(isA(EventLog.class), anyString(), eq(PERSISTENCE_CRITICAL)); } @Test public void trackEventWithInvalidFlags() { AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("token"); - target.trackEvent("eventName", null, 0x03); - verify(mChannel).enqueue(isA(EventLog.class), anyString(), eq(DEFAULTS)); - verifyStatic(); + target.trackEvent("eventName1", (Map) null, 0x03); + target.trackEvent("eventName2", (EventProperties) null, 0x03); + verify(mChannel, times(2)).enqueue(isA(EventLog.class), anyString(), eq(DEFAULTS)); + verifyStatic(times(2)); AppCenterLog.warn(eq(AppCenter.LOG_TAG), anyString()); } } diff --git a/sdk/appcenter/src/main/java/com/microsoft/appcenter/persistence/DatabasePersistence.java b/sdk/appcenter/src/main/java/com/microsoft/appcenter/persistence/DatabasePersistence.java index c37afa0759..8df93590bd 100644 --- a/sdk/appcenter/src/main/java/com/microsoft/appcenter/persistence/DatabasePersistence.java +++ b/sdk/appcenter/src/main/java/com/microsoft/appcenter/persistence/DatabasePersistence.java @@ -238,7 +238,7 @@ public long putLog(@NonNull Log log, @NonNull String group, @IntRange(from = Fla /* Convert log to JSON string and put in the database. */ try { - AppCenterLog.debug(LOG_TAG, "Storing a log to the Persistence database for log type " + log.getType() + " with sid=" + log.getSid()); + AppCenterLog.debug(LOG_TAG, "Storing a log to the Persistence database for log type " + log.getType() + " with flags=" + flags); String payload = getLogSerializer().serializeLog(log); ContentValues contentValues; boolean isLargePayload = payload.getBytes("UTF-8").length >= PAYLOAD_MAX_SIZE;