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
383 stars 115 forks source link

[Question]: OneSignal.User.addTags() is not synchronous and tags are not send #1147

Open dosercz opened 7 months ago

dosercz commented 7 months ago

How can we help?

I'm trying to addTags() in javascript web sdk v16. All User functions should be synchronous there. But added tags are not send to server in two cases: 1) Tags are added on page load and OneSignal is not init probably yet. 2) Tags are added before page redirect and redirect is done before tags are sent to server probably. It works fine, if page is loaded and there is only small delay before api request to server.

Loaded SDK https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js with defer attribute. 1) Page load use case:

            window.OneSignalDeferred = window.OneSignalDeferred || [];
            OneSignalDeferred.push(async function(OneSignal) {
                await OneSignal.init({
                    appId: $appId,
                    autoResubscribe: true,
                    notifyButton: {
                        enable: true,
                        position: 'bottom-left'
                    }
                });
            });
            // no api call after these call of addTags()
            OneSignalDeferred.push(function (OneSignal) {
                OneSignal.User.addTags({
                    cart_update: '',
                    product_name: '',
                    product_image: ''
                });
            });

How can I wait for OneSignal init?

2 Redirect use case:

        window.OneSignalDeferred.push(async function (OneSignal) {
            await OneSignal.User.addTags({
                cart_update: '',
                product_name: '',
                product_image: ''
            });

            // change location there
            ...
        });

How can I wait to saving tags?

jkasten2 commented 7 months ago

@dosercz Thanks for reporting this! Responding to each question in the sections below.

1. Tags are added on page load and OneSignal is not init probably yet.

I am able to reproduce the result you are seeing with the code you provided. We will look into accounting for this scenario so this isn't a problem in the future, in the meantime there is a work around:

window.OneSignalDeferred = window.OneSignalDeferred || [];
OneSignalDeferred.push(async function(OneSignal) {
    await OneSignal.init({
        appId: $appId,
        autoResubscribe: true,
        notifyButton: {
            enable: true,
            position: 'bottom-left'
        }
    });
    OneSignal.User.addTags({
        cart_update: '',
        product_name: '',
        product_image: ''
    });
});

2. Tags are added before page redirect and redirect is done before tags are sent to server probably.

If the redirect location is on your same domain and is a page you also initialize OneSignal the SDK will send the tags once the new page has loaded. If you are redirecting to a different domain then the tags will only be sent once the user has revisited your page.

dosercz commented 6 months ago

@jkasten2 thanks for help, 1. workaround works fine, but 2. seems it doesn't work for me, we have OneSignal initialized at all pages, but if tags were not sent to server at page, where they were added via OneSignal.User.addTags(), they are not sent after redirect/navigation (I'm not sure if never but in the most cases). Could you check it please?