Skip to content

Commit

Permalink
Merge pull request #872 from Microsoft/feature/priority-track-event-f…
Browse files Browse the repository at this point in the history
…lags-api

 Add flags to AnalyticsTransmissionTarget.trackEvent(String, Map)
  • Loading branch information
guperrot committed Nov 8, 2018
2 parents ef78840 + 190b8a3 commit d13f031
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void send(@SuppressWarnings("UnusedParameters") View view) {
Map<String, String> 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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,11 @@ public static void trackEvent(String name, Map<String, String> 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<String, String> properties, int flags) {
getInstance().trackEventAsync(name, convertProperties(properties), null, flags);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -172,14 +172,37 @@ public void trackEvent(String name) {
* @param properties Optional properties.
*/
public void trackEvent(String name, Map<String, String> properties) {
trackEvent(name, properties, Flags.DEFAULTS);
}

/**
* Track a custom event with name and optional string properties.
* <p>
* The following rules apply:
* <ul>
* <li>The event name needs to match the <tt>[a-zA-Z0-9]((\.(?!(\.|$)))|[_a-zA-Z0-9]){3,99}</tt> regular expression.</li>
* <li>The property names or values cannot be null.</li>
* <li>The <tt>baseData</tt> and <tt>baseDataType</tt> properties are reserved and thus discarded.</li>
* <li>The full event size when encoded as a JSON string cannot be larger than 1.9MB.</li>
* </ul>
*
* @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<String, String> properties, int flags) {
EventProperties eventProperties = null;
if (properties != null) {
eventProperties = new EventProperties();
for (Map.Entry<String, String> entry : properties.entrySet()) {
eventProperties.set(entry.getKey(), entry.getValue());
}
}
trackEvent(name, eventProperties, Flags.DEFAULTS);
trackEvent(name, eventProperties, flags);
}

/**
Expand Down Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String>) 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<String, String>) 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<String, String>) 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d13f031

Please sign in to comment.