Skip to content

Commit

Permalink
Merge pull request #116 from mixpanel/applinks
Browse files Browse the repository at this point in the history
Adding App Links support via Bolts
  • Loading branch information
jbwyme committed Aug 21, 2014
2 parents 0ee726f + 2826f76 commit 495ade3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
classpath 'com.android.tools.build:gradle:0.12.2'
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group = com.mixpanel.android
version = 4.2.2
version = 4.3.0-SNAPSHOT
2 changes: 1 addition & 1 deletion src/main/java/com/mixpanel/android/mpmetrics/MPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Stores global configuration options for the Mixpanel library.
*/
public class MPConfig {
public static final String VERSION = "4.2.2";
public static final String VERSION = "4.3.0-SNAPSHOT";

public static boolean DEBUG = false;

Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/mixpanel/android/mpmetrics/MixpanelAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import android.app.Application;
import android.app.FragmentTransaction;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import com.mixpanel.android.R;
Expand All @@ -23,6 +26,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -182,8 +187,12 @@ public static MixpanelAPI getInstance(Context context, String token) {
MixpanelAPI instance = instances.get(appContext);
if (null == instance) {
instance = new MixpanelAPI(appContext, sReferrerPrefs, token);
registerAppLinksListeners(context, instance);
instances.put(appContext, instance);
}

checkIntentForInboundAppLink(context);

return instance;
}
}
Expand Down Expand Up @@ -1546,7 +1555,67 @@ private void sendAllPeopleRecords(JSONArray records) {
}
}

private static void registerAppLinksListeners(Context context, final MixpanelAPI mixpanel) {
// Register a BroadcastReceiver to receive com.parse.bolts.measurement_event and track a call to mixpanel
try {
Class<?> clazz = Class.forName("android.support.v4.content.LocalBroadcastManager");
Method methodGetInstance = clazz.getMethod("getInstance", Context.class);
Method methodRegisterReceiver = clazz.getMethod("registerReceiver", BroadcastReceiver.class, IntentFilter.class);
Object localBroadcastManager = methodGetInstance.invoke(null, context);
methodRegisterReceiver.invoke(localBroadcastManager, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
JSONObject properties = new JSONObject();
Bundle args = intent.getBundleExtra("event_args");
if (args != null) {
for (String key : args.keySet()) {
try {
properties.put(key, args.get(key));
} catch (JSONException e) {
Log.e(APP_LINKS_LOGTAG, "failed to add key \"" + key + "\" to properties for tracking bolts event", e);
}
}
}
mixpanel.track("$" + intent.getStringExtra("event_name"), properties);
}
}, new IntentFilter("com.parse.bolts.measurement_event"));
} catch (InvocationTargetException e) {
Log.d(APP_LINKS_LOGTAG, "Failed to invoke LocalBroadcastManager.registerReceiver() -- App Links tracking will not be enabled due to this exception", e);
} catch (ClassNotFoundException e) {
Log.d(APP_LINKS_LOGTAG, "To enable App Links tracking android.support.v4 must be installed: " + e.getMessage());
} catch (NoSuchMethodException e) {
Log.d(APP_LINKS_LOGTAG, "To enable App Links tracking android.support.v4 must be installed: " + e.getMessage());
} catch (IllegalAccessException e) {
Log.d(APP_LINKS_LOGTAG, "App Links tracking will not be enabled due to this exception: " + e.getMessage());
}
}

private static void checkIntentForInboundAppLink(Context context) {
// call the Bolts getTargetUrlFromInboundIntent method simply for a side effect
// if the intent is the result of an App Link, it'll trigger al_nav_in
// https://github.com/BoltsFramework/Bolts-Android/blob/1.1.2/Bolts/src/bolts/AppLinks.java#L86
if (context instanceof Activity) {
try {
Class<?> clazz = Class.forName("bolts.AppLinks");
Intent intent = ((Activity) context).getIntent();
Method getTargetUrlFromInboundIntent = clazz.getMethod("getTargetUrlFromInboundIntent", Context.class, Intent.class);
getTargetUrlFromInboundIntent.invoke(null, context, intent);
} catch (InvocationTargetException e) {
Log.d(APP_LINKS_LOGTAG, "Failed to invoke bolts.AppLinks.getTargetUrlFromInboundIntent() -- Unable to detect inbound App Links", e);
} catch (ClassNotFoundException e) {
Log.d(APP_LINKS_LOGTAG, "Please install the Bolts library >= 1.1.2 to track App Links: " + e.getMessage());
} catch (NoSuchMethodException e) {
Log.d(APP_LINKS_LOGTAG, "Please install the Bolts library >= 1.1.2 to track App Links: " + e.getMessage());
} catch (IllegalAccessException e) {
Log.d(APP_LINKS_LOGTAG, "Unable to detect inbound App Links: " + e.getMessage());
}
} else {
Log.d(APP_LINKS_LOGTAG, "Context is not an instance of Activity. To detect inbound App Links, pass an instance of an Activity to getInstance.");
}
}

private static final String LOGTAG = "MixpanelAPI";
private static final String APP_LINKS_LOGTAG = "MixpanelAPI - App Links (OPTIONAL)";
private static final String ENGAGE_DATE_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss";

private final Context mContext;
Expand Down

0 comments on commit 495ade3

Please sign in to comment.