How to Enable OneSignal Web Push Notifications SendTag For Drupal (User Segmentation by Interests)

| Servers | 9 seen

I spent a good amount of time, trying to learn how to implement a better user segmentation on OneSignal. The best I could get so far was the default OneSiganal's out of the box segmentation based on user country.

As my blog covers many mutually unrelated topics, I was not utilizing the power of push notifications at 100%. In fact, after getting more than 26,000 subscribers (over a year) I felt afraid every-time I sent out a web push notification, as user targeting was very poor.

At OneSignal official documentation, I learned about using Data Tags, especially SendTag and SendTags

Once you've integrated OneSignal in your app or website, implementing tags is very straightforward - simply use the sendTag or sendTags methods to tag your users with a key-value pair at any time. You may also get and delete tags with other methods. To learn more and to check out code examples, dive into the SDK reference for your particular app or website.

Here is the code example, you should call

OneSignal.push(function() {
  /* These examples are all valid */
  OneSignal.sendTag("key", "value");
               
  OneSignal.sendTag("key", "value", function(tagsSent) {
    // Callback called when tags have finished sending
  });
               
  OneSignal.sendTag("key", "value").then(function(tagsSent) {
    // Callback called when tags have finished sending
  });  
});

As it was hard to understand what to do next, I thought about following "workaround" using Drupal blocks, before diving in, here are the things you will need:

Now, create a new block and paste in (using php filter) following code:

<script>
OneSignal.push(function() {
  OneSignal.sendTag("category", "investments");         
});
</script>

Where category = key and investments = value

Make sure to enable check box to display this block on investments category only. Also, you should put this block somewhere in enabled block regions to make it work. As I'm using custom MailChimp subscribe forms for collecting e-mail at te the end of articles, I put this code inside MailChimp block.

How to add MailChimp Signup Form for Drupal

For tracking multiple categories create new block, with new key and value, for example


<script>
OneSignal.push(function() {
  OneSignal.sendTag("category1", "shopping");         
});
</script>

And so on. 

In case user has already subscribed at investment page, but after is visiting shopping page, both values will be recorded:

Collecting user tags on OneSignal

Collecting user tags on OneSignal

Add new comment