Showing
4 changed files
with
152 additions
and
12 deletions
1 | BSD 3-Clause License | 1 | BSD 3-Clause License |
2 | 2 | ||
3 | -Copyright (c) 2024, Dubhe | 3 | +Copyright (c) 2024, epoll.dev |
4 | 4 | ||
5 | Redistribution and use in source and binary forms, with or without | 5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the following conditions are met: | 6 | modification, are permitted provided that the following conditions are met: |
1 | -# auto_track | 1 | +# Auto_Track_Plugin |
2 | 2 | ||
3 | -Auto Track Plugin | 3 | +> Flutter 自动埋点插件,支持 Android 和 iOS |
4 | 4 | ||
5 | -## Getting Started | 5 | +低侵入全局自动埋点,自动记录页面进入、退出,点击、滑动等事件,并支持自定义事件。 |
6 | 6 | ||
7 | -This project is a starting point for a Flutter | ||
8 | -[plug-in package](https://flutter.dev/developing-packages/), | ||
9 | -a specialized package that includes platform-specific implementation code for | ||
10 | -Android and/or iOS. | ||
11 | 7 | ||
12 | -For help getting started with Flutter development, view the | ||
13 | -[online documentation](https://flutter.dev/docs), which offers tutorials, | ||
14 | -samples, guidance on mobile development, and a full API reference. | 8 | +## Getting Started 使用指南 |
15 | 9 | ||
10 | +目前仅在移动端验证通过,其他平台暂无验证。 | ||
11 | + | ||
12 | +### Installation 安装 | ||
13 | + | ||
14 | +```dart | ||
15 | +flutter pub add auto_track | ||
16 | +``` | ||
17 | + | ||
18 | +### Usage example 使用示例 | ||
19 | + | ||
20 | +可直接运行项目中的 example | ||
21 | + | ||
22 | +#### 主要配置 | ||
23 | + | ||
24 | +```dart | ||
25 | +void main() { | ||
26 | + runApp(const MyApp()); | ||
27 | +} | ||
28 | + | ||
29 | +class MyApp extends StatefulWidget { | ||
30 | + const MyApp({super.key}); | ||
31 | + | ||
32 | + @override | ||
33 | + State<MyApp> createState() => _MyAppState(); | ||
34 | +} | ||
35 | + | ||
36 | +class _MyAppState extends State<MyApp> { | ||
37 | + @override | ||
38 | + void initState() { | ||
39 | + AutoTrack() | ||
40 | + .config(AutoTrackConfig( // 其余配置可查看AutoTrackConfig类 | ||
41 | + host: 'http://localhost:3000/api/track', | ||
42 | + appKey: 'xxxx', | ||
43 | + appSecret: 'xxxx', | ||
44 | + pageConfigs: [ | ||
45 | + AutoTrackPageConfig<PageA>( | ||
46 | + pageID: 'page_a', // 配置页面ID,统计时可基于此ID进行统计 | ||
47 | + ), | ||
48 | + AutoTrackPageConfig<CupertinoScaffold>( | ||
49 | + pageID: 'home_tab_page', | ||
50 | + isPageWidget: (page) { // 页面匹配是基于泛型匹配,如果是被其他widget包裹的,需要自行判断 | ||
51 | + if (page.key != null) { | ||
52 | + if (page.key is ValueKey) { | ||
53 | + return (page.key! as ValueKey).value == 'home_tab_page'; | ||
54 | + } | ||
55 | + } | ||
56 | + return false; | ||
57 | + } | ||
58 | + ) | ||
59 | + ])) | ||
60 | + .enable() | ||
61 | + .enableUpload() // 启用数据上传,需设置host | ||
62 | + .enablePageLeave() // 启用页面离开统计 | ||
63 | + .enablePageView() // 启用页面进入统计 | ||
64 | + .enableClick() // 启用点击统计 | ||
65 | + .enableDrag() // 启用滑动统计 | ||
66 | + .enableIgnoreNullKey() // 忽略空的key,如果不忽略,没有配置key的页面或事件会基于一定的规则生成一个随机的key进行上报统计 | ||
67 | + .enableLog(); // 启用日志,建议在debug模式下开启,会打印一些埋点相关的日志 | ||
68 | + | ||
69 | + super.initState(); | ||
70 | + } | ||
71 | + | ||
72 | + @override | ||
73 | + Widget build(BuildContext context) { | ||
74 | + return MaterialApp( | ||
75 | + home: Scaffold( | ||
76 | + appBar: AppBar( | ||
77 | + title: const Text('auto track example app'), | ||
78 | + ), | ||
79 | + body: const Center( | ||
80 | + child: Home(), | ||
81 | + ), | ||
82 | + ), | ||
83 | + navigatorObservers: AutoTrackNavigationObserver.wrap([]), // 需要使用AutoTrackNavigationObserver.wrap去包裹当前使用的navigatorObservers | ||
84 | + ); | ||
85 | + } | ||
86 | +} | ||
87 | + | ||
88 | +``` | ||
89 | + | ||
90 | +#### 具体使用 | ||
91 | +```dart | ||
92 | +import 'package:flutter/cupertino.dart'; | ||
93 | + | ||
94 | +class PageA extends StatelessWidget { | ||
95 | + const PageA({super.key}); | ||
96 | + | ||
97 | + @override | ||
98 | + Widget build(BuildContext context) { | ||
99 | + return Container( | ||
100 | + margin: const EdgeInsets.only(top: 200), | ||
101 | + child: Column( | ||
102 | + children: [ | ||
103 | + GestureDetector( // 如果启用了enableIgnoreNullKey,这里没有配置key的点击事件不会进行记录统计 | ||
104 | + onTap: () { | ||
105 | + print("tap page a null key"); | ||
106 | + }, | ||
107 | + child: const Text('null key'), | ||
108 | + ), | ||
109 | + GestureDetector( | ||
110 | + key: const Key('page-a-click'), | ||
111 | + onTap: () { | ||
112 | + print("tap page a"); | ||
113 | + Track.instance.customEvent('custom_event', | ||
114 | + {'other_param': 'param'}); // 自定义事件发送 | ||
115 | + }, | ||
116 | + child: const Text('have key'), | ||
117 | + ) | ||
118 | + ], | ||
119 | + ), | ||
120 | + ); | ||
121 | + } | ||
122 | +} | ||
123 | + | ||
124 | +``` | ||
125 | + | ||
126 | +### Data upload 数据上报 | ||
127 | +数据上报需配合服务端使用,参考 [AutoTrack Server(开发中)](https://github.com/epoll-j/auto_track_server),可自行实现服务端。 | ||
128 | + | ||
129 | +数据上报的格式 | ||
130 | +```json | ||
131 | +{ | ||
132 | + 'app_key': config.appKey ?? '', | ||
133 | + 'signature': config.signature!(t), // 签名,可自行配置具体实现的签名算法 | ||
134 | + 't': t, // 时间戳 | ||
135 | + 'user_id': config.userId ?? '', // 用户id,用户登录后设置,调用AutoTrack().updateUserId('userId'); | ||
136 | + 'track_id': config.trackId ?? '', // track_id,每次用户重新打开app会重新生成,在同一个周期内(app打开到关闭)track_id是相同的 | ||
137 | + 'unique_id': config.uniqueId ?? AutoTrackConfigManager.instance.deviceId, // unique_id,可自行配置,如果不配置,会使用设备id | ||
138 | + 'device_id': AutoTrackConfigManager.instance.deviceId, // 设备id,根据deviceInfo获取 | ||
139 | + 'data_list': uploadList.map((e) => e.toMap()).toList(), // TrackModel数据列表,具体格式参考下方 | ||
140 | + 'app_version': AutoTrackConfigManager.instance.appVersion, // app版本 | ||
141 | + 'device_info': AutoTrackConfigManager.instance.deviceInfo // 设备信息 | ||
142 | +} | ||
143 | +``` | ||
144 | + | ||
145 | +```json | ||
146 | +// TrackModel | ||
147 | +{ | ||
148 | + 'type': type, //事件类型 page_view | page_leave | click | drag | custom(自定义类型) | ||
149 | + 'key': key, // 事件key,如果没有启用enableIgnoreNullKey,没有配置key的地方会基于一定的规则生成一个随机的key | ||
150 | + 'time': time, // 事件触发的时间戳 | ||
151 | + 'params': params, // 事件参数,自定义事件可自行设置参数,页面进入、离开、点击、滑动等事件会自动设置(点击位置、滑动方向、页面停留时间等相关信息) | ||
152 | +} | ||
153 | + | ||
154 | +``` |
@@ -48,6 +48,7 @@ class AutoTrackQueue { | @@ -48,6 +48,7 @@ class AutoTrackQueue { | ||
48 | 'user_id': config.userId ?? '', | 48 | 'user_id': config.userId ?? '', |
49 | 'track_id': config.trackId ?? '', | 49 | 'track_id': config.trackId ?? '', |
50 | 'unique_id': config.uniqueId ?? AutoTrackConfigManager.instance.deviceId, | 50 | 'unique_id': config.uniqueId ?? AutoTrackConfigManager.instance.deviceId, |
51 | + 'device_id': AutoTrackConfigManager.instance.deviceId, | ||
51 | 'data_list': uploadList.map((e) => e.toMap()).toList(), | 52 | 'data_list': uploadList.map((e) => e.toMap()).toList(), |
52 | 'app_version': AutoTrackConfigManager.instance.appVersion, | 53 | 'app_version': AutoTrackConfigManager.instance.appVersion, |
53 | 'device_info': AutoTrackConfigManager.instance.deviceInfo | 54 | 'device_info': AutoTrackConfigManager.instance.deviceInfo |
-
Please register or login to post a comment