adjust / web_sdk

40 stars 25 forks source link

trackEvent request is not sent before page redirect with React #50

Closed fungcheukyin-TeamLab closed 1 year ago

fungcheukyin-TeamLab commented 1 year ago

I am trying to send a clicking event by AdjustSdk.trackEvent before doing external page redirect. However the request is not sent even if I tried to make it asynchronus. Is there any possible way to achieve this?

AdjustSdk.trackEvent('token', [
      { key: 'sample_key', value: 'sample_value' },
]);
// external page redirect
router.push()
YaraMatkova commented 1 year ago

Hey @fungcheukyin-TeamLab, thank you for reaching out. Most likely this happens because there is not enough time between trackEvent call and redirect to the external page for the SDK to send the event. Could you please share verbose logs from Browser Console where the also navigation to external page would be visible?

fungcheukyin-TeamLab commented 1 year ago

@YaraMatkova Sorry for replying late. I tried to reproduce the issue and take a screenshot on the network traffic tools. It seems that the event request is not completed before the page redirect. image

After I go back to the previous page, the request is sent again automatically. image

YaraMatkova commented 1 year ago

Hey @fungcheukyin-TeamLab. Sorry for another delay from my side. I hope you had happy and merry holidays!

I see. It's kind of an edge case. Web SDK has an internal queue to store requests and sent them to the backend. The event is placed in the queue, the SDK sends it, and after the SDK gets a response the event is removed from the queue. If Web SDK didn't get the response as it happens when user is redirected to external page, the event would be sent after Web SDK initialised next time. So I would expect to see duplicating events in your Adjust Dashboard.

I can suggest you a workaround here, you could wait a bit before redirecting user to external page. For example something like this:

Adjust.trackEvent({...});
const delay = 1000; // I really can't give an advice on this value, it depends on network speed
setTimeout(() => { router.push(); }, delay);

We added a ticket to our backlog to return a Promise from trackEvent method to make it possible to know when the event tracking was actually finished. Will keep you posted.

YaraMatkova commented 1 year ago

Hi @fungcheukyin-TeamLab, the ticket I've talked above is done and a new SDK version released. Now to make sure the event was tracked earlier than the redirect happen you could use something like this:

Promise.race([
  Adjust.trackEvent({
    eventToken: 'YOUR_EVENT_TOKEN',
    // ... other event parameters
  }),
  new Promise((resolve, reject) => {
    setTimeout(() => reject('Tracking an event did not complete within 2 seconds, timed out'), 2000);
  })
])
  .catch(error => console.error(error))
  .finally(() => router.push());

Please check Tracking an event and redirect to an external page chapter of our README for further information.

Feel free to reopen or comment if you have any other questions.