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

feat: support multiple PostHog instances part II #309

Merged

Conversation

ioannisj
Copy link
Contributor

@ioannisj ioannisj commented Feb 24, 2025

💡 Motivation and Context

Second part of supporting multiple instances

Todo:

  • Now storage is per api key, with some old storage migration logic so we don't miss any events. Old events will be migrated to the first API used
  • Maintain one instance per api key. So, calling .with() with the same API key should return the same PostHogSDK instance
  • Replay and element autocapture integrations installed only on first created instance
  • captureApplicationLifecycleEvents (autocapture) only on first created instance
  • captureScreenViews only on first created instance
  • Decouple URLSessionInterceptor from PostHogSDK.shared
  • Decouple UIApplication.sendEvent from PostHogSDK.shared
  • Decouple SwiftUI postHogScreenView from PostHogSDK.shared
  • Decouple URLSession extensions (replay network logs) from PostHogSDK.shared

#skip-changelog

💚 How did you test it?

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

@ioannisj ioannisj changed the title feat: support multiple PostHog instances ensuring one instance per key feat: support multiple PostHog instances part II Feb 24, 2025
Comment on lines 46 to 49
// needs to happen on next runloop cause we are still in SDK init sequence here
DispatchQueue.main.async {
self.captureAppInstallOrUpdated()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to avoid this we can do this:
https://github.com/PostHog/posthog-android/blob/41ac8e9dbf989348823052c8c0f14c9c0d1fa2fd/posthog/src/main/java/com/posthog/PostHog.kt#L125-L131
we create the instances, and "install" them once the SDK is operational at the end of the setup method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored to add PostHogIntegration. To be used only internally but should help as we continue adding integrations

@marandaneto
Copy link
Member

@ioannisj for URLSessionExtension you can just add an optional param to the methods, with a posthog instance, and use the shared instance if the param is null/not given
if the extension can be used in objc, we need a new overload, though

@marandaneto
Copy link
Member

@ioannisj for URLSessionExtension you can just add an optional param to the methods, with a posthog instance, and use the shared instance if the param is null/not given if the extension can be used in objc, we need a new overload, though

same for postHogScreenView extension

…tances-part-2

# Conflicts:
#	PostHog.xcodeproj/project.pbxproj
#	PostHog/Autocapture/PostHogAutocaptureIntegration.swift
#	PostHog/PostHogSDK.swift
#	PostHog/Replay/PostHogReplayIntegration.swift
#	PostHogTests/PostHogIntegrationInstallationTest.swift
Copy link
Member

@marandaneto marandaneto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a question otherwise LGTM

@ioannisj ioannisj merged commit c6bd7e7 into feat/multiple-sdk-instances Feb 26, 2025
6 checks passed
@ioannisj ioannisj deleted the feat/multiple-sdk-instances-part-2 branch February 26, 2025 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants