FlexTrack 🎯

A powerful and flexible analytics tracking system for Flutter that provides intelligent event routing, GDPR compliance, and seamless multi-platform support.

✨ Features

  • 🎯 Intelligent Routing: Route events to different analytics services based on configurable rules
  • πŸ”’ GDPR Compliant: Built-in consent management and privacy controls
  • πŸ“Š Multi-Platform: Works seamlessly across iOS, Android, Web, and Desktop
  • ⚑ Performance Optimized: Smart sampling, batching, and performance presets
  • πŸ§ͺ Developer Friendly: Comprehensive debugging tools and console tracker
  • πŸ”§ Highly Configurable: Extensive customization options for any use case

πŸš€ Quick Start

Installation

Add to your pubspec.yaml:

dependencies:
  flex_track: ^0.1.0

Basic Setup

import 'package:flex_track/flex_track.dart';

void main() async {
  // Quick setup with smart defaults
  await FlexTrack.setup([
    ConsoleTracker(), // For development
    YourAnalyticsTracker(), // Your production tracker
  ]);

  runApp(MyApp());
}

// Track events anywhere in your app
await FlexTrack.track(CustomEvent(
  name: 'user_signup',
  properties: {'method': 'email'},
));

Advanced Routing

await FlexTrack.setupWithRouting([
  ConsoleTracker(),
  FirebaseTracker(),
  MixpanelTracker(),
], (routing) => routing
  // Debug events only to console in development
  .routeMatching(RegExp(r'debug_.*'))
  .toDevelopment()
  .onlyInDebug()
  .and()
  
  // Sensitive data requires PII consent
  .routeCategory(EventCategory.sensitive)
  .toAll()
  .requirePIIConsent()
  .and()
  
  // High volume events get sampled
  .routeHighVolume()
  .toAll()
  .lightSampling()
  .and()
  
  // Default: everything to all trackers
  .routeDefault()
  .toAll()
);

πŸ“š Documentation

Core Concepts

  • Events: Data points you want to track
  • Trackers: Analytics services (Firebase, Mixpanel, etc.)
  • Routing Rules: Logic that determines which events go to which trackers
  • Groups: Collections of trackers for easier management

Event Types

Create custom events by implementing BaseEvent:

class PurchaseEvent extends BaseEvent {
  final String productId;
  final double amount;
  final String currency;

  PurchaseEvent({
    required this.productId,
    required this.amount,
    required this.currency,
  });

  @override
  String getName() => 'purchase';

  @override
  Map<String, Object> getProperties() => {
    'product_id': productId,
    'amount': amount,
    'currency': currency,
  };
  
  @override
  EventCategory get category => EventCategory.business;
  
  @override
  bool get containsPII => false;
}

Custom Trackers

Implement your own tracker by extending BaseTrackerStrategy:

class MyAnalyticsTracker extends BaseTrackerStrategy {
  MyAnalyticsTracker() : super(
    id: 'my_analytics',
    name: 'My Analytics Service',
  );

  @override
  Future<void> doInitialize() async {
    // Initialize your analytics SDK
  }

  @override
  Future<void> doTrack(BaseEvent event) async {
    // Send event to your analytics service
    await myAnalytics.track(
      event.getName(),
      event.getProperties(),
    );
  }
}

πŸ”§ Configuration Examples

Performance-Focused Setup

await FlexTrack.setupWithRouting([
  ConsoleTracker(),
  ProductionTracker(),
], (builder) => builder
  .applyPerformanceFocused() // Applies aggressive sampling
);

Privacy-Focused Setup

await FlexTrack.setupWithRouting([
  GDPRCompliantTracker(),
], (builder) => builder
  .applyPrivacyFocused() // Strict consent requirements
);

Development Setup

await setupFlexTrackForDevelopment(); // Convenience method

πŸ›‘οΈ GDPR Compliance

FlexTrack makes GDPR compliance straightforward:

// Set consent status
FlexTrack.setConsent(general: true, pii: false);

// Events requiring PII consent won't be tracked
await FlexTrack.track(SensitiveEvent()); // Blocked without PII consent

// Essential events always go through
await FlexTrack.track(EssentialEvent());

πŸ› Debugging

Debug Events

// See exactly how events are routed
final debugInfo = FlexTrack.debugEvent(myEvent);
print(debugInfo.routingResult);

// Get comprehensive system info
FlexTrack.printDebugInfo();

Console Tracker

Perfect for development and debugging:

ConsoleTracker(
  showProperties: true,
  showTimestamps: true,
  colorOutput: true,
)

πŸ§ͺ Testing

FlexTrack includes testing utilities:

testWidgets('analytics test', (tester) async {
  final mockTracker = await setupFlexTrackForTesting();
  
  // Your test code
  await FlexTrack.track(TestEvent());
  
  // Verify events were tracked
  expect(mockTracker.capturedEvents, hasLength(1));
  expect(mockTracker.capturedEvents.first.getName(), 'test_event');
});

πŸ—οΈ Architecture

FlexTrack follows a clean, modular architecture:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   FlexTrack     │───▢│  RoutingEngine   │───▢│   Trackers      β”‚
β”‚   (Main API)    β”‚    β”‚  (Rule Matching) β”‚    β”‚  (Analytics)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ EventProcessor  β”‚    β”‚ RoutingBuilder   β”‚    β”‚ TrackerRegistry β”‚
β”‚ (Processing)    β”‚    β”‚ (Configuration)  β”‚    β”‚ (Management)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Inspired by the need for flexible analytics in Flutter apps
  • Built with GDPR compliance and developer experience in mind
  • Thanks to the Flutter community for feedback and suggestions

Libraries

flex_track
FlexTrack - A flexible analytics tracking system for Flutter