mixpanel_analytics 1.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

Mixpanel Analytics #

Build Status License: MIT Pub

A dart wrapper on the mixpanel REST API to be used in Flutter applications. As this is using the http REST API it works both with Android and iOS.

How to use it #

There is an example app that demonstrates how to use the plugin,

You just need to instantiate the class and you'll be ready to send track or engage events.

Make sure to check mixpanel documentation for the REST API to know the options available.

MixpanelAnalytics(
  token: 'XXXXXXXXX',
  userId$: _user$.stream,
  verbose: true,
  shouldAnonymize: true,
  shaFn: (value) => value,
  onError: (e) => setState(() {
    _error = e;
    _success = null;
  }),
);

There are two different constructors, the regular one which will send events on the fly and the batch mode which will group the requests (by type) and send them every X seconds (configurable).

MixpanelAnalytics.batch(
  token: 'XXXXXXXXX',
  userId$: _user$.stream,
  uploadInterval: Duration(seconds: 30),
  shouldAnonymize: true,
  shaFn: (value) => value,
  verbose: true,
  onError: (e) => setState(() {
    _error = e;
    _success = null;
  }),
);

Not supported yet #

Check the documentation for more information on the features not yet supported https://developer.mixpanel.com/docs/http

Event Request Parameters #

In addition to the data parameter, https://api.mixpanel.com/track supports a number of optional parameters. For the most part, these optional parameters are useful only in special situations.

Parameters
ip
redirect
img
callback

Update Request Parameters #

In addition to the data parameter, https://api.mixpanel.com/engage supports a number of optional parameters. For the most part, these optional parameters are useful only in special situations.

Parameters
redirect
callback

Invalid requests #

When sending batch requests, if one of them is invalid the whole batch operation will fail and the batch will be saved to retry the next iteration. This could become a problem as the whole batch could be stored and retried over and over. A strategy for this would be either to send individually the requests and ditch the only invalid one or just dith the whole lot (TBD).

[1.2.1] - 2019-11-07 #

  • Fix an error when sending batch requests for engage.

[1.2.0] - 2019-09-10 #

  • Allow encoding function to be passed.

[1.1.0] - 2019-09-10 #

  • Add option to encode sensitive information to mixpanel.

[1.0.1] - 2019-08-05 #

  • Add some unit tests.

[1.0.0] - 2019-08-02 #

  • Initial release.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:mixpanel_analytics/mixpanel_analytics.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _user$ = StreamController<String>.broadcast();

  MixpanelAnalytics _mixpanel;
  MixpanelAnalytics _mixpanelBatch;

  String _error;
  String _success;

  int _levelNumber = 0;

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

    _mixpanel = MixpanelAnalytics(
      token: 'XXXX',
      userId$: _user$.stream,
      verbose: true,
      shouldAnonymize: true,
      shaFn: (value) => value,
      onError: (e) => setState(() {
        _error = e;
        _success = null;
      }),
    );

    _mixpanelBatch = MixpanelAnalytics.batch(
      token: 'XXXX',
      userId$: _user$.stream,
      uploadInterval: Duration(seconds: 30),
      shouldAnonymize: true,
      shaFn: (value) => value,
      verbose: true,
      onError: (e) => setState(() {
        _error = e;
        _success = null;
      }),
    );

    _user$.add('111112');
  }

  @override
  void dispose() {
    _user$.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RaisedButton(
              child: Text('Send track event'),
              onPressed: () async {
                var result = await _mixpanel
                    .track(event: 'testEvent', properties: {'prop1': 'value1'});
                if (result) {
                  setState(() {
                    _success = 'Success!';
                    _error = '';
                  });
                }
              },
            ),
            RaisedButton(
              child: Text('Send engage event'),
              onPressed: () async {
                var result = await _mixpanel
                    .engage(operation: MixpanelUpdateOperations.$set, value: {
                  'Level Number': _levelNumber,
                });
                _levelNumber++;
                if (result) {
                  setState(() {
                    _success = 'Success!';
                    _error = '';
                  });
                }
              },
            ),
            RaisedButton(
              child: Text('Send track event in batch'),
              onPressed: () async {
                var result = await _mixpanelBatch
                    .track(event: 'testEvent', properties: {'prop1': 'value1'});
                if (result) {
                  setState(() {
                    _success = 'Success!';
                    _error = '';
                  });
                }
              },
            ),
            RaisedButton(
              child: Text('Send engage event in batch'),
              onPressed: () async {
                var result = await _mixpanelBatch
                    .engage(operation: MixpanelUpdateOperations.$set, value: {
                  'Level Number': _levelNumber,
                });
                _levelNumber++;
                if (result) {
                  setState(() {
                    _success = 'Success!';
                    _error = '';
                  });
                }
              },
            ),
            _error != null ? Text(_error) : Container(),
            _success != null ? Text(_success) : Container(),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  mixpanel_analytics: ^1.2.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:mixpanel_analytics/mixpanel_analytics.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
80
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

We analyzed this package on Mar 31, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
flutter 0.0.0
http ^0.12.0+2 0.12.0+4
shared_preferences ^0.5.3+1 0.5.6+3
Transitive dependencies
async 2.4.1
charcode 1.1.3
collection 1.14.11 1.14.12
flutter_web_plugins 0.0.0
http_parser 3.1.4
meta 1.1.8
path 1.6.4
pedantic 1.9.0
shared_preferences_macos 0.0.1+6
shared_preferences_platform_interface 1.0.3
shared_preferences_web 0.1.2+4
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
mockito ^4.1.0