djangoflow_analytics
djangoflow_analytics
is a library for tracking analytics with multiple Analytics providers in Flutter applications. It is built on top of analytics package.
Installation
To use this library in your project, add the following dependency to your pubspec.yaml file:
dependencies:
djangoflow_analytics: <latest_version>
analytics: <latest_version>
Then run flutter pub get
to install it.
Usage
Initialization
Before sending any data, the library must be initialized by calling the init method on the DjangoflowAnalytics
class. It comes in handy if you want to disable sending analytics in specific cases like development
environment.
DjangoflowAnalytics.instance.init();
Adding Strategies
To handle the data that is sent, you can create a class by extending AnalyticStrategy
and add them to the DjangoflowAnalytics
class.
abstract class CustomAnalyticsAction implements AnalyticAction, HasKey, HasMapParams {}
class CustomLoginEvent extends CustomAnalyticsAction {
final String? method;
CustomLoginEvent({this.method});
@override
String get key => 'login';
@override
Map<String, Object?> get params => {
'method': method,
};
}
class CustomAnalyticStrategy extends AnalyticStrategy {
@override
bool canHandle(AnalyticAction action) => action is CustomAnalyticsAction;
@override
void performAction(AnalyticAction action) {
final customAction = action as CustomAnalyticsAction;
// Handle the action
}
}
final strategy = CustomAnalyticStrategy(); // could be firebase, facebook etc.
DjangoflowAnalytics.instance.addAllStrategies([strategy]);
Sending Data
To send data, you can create an instance of AnalyticAction and pass it to the performAction
method on the DjangoflowAnalytics class. It will internally look for suitable AnalyticStrategy
for the AnalyticsAction
and send the data.
DjangoflowAnalytics.instance.performAction(CustomLoginEvent(method:'email'));
Event Trimmer
It also provide a way to trim events by implementing EventTrimmer
interface which has 4 methods, trimName
, trimValue
, trimNullValueMapParams
and trimMapParams
.
User property trimmer
It also provide a way to trim user property by implementing UserPropertyTrimmer
interface which has 2 methods, trimName
and trimValue
.
Example
import 'package:djangoflow_analytics/djangoflow_analytics.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
DjangoflowAnalytics.instance.init();
final strategy = CustomAnalyticStrategy();
DjangoflowAnalytics.instance
.addAllStrategies([strategy]);
return MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
child: Text("Email Sign-in"),
onPressed: () {
DjangoflowAnalytics.instance
.performAction(CustomLoginEvent(method:'email'));
},
),
),
),
);
}
}
Note
This library is still under development and may be subject to breaking changes.
Contributions
We would love to have your contributions. Please feel free to open an issue or pull request.