dlink_analytics 1.1.3
dlink_analytics: ^1.1.3 copied to clipboard
Analytics Flutter Plugin
example/lib/main.dart
import 'dart:math';
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:dlink_analytics/event.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
_setupAnalytics();
}
Future<void> _setupAnalytics() async {
AnalyticsConfig config = AnalyticsConfigBuilder()
.setAccountId("YOUR_ACCOUNT_ID")
.setDevToken("YOUR_DEV_TOKEN")
//[require] Developers need to pass in the crypt 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", "count": 1, "value": 1.2, "register": false});
var oldUserId = await AnalyticsCore.instance.getCustomParam("user_id");
var oldCount = await AnalyticsCore.instance.getCustomParam("count");
var oldValue = await AnalyticsCore.instance.getCustomParam("value");
var oldRegister = await AnalyticsCore.instance.getCustomParam("register");
print(
"oldUserId $oldUserId oldCount $oldCount oldValue $oldValue oldRegister $oldRegister");
AnalyticsCore.instance.updateCustomParam("user_id", "New_UID");
AnalyticsCore.instance.updateCustomParam("count", 2);
AnalyticsCore.instance.updateCustomParam("value", 1.3);
AnalyticsCore.instance.updateCustomParam("register", true);
var newUserId = await AnalyticsCore.instance.getCustomParam("user_id");
var newCount = await AnalyticsCore.instance.getCustomParam("count");
var newValue = await AnalyticsCore.instance.getCustomParam("value");
var newRegister = await AnalyticsCore.instance.getCustomParam("register");
print("newUserId $newUserId newCount $newCount newValue $newValue newRegister $newRegister");
//[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());
//[optional] Developers can update the CryptKey through this method
AnalyticsCore.instance.updateCryptKey("NEW_DYNAMIC_CRYPT_KEY");
}
//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;
}
print("CustomDataSender result ${result}");
//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'),
),
),
],
),
),
),
);
}
}