Skip to content

Commit

Permalink
properties for $campaign_open for the cta url and if the button was p…
Browse files Browse the repository at this point in the history
…rimary or secondary. also now always track $campaign_open if they tap on either button or mini, instead of only if there was a ctal url
  • Loading branch information
Peter Chien committed Sep 11, 2017
1 parent 9457f8c commit 4151e0a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
Expand Up @@ -168,7 +168,7 @@ public void showGivenNotification(InAppNotification notif, final Activity parent
}

@Override
public void trackNotification(String eventName, InAppNotification notif) {
public void trackNotification(String eventName, InAppNotification notif, JSONObject properties) {
Assert.fail("Unexpected call");
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/mixpanel/android/mpmetrics/InAppFragment.java
Expand Up @@ -35,6 +35,9 @@
import com.mixpanel.android.util.MPLog;
import com.mixpanel.android.util.ViewUtils;

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

/**
* Attached to an Activity when you display a mini in-app notification.
*
Expand Down Expand Up @@ -135,6 +138,7 @@ public void onShowPress(MotionEvent e) { }
public boolean onSingleTapUp(MotionEvent event) {
final MiniInAppNotification inApp = (MiniInAppNotification) mDisplayState.getInAppNotification();

JSONObject trackingProperties = null;
final String uriString = inApp.getCtaUrl();
if (uriString != null && uriString.length() > 0) {
Uri uri;
Expand All @@ -148,11 +152,18 @@ public boolean onSingleTapUp(MotionEvent event) {
try {
Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
mParent.startActivity(viewIntent);
mMixpanel.getPeople().trackNotification("$campaign_open", inApp);
} catch (ActivityNotFoundException e) {
MPLog.i(LOGTAG, "User doesn't have an activity for notification URI " + uri);
}

try {
trackingProperties = new JSONObject();
trackingProperties.put("url", uriString);
} catch (final JSONException e) {
MPLog.e(LOGTAG, "Can't put url into json properties");
}
}
mMixpanel.getPeople().trackNotification("$campaign_open", inApp, trackingProperties);

remove();
return true;
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/mixpanel/android/mpmetrics/MixpanelAPI.java
Expand Up @@ -1165,8 +1165,10 @@ public interface People {
* @param eventName the name to use when the event is tracked.
*
* @param notif the {@link com.mixpanel.android.mpmetrics.InAppNotification} associated with the event you'd like to track.
*
* @param properties additional properties to be tracked with the event.
*/
public void trackNotification(String eventName, InAppNotification notif);
public void trackNotification(String eventName, InAppNotification notif, JSONObject properties);

/**
* Returns an InAppNotification object if one is available and being held by the library, or null if
Expand Down Expand Up @@ -1586,7 +1588,7 @@ public void trackNotificationSeen(InAppNotification notif) {
if(notif == null) return;

mPersistentIdentity.saveCampaignAsSeen(notif.getId());
trackNotification("$campaign_delivery", notif);
trackNotification("$campaign_delivery", notif, null);
final MixpanelAPI.People people = getPeople().withIdentity(getDistinctId());
final DateFormat dateFormat = new SimpleDateFormat(ENGAGE_DATE_FORMAT_STRING, Locale.US);
final JSONObject notifProperties = notif.getCampaignProperties();
Expand Down Expand Up @@ -1622,8 +1624,20 @@ public void showGivenNotification(final InAppNotification notif, final Activity
}

@Override
public void trackNotification(final String eventName, final InAppNotification notif) {
track(eventName, notif.getCampaignProperties());
public void trackNotification(final String eventName, final InAppNotification notif, final JSONObject properties) {
JSONObject notificationProperties = notif.getCampaignProperties();
if (properties != null) {
try {
Iterator<String> keyIterator = properties.keys();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
notificationProperties.put(key, properties.get(key));
}
} catch (final JSONException e) {
MPLog.e(LOGTAG, "Exception merging provided properties with notification properties", e);
}
}
track(eventName, notificationProperties);
}

@Override
Expand Down
Expand Up @@ -35,6 +35,9 @@
import com.mixpanel.android.util.MPLog;
import com.mixpanel.android.util.ViewUtils;

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

import java.util.ArrayList;

/**
Expand Down Expand Up @@ -117,7 +120,7 @@ private void onCreateInAppNotification() {
InAppButton inAppButtonModel = inApp.getButton(i);
Button inAppButton = ctaButtons.get(i);

setUpInAppButton(inAppButton, inAppButtonModel, inApp);
setUpInAppButton(inAppButton, inAppButtonModel, inApp, i);
}

if (inApp.getNumButtons() == 1) {
Expand All @@ -139,7 +142,7 @@ public void onClick(View v) {
setUpNotificationAnimations(inAppImageView, titleView, subtextView, ctaButtons, closeButtonWrapper);
}

private void setUpInAppButton(Button inAppButton, final InAppButton inAppButtonModel, final InAppNotification inApp) {
private void setUpInAppButton(Button inAppButton, final InAppButton inAppButtonModel, final InAppNotification inApp, final int buttonIndex) {
if (inAppButtonModel != null) {
inAppButton.setVisibility(View.VISIBLE);
inAppButton.setText(inAppButtonModel.getText());
Expand Down Expand Up @@ -174,6 +177,7 @@ public boolean onTouch(View v, MotionEvent event) {
inAppButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JSONObject trackingProperties = null;
final String uriString = inAppButtonModel.getCtaUrl();
if (uriString != null && uriString.length() > 0) {
Uri uri;
Expand All @@ -187,11 +191,31 @@ public void onClick(View v) {
try {
final Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
TakeoverInAppActivity.this.startActivity(viewIntent);
mMixpanel.getPeople().trackNotification("$campaign_open", inApp);
} catch (final ActivityNotFoundException e) {
MPLog.i(LOGTAG, "User doesn't have an activity for notification URI");
}

try {
trackingProperties = new JSONObject();
trackingProperties.put("url", uriString);
} catch (final JSONException e) {
MPLog.e(LOGTAG, "Can't put url into json properties");
}
}
String whichButton = "primary";
final TakeoverInAppNotification takeoverInApp = (TakeoverInAppNotification)inApp;
if (takeoverInApp.getNumButtons() == 2) {
whichButton = (buttonIndex == 0) ? "secondary" : "primary";
}
try {
if (trackingProperties == null) {
trackingProperties = new JSONObject();
}
trackingProperties.put("button", whichButton);
} catch (final JSONException e) {
MPLog.e(LOGTAG, "Can't put button type into json properties");
}
mMixpanel.getPeople().trackNotification("$campaign_open", inApp, trackingProperties);
finish();
UpdateDisplayState.releaseDisplayState(mIntentId);
}
Expand Down

0 comments on commit 4151e0a

Please sign in to comment.