Mixpanel Community Icon

Distinguishing TestFlight vs App Store Builds in MixPanel Integration

·
·

We are setting up MixPanel in an IOS app. We use Xcode Cloud to build and test candidate builds through TestFlight. How can I distinguish the app running through TestFlight (DEBUG) and when downloaded from the AppStore (Production) to direct data to two separate projects, when they are the same build?

  • Avatar of kapa.ai
    kapa.ai
    APP
    ·
    ·

    To distinguish between TestFlight (DEBUG) and App Store (Production) builds and direct data to separate Mixpanel projects, you can use a combination of configuration and conditional logic in your iOS app. Here's how you can approach this:1. First, set up two separate Mixpanel projects: one for development/testing and another for production. This is a recommended best practice as mentioned in the Mixpanel documentation.2. In your app's initialization code, you can use conditional logic to determine whether the app is running in a TestFlight environment or not. While there's no direct way to detect TestFlight, you can use a combination of compiler flags and runtime checks.3. Here's an example of how you might implement this in Swift:```import Mixpanelfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let devToken = "YOUR_DEV_PROJECT_TOKEN" let prodToken = "YOUR_PROD_PROJECT_TOKEN"

    #if DEBUG // This will be true for debug builds, including TestFlight Mixpanel.initialize(token: devToken) Mixpanel.mainInstance().loggingEnabled = true #else // This will be true for release builds from the App Store Mixpanel.initialize(token: prodToken) #endif return true

    }LBKSLBKSThis approach uses the `DEBUG` flag, which is typically set for debug builds (including TestFlight) but not for release builds. However, keep in mind that this method isn't foolproof, as TestFlight can also use release builds.LBKS4. For a more robust solution, you might want to combine this with a runtime check. For example:LBKSLBKSimport Mixpanelfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let devToken = "YOUR_DEV_PROJECT_TOKEN" let prodToken = "YOUR_PROD_PROJECT_TOKEN"

    let isTestFlight = Bundle.main.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt" if isTestFlight { Mixpanel.initialize(token: devToken) Mixpanel.mainInstance().loggingEnabled = true } else { Mixpanel.initialize(token: prodToken) } return true

    }```This method checks for the presence of a sandbox receipt, which is indicative of a TestFlight build.5. Remember to enable debug logging for your development builds as shown in the Mixpanel iOS SDK documentation.By implementing this approach, you can ensure that data from your TestFlight builds goes to your development Mixpanel project, while data from App Store downloads goes to your production Mixpanel project, even though they are the same build.Remember to thoroughly test this setup to ensure it's working as expected across different scenarios.

    kapa.ai is still learning and improving, please let me know how I did by reacting below.

    ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.