nytimes / react-tracking

🎯 Declarative tracking for React apps.
https://open.nytimes.com/introducing-react-tracking-declarative-tracking-for-react-apps-2c76706bb79a
Other
1.87k stars 123 forks source link

Upgraded to React 18 and our tracking code is causing our app to break #218

Open anna-rusk opened 1 year ago

anna-rusk commented 1 year ago

React - "18.2.0" React Native - "0.71.4" React Tracking - "9.3.1"

Screenshot 2023-03-21 at 1 54 15 PM

Seeing this error - I confirmed that it was react-tracking code rather than hooks in general by removing all of our tracking code to find that our app works as expected there.

tizmagik commented 1 year ago

Closed via #201

anna-rusk commented 1 year ago

Full stack trace Screenshot 2023-06-09 at 8 47 59 AM

anna-rusk commented 1 year ago

This issue has me stumped because we changed nothing else about our environment except upgrade react, react-dom, and react-native.

I have pin pointed it to react-tracking library by removing our use of react-tracking hooks/initialization and our app can start. Do we need to resolve react to our own version in a bundler? Is it because your package has a direct dependency on an older version of react?

tizmagik commented 1 year ago

Hi @anna-rusk thanks for reporting. Would you mind creating a new issue and sharing your App component, or as much as you can about how you’ve setup react-tracking? Thanks!

tizmagik commented 1 year ago

Oh sorry I realize now this was your original issue. Reopening.

If you could share any more context or a minimal reproduction, I’d be happy to take a look!

anna-rusk commented 1 year ago

React - "18.2.0" React Native - "0.71.4" React Tracking - "9.3.1"

export function useEventTrackingImpl(api: AxiosInstance, platform: IEventPlatform): IUseEventTracking {
  const { trackEvent: _trackEvent, Track } = useTracking<IEvent>({}, { dispatch: dispatchFunc });

  const trackScreenChange: (o: { page: string, pageGroup?: PageGroup }) => void = ({ page, pageGroup }) => {

    _trackEvent({
      page: page,
      section: null,
      action: EventAction.VIEW,
      target: null,
      pageGroup: pageGroup,
      timestamp: Date.now(),
    });
  };

  const trackEvent = (data: IEventPayload) => {
    /* throw an error if CLICK action doesn't have target */
    if (data.action === EventAction.CLICK && !data.target) {
      throw new Error(
        "trackEvent: action CLICK requires 'target' to be specified in the payload"
      );
    }

    if (DEBUG_EVENTS) console.log("trackEvent", data);

    _trackEvent({
      page: data.page,
      section: data.section || null,
      action: data.action,
      target: data.target || null,
      pageGroup: data.pageGroup,
      timestamp: Date.now(),
    });
  };

  const dispatchTrackedEvents = async () => {
    const events = EventStore.get();
    EventStore.clear();
    if (events.length > 0) {
      const query = serializeEventsToQuery(events, Date.now(), platform);
      // send event batch to bertha
      try {
        await api.get(`/bertha${query}`);
      } catch (err) {
        console.error(err);
      }
    }
  };
  return { Track, trackEvent, trackScreenChange, dispatchTrackedEvents };
}

And we use that hook in our App initialize the components/hooks that are in the return object

anna-rusk commented 1 year ago

And the reproduction occurs when I launch our app from Xcode - there's a runtime error with the stack trace above ^^

anna-rusk commented 1 year ago

It is important to note that the way we use react tracking has been working just fine until we upgrade react/react native

tizmagik commented 1 year ago

Is it possible you have more than one copy of React bundled?