Skip to content

Setting Up the Analytics SDK

What you'll learn

  • Generating the Analytics SDK with Firebase Analytics implementation
  • Understanding the multi-vendor analytics abstraction
  • Logging events, screen views, and user properties
  • Swapping or combining analytics providers

Prerequisites

  • An existing Archipelago monorepo (see Monorepo Scaffolding)
  • Firebase configured in your project (for the default Firebase Analytics impl)

Step 1: Generate the Analytics SDK

bash
archipelago generate analytics_sdk

You will be prompted for:

  • appNameMyApp (must match your monorepo app name)
  • isForMonorepotrue

Or use a config file:

json
{
  "appName": "MyApp",
  "isForMonorepo": true
}
bash
archipelago generate analytics_sdk --config analytics_config.json

Step 2: Understand the Generated Structure

The Analytics SDK uses the API/Impl split for vendor-agnostic event tracking:

infrastructure/
├── analytics_sdk_api/
│   └── lib/src/
│       ├── analytics_client.dart          # Abstract analytics contract
│       ├── analytics_event.dart           # Event model
│       └── analytics_user_property.dart   # User property model
└── analytics_sdk_impl/
    └── lib/src/
        ├── firebase_analytics_client.dart # Firebase implementation
        ├── composite_analytics_client.dart # Fan-out to multiple providers
        └── di/
            └── analytics_module.dart      # DI registration

Step 3: Register the Analytics SDK

In your shell app's bootstrap.dart, register analytics during initialization:

dart
import 'package:analytics_sdk_impl/analytics_sdk_impl.dart';

// Single provider
getIt.registerSingleton<AnalyticsClient>(
  FirebaseAnalyticsClient(),
);

// Or fan-out to multiple providers
getIt.registerSingleton<AnalyticsClient>(
  CompositeAnalyticsClient([
    FirebaseAnalyticsClient(),
    // MixpanelAnalyticsClient(),
  ]),
);

Step 4: Track Events

Features depend on analytics_sdk_api only and log events through the abstraction:

dart
final analytics = getIt<AnalyticsClient>();

// Track a custom event
await analytics.logEvent(AnalyticsEvent(
  name: 'purchase_completed',
  parameters: {'amount': 99.99, 'currency': 'USD'},
));

// Track screen views
await analytics.logScreenView(screenName: 'ProductDetail');

// Set user properties
await analytics.setUserProperty(
  name: 'subscription_tier',
  value: 'pro',
);

Key Customization Points

CustomizationWhere to Change
Add Mixpanel/AmplitudeCreate new AnalyticsClient impl, add to composite
Filter events per providerCompositeAnalyticsClient — add provider-specific filtering
Add default parametersFirebaseAnalyticsClient — enrich events before sending
Disable in debug modeanalytics_module.dart — register noop impl for debug builds

Next Steps

Built by Banua Coder