Example: Tag with 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 sendTag 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.sendTag("last_notification_click", String.valueOf(timestamp));

      if (data != null) {
        notification_topic = data.optString("notification_topic", null);
          if (notification_topic != null)
            OneSignal.sendTag("notification_topic", notification_topic);
      }
    }
  }
);
let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
  let payload: OSNotificationPayload? = result?.notification.payload
  let timestamp = Int(NSDate().timeIntervalSince1970)
  OneSignal.sendTags(["last_notification_click": timestamp])
  
	if let notification_topic = payload.additionalData?["notification_topic"] {
		OneSignal.sendTag("notification_topic", value: notification_topic)
	}
}
OneSignal.push(["addListenerForNotificationOpened", function(payload) {
  // Track last notification click time (paid feature only)
  let timestamp = Math.floor(Date.now() / 1000);
  OneSignal.sendTag("last_notification_click", timestamp);

  console.log("Received Payload Data: ", payload.data);
  let notification_topic = payload.data.notification_topic; // set topic in notification
  if (notification_topic) {
    OneSignal.getTags().then(function(tags) {
      var topicCount = parseInt(tags[topic], 10);
      console.log("topicCount: ", topicCount);
      if (!isNaN(topicCount)) {
        topicCount += 1;
      } else {
        topicCount = 1;
      }
      OneSignal.sendTag(notification_topic, topicCount).then(function(tagsSent) {
        console.log("tagsSent: ", JSON.stringify(tagsSent));
      });
    });
  }
}]);

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.

623

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.

804

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).

804

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.

804

👍

Done!

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