dlink_analytics 1.0.0
dlink_analytics: ^1.0.0 copied to clipboard
Analytics Flutter Plugin
example/lib/main.dart
import 'dart:math';
import 'package:dlink_analytics/Event.dart';
import 'package:dlink_analytics/analytics_config.dart';
import 'package:dlink_analytics/analytics_config_builder.dart';
import 'package:dlink_analytics/analytics_core.dart';
import 'package:dlink_analytics/data_package.dart';
import 'package:dlink_analytics/data_sender.dart';
import 'package:dlink_analytics/data_service.dart';
import 'package:dlink_analytics/default_data_service.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
_setupAnalytics();
}
var _setup = false;
void _setupAnalytics() {
if (_setup) return;
_setup = true;
AnalyticsConfig config = AnalyticsConfigBuilder()
.setAccountId("YOUR_ACCOUNT_ID")
.setDevToken("YOUR_DEV_TOKEN")
//[require] Developers need to pass in the key for event data.
//It is strongly recommended that you set different keys for different users.
.setCryptKey("DYNAMIC_CRYPT_KEY")
//[optional] The minimum interval for reporting event data, in seconds.Default 10 seconds.
.setMinReportInterval(10)
//[optional] The maximum number of event data reported each time.Default 50.
.setMaxReportNumEachTime(50)
.build();
//[require] Call this method to initialize the SDK
AnalyticsCore.instance.setup(config);
//[optional] Developers can add custom parameters globally
AnalyticsCore.instance.addCustomParams({"user_id": "UID"});
//[require] If you use the default DataService in the SDK, please pass "default" for serviceName.
//Developers customize IDataSender to report event data to the business server
AnalyticsCore.instance
.register("default", DefaultDataService(CustomDataSender()));
//[optional]If you want to customize the DataService, please pass in your custom name for DataService, do not use "default"
AnalyticsCore.instance
.register("YOUR_CUSTOM_DATA_SERVICE_NAME", FirebaseDataService());
}
//Customize DataService to report the event data to Firebase
class FirebaseDataService extends DataService {
@override
void handle(Event event) {
//Please call the Firebase api to log events here
print("FirebaseDataService handle ${event.eventName}");
}
@override
void flush() {}
}
//Send the DataPackage to your own server
class CustomDataSender extends DataSender {
@override
void send(DataPackage dataPackage, OnFinished finished) {
//bool result = api.sendToYourServer(dataPackage)
var num = Random().nextInt(10) % 3;
var result = true;
if (num == 0) {
result = false;
}
//Please pass the report result to SDK after the report is completed.
finished.call(result);
}
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Analytics Flutter Plugin'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: MediaQuery.of(context).size.width * 3 / 4,
child: ElevatedButton(
onPressed: () {
AnalyticsCore.instance.log("click_button_1",
eventParams: {"button_name": "button1"});
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(
horizontal: 32, vertical: 16),
),
child: const Text('Generate Normal Event'),
),
),
const SizedBox(height: 16),
SizedBox(
width: MediaQuery.of(context).size.width * 3 / 4,
child: ElevatedButton(
onPressed: () {
//[Optional] Set the event priority to Event.PRIORITY_HIGH, and the event will be reported immediately.
AnalyticsCore.instance.log("click_button_2",
eventParams: {"button_name": "button2"},
priority: Event.priorityHigh);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(
horizontal: 32, vertical: 16),
),
child: const Text('Generate High Priority Event'),
),
),
const SizedBox(height: 16),
SizedBox(
width: MediaQuery.of(context).size.width * 3 / 4,
child: ElevatedButton(
onPressed: () {
//[Optional] Developers can call this method to report event data when the page is destroyed or at other times.
// By default, the SDK will try to report events when the application returns to the foreground or background.
AnalyticsCore.instance.flush();
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(
horizontal: 32, vertical: 16),
),
child: const Text('Manual Flush'),
),
),
],
),
),
),
);
}
}