amplitude / Amplitude-JavaScript

JavaScript SDK for Amplitude
MIT License
315 stars 132 forks source link

429 response TooManyRequestsForDeviceError #599

Closed arianshirepublik closed 1 year ago

arianshirepublik commented 1 year ago

Expected Behavior

Our page is a long list. When scrolling the page, we need to report the data When we use "amplitude-js": "8.21.2", a large number of 429 requests are found on the web side https://www.docs.developers.amplitude.com/analytics/apis/http-v2-api/?h=too+many+requests#413-response-payloadtoolargeerror

According to the official website, it means too many requests for a user or device. Amplitude throttles requests for users and devices that exceed 30 events per second. How is this counted? Is there any way to prevent the 429 error from appearing in the scrolling list? Does Amplitude itself provide an SDK for delayed reporting?

Current Behavior

image

Environment

Mercy811 commented 1 year ago

Hi @arianshirepublik, what is the 429 response looks like? There are actually two cases of 429

  1. like the example shows, events are throttled because it exceeds the eps_threshold which is 30 in this example. You can set batchEvents to true and config eventUploadPeriodMillis and eventUploadThreshold to hold off events temporarily
  2. if it is caused by exceeding daily quota then there is no workaround to send more events

Also it's recommended to upgrade to the latest browser SDK which supports retry logics

Mercy811 commented 1 year ago

You can also set logLevel to DISABLE to simply hide it

arianshirepublik commented 1 year ago

Hi Mercy811, 429 response is:too_many_requests

image

According to your tips, I made relevant adjustments:

   const instance = amplitude.getInstance()
if (appConfig.amplitudeKey) {
  // https://github.com/amplitude/Amplitude-JavaScript/issues/599
  instance.init(appConfig.amplitudeKey, null, {
    batchEvents: true,
    // Events queued in memory will flush when number of events exceed upload threshold
    // Default value is 30
    eventUploadThreshold: 50,
    // Events queue will flush every certain milliseconds based on setting
    // Default value is 30000 milliseconds
    eventUploadPeriodMillis: 60000,
  })
}

But I found that it only works for the first time when the user refreshes the page, after that it doesn't work

https://github.com/amplitude/Amplitude-JavaScript/assets/133647949/fe814114-dfee-4c79-bad0-60f141f47352

@Mercy811 Can you have a look?

Mercy811 commented 1 year ago

Hi Arian, we are currently experiencing a high volume of support tickets. However, please be assured that your specific issue remains on our priority list. We sincerely appreciate your patience during this time. We've never seen similar issues before and still need more investigate on this. We also recommend to migrate to our latest browser SDK to see if it can fix this problem as the JavaScript browser SDK is in maintenance status and will finally be deprecated.

Mercy811 commented 1 year ago

Hi @arianshirepublik, 429 response means that, like what you've found in our dev docs, we currently cannot receive the events sent because you send them too much within a short time. It's not a SDK issue and the SDK works as expected to show that some events are not received by Amplitude in the Network tab which is not visible by customers.

If you are worried about data loss, I would recommend you to upgrade to the latest Browser SDK which supports retry logic to handle 429 response and send events later.