segmentio / analytics-react-native

The hassle-free way to add analytics to your React-Native app.
https://segment.com/docs/sources/mobile/react-native/
MIT License
354 stars 181 forks source link

Limit batch size #961

Open matthieuMay opened 1 week ago

matthieuMay commented 1 week ago

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 commented 4 days ago

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.