co.easimart.EasimartAnalytics Maven / Gradle / Ivy
package co.easimart;
import android.content.Intent;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.LinkedHashMap;
import java.util.Map;
import bolts.Capture;
import bolts.Continuation;
import bolts.Task;
/**
* The {@code EasimartAnalytics} class provides an interface to Easimart's logging and analytics backend.
* Methods will return immediately and cache requests (+ timestamps) to be handled "eventually."
* That is, the request will be sent immediately if possible or the next time a network connection
* is available otherwise.
*/
public class EasimartAnalytics {
private static final String TAG = "co.easimart.EasimartAnalytics";
/* package for test */ static EasimartAnalyticsController getAnalyticsController() {
return EasimartCorePlugins.getInstance().getAnalyticsController();
}
/**
* Tracks this application being launched (and if this happened as the result of the user opening
* a push notification, this method sends along information to correlate this open with that
* push).
*
* @param intent
* The {@code Intent} that started an {@code Activity}, if any. Can be null.
* @return A Task that is resolved when the event has been tracked by Easimart.
*/
public static Task trackAppOpenedInBackground(Intent intent) {
String pushHashStr = getPushHashFromIntent(intent);
final Capture pushHash = new Capture<>();
if (pushHashStr != null && pushHashStr.length() > 0) {
synchronized (lruSeenPushes) {
if (lruSeenPushes.containsKey(pushHashStr)) {
return Task.forResult(null);
} else {
lruSeenPushes.put(pushHashStr, true);
pushHash.set(pushHashStr);
}
}
}
return EasimartUser.getCurrentSessionTokenAsync().onSuccessTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
String sessionToken = task.getResult();
return getAnalyticsController().trackAppOpenedInBackground(pushHash.get(), sessionToken);
}
});
}
/**
* @deprecated Please use {@link #trackAppOpenedInBackground(android.content.Intent)} instead.
*/
@Deprecated
public static void trackAppOpened(Intent intent) {
trackAppOpenedInBackground(intent);
}
/**
* Tracks this application being launched (and if this happened as the result of the user opening
* a push notification, this method sends along information to correlate this open with that
* push).
*
* @param intent
* The {@code Intent} that started an {@code Activity}, if any. Can be null.
* @param callback
* callback.done(e) is called when the event has been tracked by Easimart.
*/
public static void trackAppOpenedInBackground(Intent intent, SaveCallback callback) {
EasimartTaskUtils.callbackOnMainThreadAsync(trackAppOpenedInBackground(intent), callback);
}
/**
* @deprecated Please use {@link #trackEventInBackground(String)} instead.
*/
@Deprecated
public static void trackEvent(String name) {
trackEventInBackground(name);
}
/**
* Tracks the occurrence of a custom event. Easimart will store a data point at the time of
* invocation with the given event name.
*
* @param name
* The name of the custom event to report to Easimart as having happened.
* @param callback
* callback.done(e) is called when the event has been tracked by Easimart.
*/
public static void trackEventInBackground(String name, SaveCallback callback) {
EasimartTaskUtils.callbackOnMainThreadAsync(trackEventInBackground(name), callback);
}
/**
* @deprecated Please use {@link #trackEventInBackground(String, java.util.Map)} instead.
*/
@Deprecated
public static void trackEvent(String name, Map dimensions) {
trackEventInBackground(name, dimensions);
}
/**
* Tracks the occurrence of a custom event with additional dimensions. Easimart will store a data
* point at the time of invocation with the given event name. Dimensions will allow segmentation
* of the occurrences of this custom event.
*
* To track a user signup along with additional metadata, consider the following:
*
* Map dimensions = new HashMap();
* dimensions.put("gender", "m");
* dimensions.put("source", "web");
* dimensions.put("dayType", "weekend");
* EasimartAnalytics.trackEvent("signup", dimensions);
*
* There is a default limit of 8 dimensions per event tracked.
*
* @param name
* The name of the custom event to report to Easimart as having happened.
* @param dimensions
* The dictionary of information by which to segment this event.
* @param callback
* callback.done(e) is called when the event has been tracked by Easimart.
*/
public static void trackEventInBackground(String name, Map dimensions, SaveCallback callback) {
EasimartTaskUtils.callbackOnMainThreadAsync(trackEventInBackground(name, dimensions), callback);
}
/**
* Tracks the occurrence of a custom event with additional dimensions. Easimart will store a data
* point at the time of invocation with the given event name. Dimensions will allow segmentation
* of the occurrences of this custom event.
*
* To track a user signup along with additional metadata, consider the following:
*
* Map dimensions = new HashMap();
* dimensions.put("gender", "m");
* dimensions.put("source", "web");
* dimensions.put("dayType", "weekend");
* EasimartAnalytics.trackEvent("signup", dimensions);
*
* There is a default limit of 8 dimensions per event tracked.
*
* @param name
* The name of the custom event to report to Easimart as having happened.
*
* @return A Task that is resolved when the event has been tracked by Easimart.
*/
public static Task trackEventInBackground(String name) {
return trackEventInBackground(name, (Map) null);
}
/**
* Tracks the occurrence of a custom event with additional dimensions. Easimart will store a data
* point at the time of invocation with the given event name. Dimensions will allow segmentation
* of the occurrences of this custom event.
*
* To track a user signup along with additional metadata, consider the following:
*
* Map dimensions = new HashMap();
* dimensions.put("gender", "m");
* dimensions.put("source", "web");
* dimensions.put("dayType", "weekend");
* EasimartAnalytics.trackEvent("signup", dimensions);
*
* There is a default limit of 8 dimensions per event tracked.
*
* @param name
* The name of the custom event to report to Easimart as having happened.
* @param dimensions
* The dictionary of information by which to segment this event.
*
* @return A Task that is resolved when the event has been tracked by Easimart.
*/
public static Task trackEventInBackground(final String name,
Map dimensions) {
if (name == null || name.trim().length() == 0) {
throw new IllegalArgumentException("A name for the custom event must be provided.");
}
final JSONObject jsonDimensions = dimensions != null
? (JSONObject) NoObjectsEncoder.get().encode(dimensions)
: null;
return EasimartUser.getCurrentSessionTokenAsync().onSuccessTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
String sessionToken = task.getResult();
return getAnalyticsController().trackEventInBackground(name, jsonDimensions, sessionToken);
}
});
}
// Developers have the option to manually track push opens or the app open event can be tracked
// automatically by the EasimartPushBroadcastReceiver. To avoid double-counting a push open, we track
// the pushes we've seen locally. We don't need to worry about doing this in any sort of durable
// way because a push can only launch the app once.
private static final Map lruSeenPushes = new LinkedHashMap() {
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > 10;
}
};
/* package */ static void clear() {
synchronized (lruSeenPushes) {
lruSeenPushes.clear();
}
}
/* package for test */ static String getPushHashFromIntent(Intent intent) {
String pushData = null;
if (intent != null && intent.getExtras() != null) {
pushData = intent.getExtras().getString(EasimartPushBroadcastReceiver.KEY_PUSH_DATA);
}
if (pushData == null) {
return null;
}
String pushHash = null;
try {
JSONObject payload = new JSONObject(pushData);
pushHash = payload.optString("push_hash");
} catch (JSONException e) {
EasimartLog.e(TAG, "Failed to parse push data: " + e.getMessage());
}
return pushHash;
}
}