Tags: Notification Data

How to track and segment Mobile and Web Push users by clicked notification data

If you're a publisher that provides articles on different topics or an ecommerce site tracking product interest, there are 3 common things you will want to know about your notifications:

  1. The last time they clicked a notification (steps 1 & 3)
  2. What notification topics users click (steps 1, 2, & 3)
  3. How many times they are clicking that topic (steps 1, 2, & 3)

With OneSignal, you can track this behavior using Data Tags, adding some code to your site or app (provided below) and sending notifications with a topic.

Doing so, you can segment and reengage your users with content you already know they want.

Here's how it works:

Step 1. Add the Code

Leverage the OneSignal SDKs Notification Event Handlers to track when each user opened a notification.

Within these events, you will use the addTag and getTags SDK methods to add the received notification details to your user records.

In these examples, we will tag the user with 2 things:

  1. A unix timestamp of the "last_notification_click"
  2. The "notification_topic" of the notification sent within the custom data of the push

Here is some easy example code to get you started:

OneSignal.setNotificationOpenedHandler(
  new OneSignal.OSNotificationOpenedHandler() {
    @Override
    public void notificationOpened(OSNotificationOpenedResult result) {
      JSONObject data = result.getNotification().getAdditionalData();
      Log.i("OneSignalExample", "Notification Data: " + data);
      String notification_topic;
      long timestamp = System.currentTimeMillis() / 1000L;
      OneSignal.User.addTag("last_notification_click", String.valueOf(timestamp));

      if (data != null) {
        notification_topic = data.optString("notification_topic", null);
          if (notification_topic != null)
            OneSignal.User.addTag("notification_topic", notification_topic);
      }
    }
  }
);
let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
  let payload: OSNotificationPayload? = result?.notification.payload
  let timestamp = Int(NSDate().timeIntervalSince1970)
  OneSignal.User.addTags(["last_notification_click": timestamp])
  
	if let notification_topic = payload.additionalData?["notification_topic"] {
		OneSignal.User.addTags("notification_topic", value: notification_topic)
	}
}

Step 2. Send The Notifications With Data

Optional step if tracking notification topics

When creating a notification you simply add some Additional Data to the notification using our Dashboard or API data parameter.

This will be the topic of the notification and what you use to segment users. Common topics would be "news", "entertainment", "politics", "finance", "tech", etc.

Step 3. Segment

Now, whenever users click the notification, they will get automatically tagged with:

  1. the date (unix timestamp) they clicked the notification
  2. the notification's topic and how many total times that topic has been clicked

You can now segment subscribers based on this data.

Example segment users that did not click a push in over 1 week:

Set the User Tag data filter with key: last_notification_click time elapsed greater than 604800 seconds.

Example segment users that did click a push within 1 day:

Set the User Tag data filter with key: last_notification_click time elapsed less than 86400 seconds.

Simply switch to time elapsed greater than to target users that have not clicked a push in over x amount of time like 1209600 seconds (2 weeks = 60 seconds/min 60 minutes/hour 24 hours/day * 14 days).

Example segment users that clicked a push about politics 3 times or more

Set the User Tag data filter with key: politics *greater than 2.

👍

Done!

Finish setting up any more segments you need and continue Sending Push Messages with the notification_topic data.