OneSignal / OneSignal-Website-SDK

OneSignal is a push notification service for web and mobile apps. This SDK makes it easy to integrate your website with OneSignal Push Notifications. https://onesignal.com
Other
389 stars 115 forks source link

[Question]: Adding tag to user not working? #1177

Closed LelvProgrammer closed 5 months ago

LelvProgrammer commented 6 months ago

I created a new one signal app, followed the steps of configuring the web script, copy-pasting the configuration added + calling the method for adding a tag, on the head of the index file

<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
<script>
    window.OneSignalDeferred = window.OneSignalDeferred || [];
    OneSignalDeferred.push(function(OneSignal) {
        OneSignal.init({
            appId: "XXXXXXXXXXXXXXXXXXXX",
        });
        OneSignal.User.addTag("test-tag", "whatever")
    });
</script>

When I go to the onesignal dashboard, I see the new client registered, but no tags assigned to him. If I were to send a message filtering for tag, the person doesn't receive the message. I feel like I'm probably just missing something silly but can't put my finger onto what that is. Anyone can help?

shepherd-l commented 5 months ago

OneSignal.init({}) is an asynchronous method and you need to call await. Try updating your code to this:

    OneSignalDeferred.push(async function(OneSignal) {
        await OneSignal.init({
            appId: "XXXXXXXXXXXXXXXXXXXX",
        });
        OneSignal.User.addTag("test-tag", "whatever")
    });

Thanks for bringing attention to this, our documentation needs to be updated Hope this helps. Let us know if you have any issues