Skip to content
Back to archipelago.banuacoder.com

Analytics Implementations

How to add Mixpanel to a Flutter app with analytics_sdk ​

Free Reference for mixpanel_analytics_impl: every variable, option and package it generates. Command: archipelago generate mixpanel_analytics_impl

Generate the Mixpanel vendor, set the project token in shared/app_config, and see how the deferred, consent-gated start works.

What you'll learn ​

  • Generating the Mixpanel vendor for analytics_sdk
  • Setting the project token in shared/app_config
  • Understanding the deferred, consent-gated start

Prerequisites ​

  • Analytics SDK generated in your monorepo
  • A Mixpanel account and its Project Token (Project Settings)

Generate analytics_sdk first. This brick does not check for it. The workspace fails to resolve without it.

Step 1: Generate the Brick ​

Skip this step if you chose mixpanel in analyticsVendors at project creation.

bash
archipelago generate mixpanel_analytics_impl

The prompts are:

  • appName: the name of your app
  • isForMonorepo: leave this as true

The brick creates infrastructure/mixpanel_analytics_impl/. The post-generation hook adds the package to the app pubspec.yaml and registers MixpanelAnalyticsImplPackageModule in lib/di/injector.dart.

Step 2: Add the Project Token ​

Add the token to the .env files in shared/app_config:

text
MIXPANEL_TOKEN=your-project-token

The vendor reads it as AppEnv.mixpanelToken. Use one token per flavor if you have separate Mixpanel projects. See shared_app_config.

Step 3: Understand the Deferred Start ​

Mixpanel.init is async and starts collecting automatic events when it finishes. To keep this behind consent, the vendor creates its Mixpanel instance inside initialize(). AnalyticsInitializer calls initialize() only after analytics consent is granted.

Until then the vendor ignores track, setUserId and the other calls. You do not sequence anything yourself. The vendor registers under AnalyticInjectorKey.mixpanelImpl, and the tracker finds it through AnalyticVendorRegistry.

Step 4: Verify Events Flow ​

Grant analytics consent, then track an event. Open the Mixpanel dashboard and look at the Events feed. Automatic events are on (trackAutomaticEvents: true), so session events show next to yours.

How the vendor behaves:

  • setUserId calls identify().
  • setUserProperty writes to the People profile with getPeople().set(key, value).
  • reset() clears the identity and starts an anonymous session. Call it on logout.
  • Revoking consent calls optOutTracking. Granting it again calls optInTracking.

Next Steps ​