Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dosercz opened this issue Dec 5, 2023 · 2 comments

Comments

@dosercz
Copy link

dosercz commented Dec 5, 2023

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
Copy link
Member

jkasten2 commented Dec 6, 2023

@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
Copy link
Author

dosercz commented Dec 15, 2023

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants