flutter-femtolytics

A Flutter client for femtolytics.

Background

Femtolytics is a lightweight open-source mobile application analytics solution.

You can either use the hosted solution at femtolytics or host your own instance of femtolytics by using django-femtolytics

This plugin is a pure Dart plugin and has minimal dependencies.

Getting Started

You should ensure that you add the dependency to your flutter project.

dependencies:
 femtolytics: "^1.0.0"

You can also reference the git repo directly if you want:

dependencies:
 femtolyics:
   git: git://github.com/femtolytics/flutter-femtolyics.git

Setting up

As early as possible in your application you should provide the URL of the endpoint for your femtolytics instance, a good place is either in your main or the constructor of your application as follows:

class MyApp extends StatelessWidget {
    MyApp() {
        Femtolytics.setEndpoint('https://example.com/analytics');
    }
}

By default the SDK will not track events in a Simulator or in a Debug Build. You can change that behavior by passing flags to the setEndpoint() call.

class MyApp extends StatelessWidget {
    MyApp() {
        Femtolytics.setEndpoint(
          'https://femtolytics.com/api/v1',
          enableOnSimulator: true,
          enableOnDebugBuild: true,
        );
    }
}

The plugin queues in the event in a local database no matter, and will not lose events even if one were to happen before the endpoint is defined.

Tracking views

flutter-femtolytics comes with mixins to help you track how your user are navigating through your application.

Simply replace StatelessWidget by TraceableStatelessWidget, StatefulWidget by TraceableStatefulWidget and finally InheritedWidget by TraceableInheritedWidget.

The constructor of those mixins take a name argument so you can customize the name of the view. If not specified, then the class name (this.runtimeType.toString()) will be used.

Here is an example of how you can pass the name to the mixins:

class HomePage extends TraceableStatefulWidget {
  HomePage({Key key}) : super(key: key, name: "Home");

  @override
  State<StatefulWidget> createState() => HomeState();
}

Tracking actions and goals

Other than tracking basic flows within the application, you can record custom actions and goals.

To record an action simply call Femtolytics.action("my action") and similarly to track goals call Femtolytics.goal("purchase").

Both of those calls also take a dictionary of arbitrary values so that you can track custom properties with both goals and actions.

For example

Femtolytics.action('SelectColorClicked', properties: {
    'color': 'blue',
});
Femtolytics.goal('in_app', properties: {
    'product': 'loot10',
    'price': 10,
});

Recording crashes and exceptions

Femtolytics can track crashes for you as long as you call the Femtolytics.crash at the right moment. The following sample code shows how to modify your main.dart to catch Dart exceptions as well as Flutter exceptions.

import 'dart:async';

import 'package:flutter/femtolytics.dart';
import 'package:flutter/foundation.dart' as Foundation;
import 'package:flutter/material.dart';

void main() {
  FlutterError.onError = (FlutterErrorDetails details) async {
    if (Foundation.kReleaseMode) {
      Zone.current.handleUncaughtError(details.exception, details.stack);
    } else {
      FlutterError.dumpErrorToConsole(details);
    }
  };

  runZonedGuarded<Future<Null>>(() async {
    runApp(App());
  }, (Object error, StackTrace stackTrace) {
    Femtolytics.crash(error, stackTrace);
  });
}

Opting Out

If you want to offer a way for the user to opt-out of analytics, you can use the setOptOut() call.

Femtolytics.setOptOut(true);

Libraries

femtolytics