OneSignal Help & Documentation

Welcome to the OneSignal New IA developer hub. You'll find comprehensive guides and documentation to help you start working with OneSignal New IA as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    Discussions

Auto-Segment By 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 device opened or received 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.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));
      });
    });
  }
}]);
class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
  // This fires when a notification is opened by tapping on it.
  @Override
  public void notificationOpened(OSNotificationOpenResult result) {
    JSONObject data = result.notification.payload.additionalData;
    String topic;
    long timestamp = System.currentTimeMillis() / 1000L;
    OneSignal.sendTag("last_notification_click", 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)
    }
}

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.

Updated 13 days ago



Auto-Segment By Notification Data


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

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.