analytics_hub 0.4.0 copy "analytics_hub: ^0.4.0" to clipboard
analytics_hub: ^0.4.0 copied to clipboard

A unified analytics abstraction for Dart/Flutter. Send events to multiple backends (Firebase, Mixpanel, etc.) through one API with a single LogEvent model and session support.

example/main.dart

// ignore_for_file: unreachable_from_main

import 'package:analytics_hub/analytics_hub.dart';

class EmptySessionDelegate implements HubSessionDelegate {
  @override
  Stream<Session?> get sessionStream => Stream.value(null);

  @override
  Future<Session?> getSession() async => null;
}

void main() async {
  final hub = AnalyticsHub(
    sessionDelegate: EmptySessionDelegate(),
    providers: [
      ExampleAnalyticsProvider(),
      ExampleAnalyticsProvider(name: 'Another Provider'),
    ],
  );

  await hub.initialize();

  await hub.sendEvent(const ExampleEvent(exampleProperty: 'example_value'));
}

class ExampleEvent extends Event {
  const ExampleEvent({required this.exampleProperty})
      : super('example_log_event');

  final String exampleProperty;

  @override
  Map<String, Object>? get properties => {'example_property': exampleProperty};

  @override
  EventContext get context => const EventContext()
      .withEntry(const _ExampleContextEntry('example-source'));

  @override
  List<EventProvider> get providers => [
        const EventProvider(ExampleAnalyticsProviderKey()),
        EventProvider(
          const ExampleAnalyticsProviderKey(name: 'Another Provider'),
          overrides: EventOverrides(
            name: 'example_log_event_overridden',
            properties: {
              'example_property_overridden': exampleProperty,
            },
          ),
        ),
      ];
}

class ExampleAnalyticsProvider extends AnalytycsProvider {
  ExampleAnalyticsProvider({String? name})
      : super(
          identifier: ExampleAnalyticsProviderKey(name: name),
          interceptors: const [],
        );

  @override
  ExampleEventResolver get resolver => const ExampleEventResolver();

  @override
  Future<void> initialize() async {
    print('ExampleAnalyticsProvider initialized');
  }

  @override
  Future<void> setSession(Session? session) async {
    print('ExampleAnalyticsProvider set session: $session');
  }
}

class ExampleAnalyticsProviderKey extends ProviderIdentifier {
  const ExampleAnalyticsProviderKey({super.name});
}

class ExampleEventResolver implements EventResolver {
  const ExampleEventResolver();

  @override
  Future<void> resolve(
    ResolvedEvent event, {
    required EventDispatchContext context,
  }) async {
    final source = event.context.entry<_ExampleContextEntry>()?.source;
    print(
      'ExampleEventResolver resolved log event: ${event.name}, '
      'source: $source, '
      'correlationId: ${context.correlationId}',
    );
  }
}

final class _ExampleContextEntry extends ContextEntry {
  const _ExampleContextEntry(this.source);

  final String source;
}
0
likes
160
points
468
downloads

Publisher

unverified uploader

Weekly Downloads

A unified analytics abstraction for Dart/Flutter. Send events to multiple backends (Firebase, Mixpanel, etc.) through one API with a single LogEvent model and session support.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

logging

More

Packages that depend on analytics_hub