Skip to content

Commit

Permalink
Merge pull request #3448 from wordpress-mobile/feature/3390-track-pus…
Browse files Browse the repository at this point in the history
…h-notifications

[Analytics] - Track push notifications
  • Loading branch information
roundhill committed Dec 5, 2015
2 parents fbc2001 + f9cbb1b commit ac3f98d
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public class GCMMessageService extends GcmListenerService {
private static final String PUSH_TYPE_REBLOG = "reblog";
private static final String PUSH_TYPE_PUSH_AUTH = "push_auth";

// Add to the analytics properties map a subset of the push notification payload.
private static String[] propertiesToCopyIntoAnalytics = { PUSH_ARG_NOTE_ID, PUSH_ARG_TYPE, "blog_id", "post_id", "comment_id" };

private void handleDefaultPush(String from, Bundle data) {
// Ensure Simperium is running so that notes sync
SimperiumUtils.configureSimperium(this, AccountHelper.getDefaultAccount().getAccessToken());
Expand All @@ -87,6 +90,7 @@ private void handleDefaultPush(String from, Bundle data) {
return;
}


String title = StringEscapeUtils.unescapeHtml(data.getString(PUSH_ARG_TITLE));
if (title == null) {
title = getString(R.string.app_name);
Expand Down Expand Up @@ -152,17 +156,21 @@ private void handleDefaultPush(String from, Bundle data) {
}
}

// Bump Analytics
Map<String, String> properties = new HashMap<>();
if (!TextUtils.isEmpty(noteType)) {
// 'comment' and 'comment_pingback' types are sent in PN as type = "c"
if (noteType.equals(PUSH_TYPE_COMMENT)) {
properties.put("notification_type", "comment");
} else {
properties.put("notification_type", noteType);
// Bump Analytics for PNs if "Show notifications" setting is checked (default). Skip otherwise.
if (NotificationsUtils.isNotificationsEnabled(this)) {
Map<String, Object> properties = new HashMap<>();
if (!TextUtils.isEmpty(noteType)) {
// 'comment' and 'comment_pingback' types are sent in PN as type = "c"
if (noteType.equals(PUSH_TYPE_COMMENT)) {
properties.put("notification_type", "comment");
} else {
properties.put("notification_type", noteType);
}
}

bumpPushNotificationsAnalytics(Stat.PUSH_NOTIFICATION_RECEIVED, data, properties);
AnalyticsTracker.flush();
}
AnalyticsTracker.track(Stat.PUSH_NOTIFICATION_RECEIVED, properties);

NotificationCompat.Builder builder;

Expand Down Expand Up @@ -413,7 +421,9 @@ public static void removeNotification(int notificationId) {
mActiveNotificationsMap.remove(notificationId);
}

// Removes all app notifications from the system bar
// Removes all app notifications from the system bar.
// This is called when the Notifications tab is resumed. EX: app started, app resumed, even if
// the current screen is not the Notifications.
public static void removeAllNotifications(Context context) {
if (context == null || !hasNotifications()) return;

Expand All @@ -425,4 +435,48 @@ public static void removeAllNotifications(Context context) {

clearNotifications();
}

//NoteID is the ID if the note in WordPress
public static void bumpPushNotificationsTappedAnalytics(String noteID) {
for (int id : mActiveNotificationsMap.keySet()) {
Bundle noteBundle = mActiveNotificationsMap.get(id);
if (noteBundle.getString(PUSH_ARG_NOTE_ID, "").equals(noteID)) {
bumpPushNotificationsAnalytics(Stat.PUSH_NOTIFICATION_TAPPED, noteBundle, null);
AnalyticsTracker.flush();
return;
}
}
}

// Mark all notifications as tapped
public static void bumpPushNotificationsTappedAllAnalytics() {
for (int id : mActiveNotificationsMap.keySet()) {
Bundle noteBundle = mActiveNotificationsMap.get(id);
bumpPushNotificationsAnalytics(Stat.PUSH_NOTIFICATION_TAPPED, noteBundle, null);
}
AnalyticsTracker.flush();
}

private static void bumpPushNotificationsAnalytics(Stat stat, Bundle noteBundle, Map<String, Object> properties) {
// Bump Analytics for PNs if "Show notifications" setting is checked (default). Skip otherwise.
if (!NotificationsUtils.isNotificationsEnabled(WordPress.getContext())) {
return;
}
if (properties == null) {
properties = new HashMap<>();
}

String notificationID = noteBundle.getString(PUSH_ARG_NOTE_ID, "");
if (!TextUtils.isEmpty(notificationID)) {
for (String currentPropertyToCopy: propertiesToCopyIntoAnalytics) {
if (noteBundle.containsKey(currentPropertyToCopy)) {
properties.put("push_notification_" + currentPropertyToCopy, noteBundle.get(currentPropertyToCopy));
}
}
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(WordPress.getContext());
String lastRegisteredGCMToken = preferences.getString(NotificationsUtils.WPCOM_PUSH_DEVICE_TOKEN, null);
properties.put("push_notification_token", lastRegisteredGCMToken);
AnalyticsTracker.track(stat, properties);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.UUID;

public class GCMRegistrationIntentService extends IntentService {

public GCMRegistrationIntentService() {
super("GCMRegistrationIntentService");
}
Expand Down Expand Up @@ -53,6 +54,7 @@ public void sendRegistrationToken(String gcmToken) {
uuid = UUID.randomUUID().toString();
preferences.edit().putString(NotificationsUtils.WPCOM_PUSH_DEVICE_UUID, uuid).apply();
}
preferences.edit().putString(NotificationsUtils.WPCOM_PUSH_DEVICE_TOKEN, gcmToken).apply();
NotificationsUtils.registerDeviceForPushNotifications(this, gcmToken);
}

Expand All @@ -61,6 +63,7 @@ public void sendRegistrationToken(String gcmToken) {
AnalyticsTracker.registerPushNotificationToken(gcmToken);
} else {
AppLog.w(T.NOTIFS, "Empty GCM token, can't register the id on remote services");
PreferenceManager.getDefaultSharedPreferences(this).edit().remove(NotificationsUtils.WPCOM_PUSH_DEVICE_TOKEN).apply();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,12 @@ private void launchWithNoteId() {
if (GCMMessageService.getNotificationsCount() == 1) {
String noteId = getIntent().getStringExtra(NotificationsListFragment.NOTE_ID_EXTRA);
if (!TextUtils.isEmpty(noteId)) {
GCMMessageService.bumpPushNotificationsTappedAnalytics(noteId);
NotificationsListFragment.openNote(this, noteId, shouldShowKeyboard, false);
}
} else {
// mark all tapped here
GCMMessageService.bumpPushNotificationsTappedAllAnalytics();
}

GCMMessageService.clearNotifications();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public void onReceive(Context context, Intent intent) {
GCMMessageService.clearNotifications();
} else {
GCMMessageService.removeNotification(notificationId);

// Dismiss the grouped notification if a user dismisses all notifications from a wear device
if (!GCMMessageService.hasNotifications()) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ public void onResume() {
mBucket.addListener(this);
}

// Remove app notification if it is showing when we resume
cancelNotifications();
// Removes app notifications from the system bar
new Thread(new Runnable() {
public void run() {
GCMMessageService.removeAllNotifications(getActivity());
}
}).start();

if (SimperiumUtils.isUserAuthorized()) {
SimperiumUtils.startBuckets();
Expand Down Expand Up @@ -474,15 +478,6 @@ public void onStart() {
EventBus.getDefault().registerSticky(this);
}

// Removes app notifications from the system bar
private void cancelNotifications() {
new Thread(new Runnable() {
public void run() {
GCMMessageService.removeAllNotifications(getActivity());
}
}).start();
}

@SuppressWarnings("unused")
public void onEventMainThread(NotificationEvents.NoteModerationStatusChanged event) {
setNoteIsModerating(event.mNoteId, event.mIsModerating);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class NotificationsUtils {

public static final String WPCOM_PUSH_DEVICE_NOTIFICATION_SETTINGS = "wp_pref_notification_settings";
public static final String WPCOM_PUSH_DEVICE_UUID = "wp_pref_notifications_uuid";
public static final String WPCOM_PUSH_DEVICE_TOKEN = "wp_pref_notifications_token";

public static final String WPCOM_PUSH_DEVICE_SERVER_ID = "wp_pref_notifications_server_id";
private static final String PUSH_AUTH_ENDPOINT = "me/two-step/push-authentication";
Expand Down Expand Up @@ -147,6 +148,7 @@ public void onResponse(JSONObject jsonObject) {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
editor.remove(WPCOM_PUSH_DEVICE_SERVER_ID);
editor.remove(WPCOM_PUSH_DEVICE_UUID);
editor.remove(WPCOM_PUSH_DEVICE_TOKEN);
editor.apply();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public enum Stat {
PERFORMED_JETPACK_SIGN_IN_FROM_STATS_SCREEN,
STATS_SELECTED_INSTALL_JETPACK,
PUSH_NOTIFICATION_RECEIVED,
PUSH_NOTIFICATION_TAPPED, // Same of opened
SUPPORT_OPENED_HELPSHIFT_SCREEN,
SUPPORT_SENT_REPLY_TO_SUPPORT_MESSAGE,
LOGIN_FAILED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,11 @@ private AnalyticsTrackerMixpanelInstructionsForStat instructionsForStat(
break;
case PUSH_NOTIFICATION_RECEIVED:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
mixpanelInstructionsForEventName("Push Notification Received");
mixpanelInstructionsForEventName("Push Notification - Received");
break;
case PUSH_NOTIFICATION_TAPPED:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
mixpanelInstructionsForEventName("Push Notification - Alert Tapped");
break;
case SUPPORT_OPENED_HELPSHIFT_SCREEN:
instructions = AnalyticsTrackerMixpanelInstructionsForStat.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ public void track(AnalyticsTracker.Stat stat, Map<String, ?> properties) {
case PUSH_NOTIFICATION_RECEIVED:
eventName = "push_notification_received";
break;
case PUSH_NOTIFICATION_TAPPED:
eventName = "push_notification_alert_tapped";
break;
case SUPPORT_OPENED_HELPSHIFT_SCREEN:
eventName = "support_helpshift_screen_opened";
break;
Expand Down

0 comments on commit ac3f98d

Please sign in to comment.