Flutter数据分析插件zhugeio的使用
Flutter数据分析插件zhugeio的使用
诸葛io移动统计支持Flutter框架,以下是集成方法。
1. 在项目中添加安装插件
在Flutter项目的pubspec.yaml
文件中添加zhugeio
依赖包:
dependencies:
# zhugeio flutter plugin
zhugeio: ^1.0.2
执行以下命令安装插件:
flutter packages get
请参考:Flutter官网文档
2. Android端
2.1 添加诸葛分析所需相关权限
在AndroidManifest.xml
文件中添加以下权限:
<!-- 需要网络权限 -->
<uses-permission
android:name="android.permission.INTERNET"/>
<!-- 需要获取网络状态 -->
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"/>
<!-- 获取设备唯一标识 -->
<uses-permission
android:name="android.permission.READ_PHONE_STATE"/>
2.2 添加项目依赖
在build.gradle
文件中添加以下依赖:
dependencies {
implementation 'com.ZhugeioAnalytic:zhugeio:3.4.1'
}
2.3 初始化SDK
通过代码配置AppKey及Channel并启动;
若你希望通过代码传递appKey
及渠道信息,请使用ZhugeParam
实体类来定义信息,然后使用该参数初始化SDK:
import com.zhuge.analysis.stat.ZhugeParam;
import com.zhuge.analysis.stat.ZhugeSDK;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
ZhugeParam param = new ZhugeParam.Builder()
.appKey("appkey")
.appChannel("channel")
.build();
ZhugeSDK.getInstance().initWithParam(getApplicationContext(), param);
}
}
若是私有部署的用户,需要更改数据上传地址,请将上述onCreate()
里的代码更改为:
@Override
public void onCreate(){
super.onCreate();
...
// 设置上传地址,普通用户请勿更改。必须在调用init之前设置
ZhugeSDK.getInstance().setUploadURL("URL","backupUrl");
// ZhugeSDK初始化
ZhugeSDK.getInstance().init(this,"your AppKey","youAppChannel");
...
}
3. iOS端
3.1 项目目录下执行pod install
,依赖原始的SDK
进入ios
目录执行以下命令:
cd ios
pod install
3.2 用应用的Appkey启动诸葛io SDK
在AppDelegate.m
文件中添加以下代码:
#import "Zhuge.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[Zhuge sharedInstance] startWithAppKey:@"Your AppKey" launchOptions:launchOptions];
}
若是私有部署的用户,需要更改数据上传地址,请将上述代码更改为:
#import "Zhuge.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 设置上传地址,一般用户无需更改
[[Zhuge sharedInstance] setUploadURL:@"URL" andBackupUrl:@"BackUpURL"];
[[Zhuge sharedInstance] startWithAppKey:@"Your AppKey" launchOptions:launchOptions];
}
如果你需要修改SDK的默认设置,如设置版本渠道时,一定要在startWithAppKey
前执行;代码如下:
Zhuge *zhuge = [Zhuge sharedInstance];
// 实时调试开关
// 设置为YES,可在诸葛io的「实时调试」页面实时观察事件数据上传
// 建议仅在需要时打开,调试完成后,请及时关闭
[zhuge.config setDebug:YES];
// 自定义应用版本
[zhuge.config setAppVersion:@"0.9-beta"]; // 默认是info.plist中CFBundleShortVersionString值
// 自定义渠道
[zhuge.config setChannel:@"My App Store"]; // 默认是@"App Store"
// 开启行为追踪
[zhuge startWithAppKey:@"Your AppKey" launchOptions:launchOptions];
4. Flutter中使用插件
在具体dart文件中导入zhugeio.dart
:
import 'package:zhugeio/zhugeio.dart';
4.1 记录用户行为
在需要记录用户行为的部分,调用如下方法:
Zhugeio.track('事件名称',{'属性1':'值1','属性2':'值2'});
注意: 在添加事件属性时,需注意事件属性类型。如果事件属性类型为「数值型属性」,需要上传数据时修改数据类型为「数值型」,并且在诸葛io后台埋点管理中修改为「数值型」。
4.2 绑定用户信息
为了保持对用户的跟踪,你需要为他们记录一个识别码,可以使用手机号、email等唯一值来作为用户的识别码。另外,也可以在跟踪用户的时候,记录用户更多的属性信息,便于你更了解你的用户:
Zhugeio.identify('用户ID',{'用户属性':'用户值'});
4.3 统计事件时长
使用startTrack()
开始事件:
Zhugeio.startTrack('事件名称');
使用endTrack()
来结束具有相同事件名称的事件:
Zhugeio.endTrack('事件名称', {'属性1':'值1'});
startTrack()
与 endTrack()
需成对出现,单独使用没有效果。
4.4 开启实时调试
Zhugeio.openDebug();
4.5 采集收入数据
记录收入数据采集,需调用trackRevenue
函数,自动记录收入事件以及事件属性;price
(商品价格)、productID
(商品ID)、productQuantity
(商品数量)、revenueType
(收入类型)为收入事件内置属性,必传项。
具体使用方法如下:
Map product = {
"price": 229,
"productQuantity": 2,
"productID": "小米NFC手环",
"revenueType": "手环"
};
Zhugeio.trackRevenue(product);
完整示例代码
以下是一个完整的Flutter示例代码,展示了如何使用zhugeio
插件进行用户行为跟踪、绑定用户信息以及收入事件采集。
import 'package:flutter/material.dart';
import 'package:zhugeio/zhugeio.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
[@override](/user/override)
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final _formKey = GlobalKey<FormState>();
TextEditingController _userIdController = TextEditingController();
TextEditingController _eventController = TextEditingController();
TextEditingController _productPriceController = TextEditingController();
TextEditingController _productQuantityController = TextEditingController();
TextEditingController _productIdController = TextEditingController();
TextEditingController _revenueTypeController = TextEditingController();
void _identifyUser() {
Zhugeio.identify(_userIdController.text, {'name': 'John Doe'});
}
void _trackEvent() {
Zhugeio.track(_eventController.text, {'category': 'test', 'value': 'success'});
}
void _trackRevenue() {
Zhugeio.trackRevenue({
"price": double.parse(_productPriceController.text),
"productQuantity": int.parse(_productQuantityController.text),
"productID": _productIdController.text,
"revenueType": _revenueTypeController.text,
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Zhugeio Example')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
controller: _userIdController,
decoration: InputDecoration(labelText: 'User ID'),
),
ElevatedButton(
onPressed: _identifyUser,
child: Text('Identify User'),
),
TextFormField(
controller: _eventController,
decoration: InputDecoration(labelText: 'Event Name'),
),
ElevatedButton(
onPressed: _trackEvent,
child: Text('Track Event'),
),
TextFormField(
controller: _productPriceController,
decoration: InputDecoration(labelText: 'Product Price'),
),
TextFormField(
controller: _productQuantityController,
decoration: InputDecoration(labelText: 'Product Quantity'),
),
TextFormField(
controller: _productIdController,
decoration: InputDecoration(labelText: 'Product ID'),
),
TextFormField(
controller: _revenueTypeController,
decoration: InputDecoration(labelText: 'Revenue Type'),
),
ElevatedButton(
onPressed: _trackRevenue,
child: Text('Track Revenue'),
),
],
),
),
),
);
}
}
更多关于Flutter数据分析插件zhugeio的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter数据分析插件zhugeio的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中使用zhugeio
插件进行数据分析,可以帮助你收集用户行为数据,以便更好地优化产品和用户体验。zhugeio
是一个国内常用的数据分析工具,支持多种平台,包括 Flutter。
以下是如何在 Flutter 项目中使用 zhugeio
插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 zhugeio
插件的依赖。
dependencies:
flutter:
sdk: flutter
zhugeio: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化 zhugeio
在你的 Flutter 项目中,通常是在 main.dart
文件中初始化 zhugeio
。
import 'package:flutter/material.dart';
import 'package:zhugeio/zhugeio.dart';
void main() {
runApp(MyApp());
// 初始化 Zhugeio
Zhugeio.init(
appKey: 'YOUR_APP_KEY', // 替换为你的 Zhugeio App Key
channel: 'App Store', // 渠道名称,如 App Store、Google Play 等
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
3. 发送事件
你可以在应用中的任何地方发送事件,以记录用户的行为。
import 'package:flutter/material.dart';
import 'package:zhugeio/zhugeio.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Zhugeio Demo'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// 发送自定义事件
Zhugeio.track('button_clicked', properties: {
'button_name': 'demo_button',
'click_time': DateTime.now().toString(),
});
},
child: Text('Click Me'),
),
),
);
}
}
4. 用户标识
你可以通过 Zhugeio.identify
方法来标识用户,以便在分析中关联用户的行为。
Zhugeio.identify('USER_ID', properties: {
'name': 'John Doe',
'email': 'john.doe@example.com',
});
5. 其他功能
zhugeio
还提供了其他功能,如设置用户属性、记录页面浏览等。
-
设置用户属性:
Zhugeio.setUserInfo({ 'age': 25, 'gender': 'male', });
-
记录页面浏览:
Zhugeio.trackPageView('HomePage');
6. 调试模式
在开发阶段,你可以启用调试模式,以便在控制台中查看日志。
Zhugeio.setDebug(true);