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

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

Closed
efstathiosntonas opened this issue Oct 10, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@efstathiosntonas
Copy link

efstathiosntonas commented Oct 10, 2024

  • analytics-react-native version: 2.20.0
  • Integrations versions (if used):
  • React Native version: 0.75.4 Paper
  • iOS or Android or both? Both

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 efstathiosntonas added the bug Something isn't working label Oct 10, 2024
@efstathiosntonas
Copy link
Author

downgrading to 2.19.5 works fine.

@wenxi-zeng
Copy link
Contributor

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
Copy link
Author

efstathiosntonas commented Oct 11, 2024

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
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants