Skip to main content

Installation

1

Add dependency

Add to pubspec.yaml:
dependencies:
  mixpanel_flutter: ^1.x.x
2

Install packages

flutter pub get
3

Initialize Mixpanel

import 'package:mixpanel_flutter/mixpanel_flutter.dart';

class _YourClassState extends State<YourClass> {
  Mixpanel mixpanel;

  @override
  void initState() {
    super.initState();
    initMixpanel();
  }

  Future<void> initMixpanel() async {
    mixpanel = await Mixpanel.init(
      "YOUR_PROJECT_TOKEN",
      trackAutomaticEvents: false
    );
  }
}

Flutter Web Support

Add to web/index.html inside <head>:
<script src="./assets/packages/mixpanel_flutter/assets/mixpanel.js"></script>

Track Events

mixpanel.track('Button Clicked', {
  'button_name': 'Sign Up',
  'screen': 'Landing Page'
});

Timing Events

mixpanel.timeEvent('Image Upload');

// 20 seconds later
mixpanel.track('Image Upload');

Flush Events

mixpanel.flush();

Identify Users

mixpanel.track('sign in');
mixpanel.identify('12345');

Reset on Logout

mixpanel.track('log out');
mixpanel.reset();

User Profiles

mixpanel.identify('12345');
mixpanel.getPeople().set('Plan', 'Premium');

// Set multiple properties
mixpanel.getPeople().set({
  'name': 'John',
  'email': '[email protected]'
});
mixpanel.getPeople().setOnce('name', 'John');
mixpanel.getPeople().setOnce('location', 'US');

Super Properties

mixpanel.registerSuperProperties({
  'app_version': '2.0.1',
  'platform': 'Flutter'
});

mixpanel.registerSuperPropertiesOnce({
  'first_launch': DateTime.now().toIso8601String()
});

Group Analytics

// Assign to group
mixpanel.setGroup('company', 'Acme Inc');

// Track event
mixpanel.track('feature_used');

// Set group properties
mixpanel.getGroup('company', 'Acme Inc').set('industry', 'Technology');

Privacy Controls

Opt Out

mixpanel.optOutTracking();

// Initialize with opt-out
mixpanel = await Mixpanel.init(
  "YOUR_PROJECT_TOKEN",
  trackAutomaticEvents: false,
  optOutTrackingDefault: true
);

EU/India Data Residency

// EU
mixpanel.setServerURL('https://api-eu.mixpanel.com');

// India
mixpanel.setServerURL('https://api-in.mixpanel.com');

Disable Geolocation

mixpanel.setUseIpAddressForGeolocation(false);

Debug Mode

mixpanel.setLoggingEnabled(true);

Platform Considerations

  • Works on iOS, Android, and Web
  • Events flush every 60 seconds
  • Supports hot reload during development
  • Legacy auto-track events available but not recommended
  • Web requires additional script tag

Resources

Build docs developers (and LLMs) love