Skip to content

Commit

Permalink
Track open push for API 14 and above (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Alonso Fernández authored Feb 21, 2017
1 parent 909d120 commit c99561c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
38 changes: 36 additions & 2 deletions src/main/java/com/mixpanel/android/mpmetrics/GCMReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.mixpanel.android.mpmetrics.MixpanelAPI.InstanceProcessor;
import com.mixpanel.android.util.MPLog;

import org.json.JSONException;
import org.json.JSONObject;


/**
* BroadcastReceiver for handling Google Cloud Messaging intents.
Expand Down Expand Up @@ -139,8 +142,12 @@ private NotificationData(int anIcon, int aLargeIcon, int aWhiteIcon, CharSequenc
final String uriString = inboundIntent.getStringExtra("mp_cta");
CharSequence notificationTitle = inboundIntent.getStringExtra("mp_title");
final String colorName = inboundIntent.getStringExtra("mp_color");
final String campaignId = inboundIntent.getStringExtra("mp_campaign_id");
final String messageId = inboundIntent.getStringExtra("mp_message_id");
int color = NotificationData.NOT_SET;

trackCampaignReceived(campaignId, messageId);

if (colorName != null) {
try {
color = Color.parseColor(colorName);
Expand Down Expand Up @@ -195,12 +202,12 @@ private NotificationData(int anIcon, int aLargeIcon, int aWhiteIcon, CharSequenc
notificationTitle = "A message for you";
}

final Intent notificationIntent = buildNotificationIntent(context, uriString);
final Intent notificationIntent = buildNotificationIntent(context, uriString, campaignId, messageId);

return new NotificationData(notificationIcon, largeNotificationIcon, whiteNotificationIcon, notificationTitle, message, notificationIntent, color);
}

private Intent buildNotificationIntent(Context context, String uriString) {
private Intent buildNotificationIntent(Context context, String uriString, String campaignId, String messageId) {
Uri uri = null;
if (null != uriString) {
uri = Uri.parse(uriString);
Expand All @@ -213,6 +220,14 @@ private Intent buildNotificationIntent(Context context, String uriString) {
ret = new Intent(Intent.ACTION_VIEW, uri);
}

if (campaignId != null) {
ret.putExtra("mp_campaign_id", campaignId);
}

if (messageId != null) {
ret.putExtra("mp_message_id", messageId);
}

return ret;
}

Expand Down Expand Up @@ -375,6 +390,25 @@ protected Notification makeNotificationSDK21OrHigher(Context context, PendingInt
return n;
}

private void trackCampaignReceived(final String campaignId, final String messageId) {
if (campaignId != null && messageId != null) {
MixpanelAPI.allInstances(new InstanceProcessor() {
@Override
public void process(MixpanelAPI api) {
if(api.isAppInForeground()) {
JSONObject pushProps = new JSONObject();
try {
pushProps.put("campaign_id", campaignId);
pushProps.put("message_id", messageId);
pushProps.put("message_type", "push");
api.track("$campaign_received", pushProps);
} catch (JSONException e) {}
}
}
});
}
}

@SuppressWarnings("unused")
private static final String LOGTAG = "MixpanelAPI.GCMReceiver";
}
27 changes: 23 additions & 4 deletions src/main/java/com/mixpanel/android/mpmetrics/MixpanelAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1357,14 +1357,34 @@ public void logPosts() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (mContext.getApplicationContext() instanceof Application) {
final Application app = (Application) mContext.getApplicationContext();
MixpanelActivityLifecycleCallbacks mixpanelActivityLifecycleCallbacks = new MixpanelActivityLifecycleCallbacks(this, mConfig);
app.registerActivityLifecycleCallbacks(mixpanelActivityLifecycleCallbacks);
mMixpanelActivityLifecycleCallbacks = new MixpanelActivityLifecycleCallbacks(this, mConfig);
app.registerActivityLifecycleCallbacks(mMixpanelActivityLifecycleCallbacks);
} else {
MPLog.i(LOGTAG, "Context is not an Application, Mixpanel will not automatically show surveys, in-app notifications, or A/B test experiments. We won't be able to automatically flush on an app background.");
}
}
}

/**
* Based on the application's event lifecycle this method will determine whether the app
* is running in the foreground or not.
*
* If your build version is below 14 this method will always return false.
*
* @return True if the app is running in the foreground.
*/
public boolean isAppInForeground() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (mMixpanelActivityLifecycleCallbacks != null) {
return mMixpanelActivityLifecycleCallbacks.isInForeground();
}
} else {
MPLog.e(LOGTAG, "Your build version is below 14. This method will always return false.");
}

return false;
}

// Package-level access. Used (at least) by GCMReceiver
// when OS-level events occur.
/* package */ interface InstanceProcessor {
Expand Down Expand Up @@ -2256,6 +2276,7 @@ private static void checkIntentForInboundAppLink(Context context) {
private final DecideMessages mDecideMessages;
private final Map<String, String> mDeviceInfo;
private final Map<String, Long> mEventTimings;
private MixpanelActivityLifecycleCallbacks mMixpanelActivityLifecycleCallbacks;

// Maps each token to a singleton MixpanelAPI instance
private static final Map<String, Map<Context, MixpanelAPI>> sInstanceMap = new HashMap<String, Map<Context, MixpanelAPI>>();
Expand All @@ -2266,6 +2287,4 @@ private static void checkIntentForInboundAppLink(Context context) {
private static final String LOGTAG = "MixpanelAPI.API";
private static final String APP_LINKS_LOGTAG = "MixpanelAPI.AL";
private static final String ENGAGE_DATE_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss";

private boolean mDisableDecideChecker;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.mixpanel.android.viewcrawler.GestureTracker;

import org.json.JSONException;
import org.json.JSONObject;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
/* package */ class MixpanelActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
private Handler mHandler = new Handler(Looper.getMainLooper());
private Runnable check;
private boolean mIsForeground = false;
private boolean mIsForeground = true;
private boolean mPaused = true;
public static final int CHECK_DELAY = 500;

Expand All @@ -24,6 +28,22 @@ public MixpanelActivityLifecycleCallbacks(MixpanelAPI mpInstance, MPConfig confi

@Override
public void onActivityStarted(Activity activity) {
if (activity.getIntent().hasExtra("mp_campaign_id") && activity.getIntent().hasExtra("mp_message_id")) {
String campaignId = activity.getIntent().getStringExtra("mp_campaign_id");
String messageId = activity.getIntent().getStringExtra("mp_message_id");

JSONObject pushProps = new JSONObject();
try {
pushProps.put("campaign_id", campaignId);
pushProps.put("message_id", messageId);
pushProps.put("message_type", "push");
mMpInstance.track("$app_open", pushProps);
} catch (JSONException e) {}

activity.getIntent().removeExtra("mp_campaign_id");
activity.getIntent().removeExtra("mp_message_id");
}

if (android.os.Build.VERSION.SDK_INT >= MPConfig.UI_FEATURES_MIN_API && mConfig.getAutoShowMixpanelUpdates()) {
if (!activity.isTaskRoot()) {
return; // No checks, no nothing.
Expand Down Expand Up @@ -85,6 +105,10 @@ public void onActivityResumed(Activity activity) {
@Override
public void onActivityStopped(Activity activity) { }

protected boolean isInForeground() {
return mIsForeground;
}

private final MixpanelAPI mMpInstance;
private final MPConfig mConfig;
}

0 comments on commit c99561c

Please sign in to comment.