PostHog / posthog-js-lite

Reimplementation of posthog-js to be as light and modular as possible.
https://posthog.com/docs/libraries
MIT License
69 stars 36 forks source link

React Native Session Replays missing network requests #276

Closed jrusso1020 closed 1 month ago

jrusso1020 commented 1 month ago

Bug Description

Bug description

When enabling react native session replays, we are noticing that only network requests from third party SDKs are being recorded. For example, requests to revenue cat or sentry which come from their own react native packages.

Network requests to our own backend are not being recorded, throughout the entire session. We use fetch for requests in our react native application.

here's a link to our screen recording for help debugging

Please describe. If this affects the front-end, screenshots would be of great help.

If you are on PostHog Cloud it would be really valuable if you can share any links where the problem occurs. This speeds up our ability to troubleshoot tremendously.

How to reproduce

  1. Enabled session replays on latest posthog react native version (version below)
  2. Checked the session replays in posthog cloud

Additional context

Fetch

We initialize our API client to use fetch like so

this.fetch = (...args: any[]) => window.fetch(...args);

And then make requests like so, similar for 'get' requests as well

the Content-Type will either be application/json or text/plain(used for sending base64 encoded protocol buffers)

response = await this.fetch(url, {
        method: 'post',
        body: body,
        headers: {
          'Content-Type': contentType,
          authorization: `Bearer ${this.accessToken}`,
          ...this.extraHeaders,
        },
      });

And example fetch request

{
  "method": "post",
  "body": "[REDACTED]",
  "headers": {
    "Content-Type": "application/json",
    "authorization": "Bearer [REDACTED]",
    "appId": "[REDACTED]",
    "appVersion": "2.12.0 2024/09/19"
  }
}

Posthog Initailization

We enable the posthog client separately and then pass this into the posthogProvider

export function initPostHog(config: EHAppConfig) {
  if (posthogInstance) {
    return posthogInstance;
  }

  if (!config.posthog.enabled || !config.posthog.apiKey) {
    return null;
  }

  posthogInstance = new PostHog(config.posthog.apiKey, {
    host: 'https://us.i.posthog.com',
    disableGeoip: true,
    // Comment out the line below to enable PostHog in dev
    disabled: __DEV__ || !config.posthog.enabled, // Disable in dev or if not enabled in config
    enableSessionReplay: true,
  });

  return posthogInstance;
}
<PostHogProvider client={posthog} autocapture>
...

Package versions

We don't technically use expo to build our app but including it as well

Testing on iOS 18 (built and running on an actual device, not a simulator)

Debug info

- [x] PostHog Cloud, Debug information:
Session: https://us.posthog.com/project/sTMFPsFhdP1Ssg/replay/01924347-1cab-7922-be47-db03acae31e6?t=4771
Admin: http://go/adminOrgUS/01910e4f-b970-0000-fa38-08ea1e2e4866 (project ID 82677)
Sentry: http://go/sentryUS/82677
marandaneto commented 1 month ago

Hello @jrusso1020 checking this out now.

marandaneto commented 1 month ago

Looks like https://github.com/PostHog/posthog-ios/blob/333ca7fa6d5072b1fa12f93226c2e8ea153f3b45/PostHog/Replay/URLSessionInterceptor.swift#L27 gets called correctly, but https://github.com/PostHog/posthog-ios/blob/333ca7fa6d5072b1fa12f93226c2e8ea153f3b45/PostHog/Replay/URLSessionInterceptor.swift#L59 not, checking why's that.

marandaneto commented 1 month ago

The reason is that RN uses the URLSession.shared.dataTask(with: X) instead of the methods using the method with completionHandler, so the instrumentation (swizzling) isn't picking it up correctly. Libraries that would use the URLSession.shared.dataTask(with: X, { completionHandler } would be captured correctly, but if libraries would also use the URLSession.shared.dataTask(with: X) it'd not work either. Checking how to fix this.

jrusso1020 commented 1 month ago

Thanks for the quick update and work here @marandaneto ! will stay up to date and happy to try out any fix you have

marandaneto commented 1 month ago

I've opened this issue for later refactoring https://github.com/PostHog/posthog-ios/issues/204 https://github.com/PostHog/posthog-ios/releases/tag/3.12.6 should be a quick fix for now. The RN session plugin uses the latest posthog-ios version, so if you update your Pods, you can test this (be sure its using the 3.12.6 version).

jrusso1020 commented 1 month ago

@marandaneto just tested it out with a local build and looks like it's working now for us thanks for the quick fix!