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

Limit batch size #961

Closed
matthieuMay opened this issue Jun 17, 2024 · 1 comment
Closed

Limit batch size #961

matthieuMay opened this issue Jun 17, 2024 · 1 comment

Comments

@matthieuMay
Copy link

When batching too much events, we often hit the 500kb limit of batch API.

Inspecting the events I realized that a lot of data sent is redundant (integrating context, _metadata, integrations)....

Reading the documentation of batch API it seems that integrations and context should be extracted which drastically reduce the payload size.

patch-package to realize this is :

diff --git a/node_modules/@segment/analytics-react-native/src/api.ts b/node_modules/@segment/analytics-react-native/src/api.ts
index a3048da..41e6099 100644
--- a/node_modules/@segment/analytics-react-native/src/api.ts
+++ b/node_modules/@segment/analytics-react-native/src/api.ts
@@ -9,12 +9,17 @@ export const uploadEvents = async ({
   url: string;
   events: SegmentEvent[];
 }) => {
+  const context = events.find((event) => !!event.context)?.context;
+  const integrations = events.find((event) => !!event.integrations)?.integrations;
+  let sentEvents = events.map(({context,integrations, ...event}) => (event));
   return await fetch(url, {
     method: 'POST',
     body: JSON.stringify({
-      batch: events,
+      batch: sentEvents,
       sentAt: new Date().toISOString(),
       writeKey: writeKey,
+      context: context,
+      integrations: integrations,
     }),
     headers: {
       'Content-Type': 'application/json; charset=utf-8',
@hvardhan-unth
Copy link
Contributor

Hey @matthieuMay
Thanks for the good suggestion.
Please raise a PR for the implementation you have mentioned here so that our team can review and consider it further.

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