Skip to content

Instantly share code, notes, and snippets.

@amsterdatech
Forked from Actine/Analytics.java
Created December 18, 2020 20:51
Show Gist options
  • Save amsterdatech/d615da425a95f5f6a37e06ad717f6216 to your computer and use it in GitHub Desktop.
Save amsterdatech/d615da425a95f5f6a37e06ad717f6216 to your computer and use it in GitHub Desktop.
package com.actinarium.materialcue.analytics;
import android.app.Application;
import android.support.annotation.NonNull;
import android.util.Log;
import com.actinarium.materialcue.dto.Overlay;
import com.actinarium.materialcue.iab.PremiumStatus;
import com.actinarium.materialcue.iab.PremiumStatusChangeListener;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
/**
* <p></p>
*
* @author Paul Danyliuk
*/
public class Analytics implements PremiumStatusChangeListener {
private static final String TAG = "MaterialCueAnalytics";
public static final String SCREEN_IN_BACKGROUND = "In background";
public static final String SCREEN_MAIN = "Main screen";
public static final String SCREEN_OVERLAY_EDITOR = "Overlay editor";
public static final String SCREEN_SETTINGS = "Settings";
public static final String SCREEN_HELP_AND_ABOUT = "Help & about";
public static final String SCREEN_SUPPORT_DEVELOPER = "Support developer";
public static final String SCREEN_SUPPORT_DEVELOPER_RETURNING = "Support developer (returning)";
public static final String CATEGORY_OVERLAY_NOTIFICATION_OPS = "Notification operations";
public static final String CATEGORY_OVERLAY_CONFIG = "In-app overlay management";
public static final String CATEGORY_EDITOR_EVENTS = "Overlay editor events";
public static final String CATEGORY_SETTINGS = "Settings";
public static final String CATEGORY_HELP = "Seeking help";
public static final String CATEGORY_ENGAGEMENT = "Engagement";
public static final String CATEGORY_HEARTBEAT = "Heartbeat";
public static final String EVENT_OVERLAY_SELECT = "Overlay selected";
public static final String EVENT_OVERLAY_HIDE = "Overlay hidden";
public static final String EVENT_OVERLAY_SHOW = "Overlay shown";
public static final String EVENT_OVERLAY_BACK = "Back to loop pressed";
public static final String EVENT_OVERLAY_ACTIVATE = "Overlay activated";
public static final String EVENT_OVERLAY_DEACTIVATE = "Overlay deactivated";
public static final String EVENT_OVERLAY_DELETE = "Overlay deleted";
public static final String EVENT_OVERLAY_RESTORE = "Overlay restored";
public static final String EVENT_EDITOR_HELP = "Editor help called";
public static final String EVENT_DOCS_LINK_CLICKED = "Docs link clicked";
public static final String EVENT_COMMUNITY_LINK_CLICKED = "Navigated to community";
public static final String EVENT_PLAY_STORE = "Navigated to Play Store";
public static final String EVENT_PRIVACY_POLICY = "Opened Privacy Policy";
public static final String EVENT_OS_LICENSES = "Opened open source licenses";
public static final String EVENT_EDITOR_PREVIEW_SUCCESS = "Successful preview served";
public static final String EVENT_EDITOR_ERRORS_REPORTED = "Validation errors reported";
public static final String EVENT_EDITOR_HIDE_PREVIEW = "Preview hidden from bottom pane";
public static final String EVENT_EDITOR_IGNORE_WARNINGS = "Ignore warnings pressed";
public static final String EVENT_EDITOR_ERROR_DETAILS_VIEWED = "Validation details viewed";
public static final String EVENT_SAVED_NEW = "Saved new overlay";
public static final String EVENT_SAVED_CLONE = "Saved cloned overlay";
public static final String EVENT_SAVED_EDITED = "Saved overlay edit";
public static final String EVENT_PURCHASE_STARTED = "Purchase flow started";
public static final String EVENT_PURCHASE_ABORTED = "Purchase flow aborted";
public static final String EVENT_PURCHASE_COMPLETED = "Purchase completed";
public static final String EVENT_SHARE_CLICKED = "Share clicked";
public static final String EVENT_RATE_REVIEW_CLICKED = "Rate & Review link clicked";
public static final String EVENT_EMAIL_CLICKED = "Email link clicked";
public static final String EVENT_CONTRIBUTE_MORE_CLICKED = "Contribute more clicked";
public static final String EVENT_FULLSCREEN_GRID_ON = "Fullscreen grid turned on";
public static final String EVENT_FULLSCREEN_GRID_OFF = "Fullscreen grid turned off";
public static final String EVENT_OVER_NOTIFICATION_ON = "Grid brought forward over notifications";
public static final String EVENT_OVER_NOTIFICATION_OFF = "Grid brought back behind notifications";
public static final String EVENT_TABLET_KEYLINES_ON = "Switched to tablet keylines";
public static final String EVENT_TABLET_KEYLINES_OFF = "Switched to phone keylines";
public static final String EVENT_GRID_COLOR_CHANGED = "Grid color changed";
public static final String EVENT_KEYLINE_COLOR_CHANGED = "Keyline color changed";
public static final String EVENT_RATIO_KEYLINE_COLOR_CHANGED = "Ratio keyline color changed";
public static final String EVENT_FILL_COLOR_CHANGED = "Fill color changed";
private static final String EVENT_PREMIUM_CHECK_FAILURE = "Premium check failure";
private static final String EVENT_END_OF_EARLY_REPORTED = "Reported end of early support";
public static final String EVENT_UNCAUGHT_INFLATION_ERROR = "Uncaught Rhythm inflation error";
public static final String EVENT_COULD_NOT_LOAD_SUPPORT_OPTIONS = "Couldn't load support options";
public static final String LABEL_NO_OVERLAY = "No overlay";
public static final String LABEL_STANDARD = "Standard";
public static final String LABEL_TYPOGRAPHY = "Typography";
public static final String LABEL_AVATAR = "Avatar";
public static final String LABEL_CARD = "Cards";
public static final String LABEL_RATIO = "Ratio";
public static final String LABEL_COLUMNS = "Columns";
public static final String LABEL_HYBRID = "Hybrid";
public static final String LABEL_SPLIT = "Split";
public static final String LABEL_CUSTOM = "Custom";
public static final String LABEL_EVENT_FROM_SUPPORT_PAGE = "From first support page";
public static final String LABEL_EVENT_FROM_THANK_YOU_PAGE = "From thank you page";
public static final String LABEL_ERRORS_PREVIEWING = "When previewing";
public static final String LABEL_ERRORS_SAVING = "When saving";
public static final String[] BASIC_OVERLAYS_LABELS = {
LABEL_STANDARD, LABEL_TYPOGRAPHY, LABEL_AVATAR, LABEL_CARD,
LABEL_RATIO, LABEL_COLUMNS, LABEL_HYBRID, LABEL_SPLIT
};
public static final String[] LICENSE_TYPES = {"Unknown", "Free", "Indie", "Pro"};
private Tracker mTracker;
private String mLastScreenName;
private String mLastOverlay;
private boolean mIsEndOfEarlySupportReported;
public static Analytics from(Application application) {
return ((Provider) application).getAnalytics();
}
public Analytics(Tracker tracker) {
mTracker = tracker;
Log.d(TAG, "Initialized analytics facade.");
}
public Tracker getTracker() {
return mTracker;
}
/**
* Start a new session and send a screen hit. Use this when the user upgrades their support tier
*/
public Analytics startNewSession() {
mTracker.send(new HitBuilders.ScreenViewBuilder().setNewSession().build());
Log.d(TAG, "Started new session and sent screen hit: " + mLastScreenName);
return this;
}
/**
* Tell Analytics what screen we're on. This method prevents from sending the same screen hit
*
* @param screenName screen name, pick one from constants in this class
*/
public Analytics sendScreenEvent(@NonNull String screenName) {
mTracker.setScreenName(screenName);
Log.d(TAG, "Gone foreground at " + screenName);
if (!screenName.equals(mLastScreenName)) {
mLastScreenName = screenName;
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
Log.d(TAG, "Sent screen hit: " + screenName);
}
return this;
}
/**
* Notify Analytics that the user left the app (e.g. put it in background), so that when events are recorded outside
* the app (e.g. changing overlays from the notification via the service), it gets reported as "In background".
* Doesn't send a screen hit, doesn't rewrite {@link #mLastScreenName} variable
*/
public Analytics notifyGoneBackground() {
mTracker.setScreenName(SCREEN_IN_BACKGROUND);
Log.d(TAG, "Gone background");
return this;
}
public Analytics sendEvent(@NonNull String category, @NonNull String action) {
mTracker.send(new HitBuilders.EventBuilder(category, action).build());
return this;
}
public Analytics sendEvent(@NonNull String category, @NonNull String action, String label) {
mTracker.send(new HitBuilders.EventBuilder(category, action).setLabel(label).build());
return this;
}
public Analytics sendEvent(@NonNull String category, @NonNull String action, String label, long value) {
mTracker.send(new HitBuilders.EventBuilder(category, action).setLabel(label).setValue(value).build());
return this;
}
public Analytics sendOverlaySelectedOnMain(@NonNull String overlayLabel) {
if (!overlayLabel.equals(mLastOverlay)) {
mLastOverlay = overlayLabel;
sendEvent(CATEGORY_OVERLAY_CONFIG, EVENT_OVERLAY_SELECT, overlayLabel);
}
return this;
}
public Analytics sendOverlaySelectedInNotification(@NonNull String overlayLabel) {
if (!overlayLabel.equals(mLastOverlay)) {
mLastOverlay = overlayLabel;
sendEvent(CATEGORY_OVERLAY_NOTIFICATION_OPS, EVENT_OVERLAY_SELECT, overlayLabel);
}
return this;
}
public Analytics sendEndOfEarlySupportReported() {
if (!mIsEndOfEarlySupportReported) {
mIsEndOfEarlySupportReported = true;
sendEvent(CATEGORY_HEARTBEAT, EVENT_END_OF_EARLY_REPORTED);
}
return this;
}
@Override
public void onPremiumStatusChanged(PremiumStatus status) {
if (!status.isSuccess) {
sendEvent(CATEGORY_HEARTBEAT, EVENT_PREMIUM_CHECK_FAILURE, "Assuming " + status);
}
Log.d(TAG, "Premium status set: " + status);
mTracker.set("&cd1", LICENSE_TYPES[status.licenseType + 1]);
mTracker.set("&cd2", status.purchaseCode);
}
public static String determineOverlayType(@NonNull Overlay overlay) {
if (overlay.isCustom()) {
return Analytics.LABEL_CUSTOM;
} else {
return Analytics.BASIC_OVERLAYS_LABELS[(int) (overlay.id - 1)];
}
}
public interface Provider {
Analytics getAnalytics();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment