braze-inc / braze-web-sdk

Public repo for the Braze Web SDK
https://www.braze.com
Other
73 stars 25 forks source link

No cards received from appBoy.getCachedContentCards() in 2.4.0 #74

Closed kevinrodrigues closed 4 years ago

kevinrodrigues commented 4 years ago

Hi,

I seem to be having an issue with the method getCachedContentCards returning an empty array the very first time a user lands on a page (the second refresh is fine). I've narrowed it down to a localStorage value ab.storage.cc.[api-key] that seems to be set to "{"v":[]}" initially and then it gets populated but by that time the sync has already happened. On second reload of the browser I get cards coming back fine because the localStorage value mentioned is already in place from the first load.

In terms of the rough implementation please see below, please also note that this is only an issue on mobile safari I've tried several iPhones on BrowserStack and this is the same issue across them all including actual physical devices. I did see an issue here that looks related and tried using the appboy.subscribeToContentCardsUpdates() in place of the getCachedContentCards() call below but it still doesn't work.

There might be something really obvious that I've missed or left out here so please let me know and I'll try to provide that for you.

// The `appboy` sdk is imported using dynamic imports e.g import(/* webpackChunkName: "appboy-web-sdk" */ 'appboy-web-sdk')

appboy.initialize(options.apiKey, { enableLogging: options.enableLogging });
appboy.display.automaticallyShowNewInAppMessages();
appboy.openSession();
appboy.requestContentCardsRefresh();
const contentCards = appboy.getCachedContentCards()

/* 
Logging out when the user lands the first time returns the below, the second time the `lastUpdate` gets updated and the cards array is populated too.

console.log(contentCards) => {"cards":[],"lastUpdated":null}
*/

if (contentCards && contentCards.cards.length) {
   options.callbacks.handleContentCards(contentCards.cards);
}
wesleyorbin commented 4 years ago

Hi @kevinrodrigues,

The backend determines which content cards to send each user, so this behavior is expected since the SDK always starts with an empty array of cards for new users.

As you mentioned, you should be using subscribeToContentCardsUpdates with something like below:

// initialize, openSession, etc
appboy.subscribeToContentCardsUpdates(contentCards => {
  options.callbacks.handleContentCards(contentCards.cards);
});

Please let me know if this does not work for you or if I misunderstood your issue. Note it may take some time for the callback to run depending on how long it takes for the sync call to come through.

Also, if you're using 2.4.0 and the core version of the SDK (appboy.core.min.js), I highly recommend upgrading to at least 2.4.1 because 2.4.0 contains a bug with content cards (see the changelog).

kevinrodrigues commented 4 years ago

@wesleyorbin Thanks a lot for your super quick reply. I've tried this and looks like it works as expected now 🎉 . I'll test it a bit more this week in a release and if it's resolved I'll close this ticket by the end of this week if that's ok. Thanks again for your help! Out of interest are there any other useful API docs other than this one that I've been using? https://js.appboycdn.com/web-sdk/latest/doc/module-appboy.html

wesleyorbin commented 4 years ago

@kevinrodrigues The docs that you linked are the official API docs for the Web SDK. You can also check our integration guide or contact our our support team and they would be happy to help you with any integration issues.