If you’re a publisher with topic-based content or an eCommerce site tracking product interest, you can segment your users based on the custom data in notifications they interact with.

With OneSignal, you can do this using Data Tags. By tagging users when they click a notification—based on metadata you define in the notification payload—you can create rich, behavior-driven segments.


1. Add the Code

Use OneSignal SDK’s notification event handlers to detect when a notification is opened. In that handler, extract your custom data from the payload and use addTag or addTags to store it.

In this example, we tag the user with a "notification_topic" from the custom data. You can add any other fields you want—like category, product type, or campaign.

OneSignal.setNotificationOpenedHandler(
  new OneSignal.OSNotificationOpenedHandler() {
    @Override
    public void notificationOpened(OSNotificationOpenedResult result) {
      JSONObject data = result.getNotification().getAdditionalData();
      Log.i("OneSignalExample", "Notification Data: " + data);

      if (data != null) {
        String topic = data.optString("notification_topic", null);
        if (topic != null)
          OneSignal.User.addTag("notification_topic", topic);

        // Add more tags from custom data if needed
        String category = data.optString("category", null);
        if (category != null)
          OneSignal.User.addTag("notification_category", category);
      }
    }
  }
);
```swift Swift
let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
let payload: OSNotificationPayload? = result?.notification.payload
let additionalData = payload?.additionalData

if let topic = additionalData?["notification_topic"] as? String {
  OneSignal.User.addTags(["notification_topic": topic])
}

if let category = additionalData?["category"] as? String {
  OneSignal.User.addTags(["notification_category": category])
}

// Add more tags from custom data if needed
}

2. Add custom data to your notifications

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.

Adding notification topic data in dashboard

3. Segment based on the tags

You can now create segments based on the custom data values users clicked on.

Example: Users who clicked a finance topic

  • Key: notification_topic
  • Condition: equals
  • Value: finance

Segmenting users based on topic clicked

4. Send notifications with the custom data

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.