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
367 stars 191 forks source link

[2.20.0] TypeError · Object is not a function on pluginResult.enrichment(pluginResult) #1011

Closed efstathiosntonas closed 1 month ago

efstathiosntonas commented 1 month ago

Steps to reproduce

Expected behavior

Actual behavior

After upgrading to 2.20.0 I get this crash:

Object is not a function

node_modules/@segment/analytics-react-native/src/timeline.ts:79:42:


if (result === undefined) {
  return;
   } else if (key === PluginType.enrichment && pluginResult?.enrichment) {
   result = pluginResult.enrichment(pluginResult); <---- this line throws the error
  } else {
  result = pluginResult;
   }

my setup:

import { createClient, SegmentClient } from '@segment/analytics-react-native';
import Keys from 'react-native-keys';

import { InjectTraits } from '@utils/InjectTraits';

let client: SegmentClient | null = null;

function segment(): SegmentClient {
  if (!client) {
    client = createClient({
      writeKey: __DEV__ ? '' : Keys.secureFor('SEGMENT_WRITE_KEY'),
      debug: false, // __DEV__,
      flushAt: 10,
      trackAppLifecycleEvents: true
    });
    client.add({ plugin: new InjectTraits() });
  }
  return client;
}

export default segment;

InjectTraits.ts

import {
  PlatformPlugin,
  PluginType,
  SegmentEvent
} from '@segment/analytics-react-native';

/**
 * Plugin that injects the user traits to every event
 */
export class InjectTraits extends PlatformPlugin {
  type = PluginType.before;

  execute(event: SegmentEvent) {
    return {
      ...event,
      context: {
        ...event.context,
        traits: {
          ...event.context,
          ...this.analytics!.userInfo.get().traits
        }
      }
    };
  }
}

index.js

const analyticsClient = segment();

const ThemedApp = () => {
  return (
      <AnalyticsProvider client={analyticsClient}>
          <App />
      </AnalyticsProvider>
  );
};
efstathiosntonas commented 1 month ago

downgrading to 2.19.5 works fine.

wenxi-zeng commented 1 month ago

hi @efstathiosntonas thanks for reporting this. can you please provide the line where segmentClient.track/screen/etc being called? looks like the new optional parameter enrichment causes confusion. will send a fix.

efstathiosntonas commented 1 month ago

Hi @wenxi-zeng, these are the calls to track:

 const { track } = useAnalytics();

   track('User XXX_1', {
        status: status.trim()
      }).catch();

  track('User XXX_2').catch();
wenxi-zeng commented 1 month ago

@efstathiosntonas please try the latest version 2.20.1.

we couldn't reproduce the issue on our end though. it looks like the closure lost context somewhere and was treated as a normal object. but we did able to force the issue to happen if we pass an any object to it. the fix add a safe check on whether the closure is a function. let us know if your issue still persists.