Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
auto_track_plugin
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
epoll-j
2024-11-21 15:29:18 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
6a11debe4b8ced512f1eca547271f8f848b7d204
6a11debe
1 parent
4866ec88
feat(track): 添加采样率设置
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
45 additions
and
5 deletions
README.md
example/lib/main.dart
lib/auto_track/config/config.dart
lib/auto_track/config/manager.dart
lib/auto_track/config/queue.dart
lib/auto_track/index.dart
pubspec.yaml
README.md
View file @
6a11deb
...
...
@@ -40,6 +40,7 @@ class _MyAppState extends State<MyApp> {
void
initState
()
{
AutoTrack
()
.
config
(
AutoTrackConfig
(
// 其余配置可查看AutoTrackConfig类
samplingRate:
0.9
,
// 采样率
host:
'http://localhost:3000/api/track'
,
eventHandler:
(
model
)
=>
{
// 事件触发会调用此方法,可自行处理
...
...
@@ -90,6 +91,20 @@ class _MyAppState extends State<MyApp> {
}
```
##### 更新采样率
```
dart
AutoTrack
().
updateSampleRate
(
0.5
);
```
##### 登录后更新用户id
```
dart
AutoTrack
().
updateUserId
(
'userId'
);
```
##### 采样错误信息
```
dart
FlutterError
.
onError
=
(
details
)
{
Track
.
instance
.
reportError
(
details
,
details
.
stack
!);
};
```
#### 具体使用
```
dart
...
...
example/lib/main.dart
View file @
6a11deb
...
...
@@ -19,9 +19,8 @@ class _MyAppState extends State<MyApp> {
void
initState
()
{
AutoTrack
()
.
config
(
AutoTrackConfig
(
eventHandler:
(
model
)
=>
{
print
(
'event handler
${model.type}
'
)
},
samplingRate:
0.9
,
// 采样率
eventHandler:
(
model
)
=>
{
print
(
'event handler
${model.type}
'
)},
pageConfigs:
[
AutoTrackPageConfig
<
PageA
>(
pageID:
'page_a'
,
...
...
@@ -34,8 +33,15 @@ class _MyAppState extends State<MyApp> {
.
enableDrag
()
.
enableIgnoreNullKey
()
.
enableLog
();
super
.
initState
();
// AutoTrack().updateSampleRate(0.5); 更新采样率
// AutoTrack().updateUserId('xxxxxx'); 用户登录后设置用户id
//
// 采样错误信息
// FlutterError.onError = (details) {
// Track.instance.reportError(details, details.stack!);
// };
}
@override
...
...
lib/auto_track/config/config.dart
View file @
6a11deb
...
...
@@ -11,6 +11,7 @@ class AutoTrackConfig {
AutoTrackConfig
({
this
.
host
,
// 数据上报地址
this
.
uploadInterval
,
// 数据上报间隔
this
.
samplingRate
=
1
,
// 采样率
this
.
appKey
=
''
,
// 数据上报时根据key和secret生成签名
this
.
appSecret
=
''
,
this
.
signature
,
// 签名生成方法,默认使用sha256对key、时间戳和secret进行签名
...
...
@@ -40,6 +41,9 @@ class AutoTrackConfig {
String
?
userId
;
String
?
uniqueId
;
/// 采样率,默认 1 (100%)
double
samplingRate
;
int
?
uploadInterval
;
Function
?
signature
;
...
...
lib/auto_track/config/manager.dart
View file @
6a11deb
...
...
@@ -62,6 +62,10 @@ class AutoTrackConfigManager {
_config
.
userId
=
userId
;
}
void
updateSampleRate
(
double
rate
)
{
_config
.
samplingRate
=
rate
;
}
void
updatePageConfigs
(
List
<
AutoTrackPageConfig
>
pageConfigs
)
{
_config
.
pageConfigs
=
pageConfigs
;
}
...
...
lib/auto_track/config/queue.dart
View file @
6a11deb
import
'dart:async'
;
import
'dart:math'
;
import
'package:auto_track/auto_track/config/manager.dart'
;
import
'package:auto_track/auto_track/utils/track_model.dart'
;
...
...
@@ -39,6 +40,12 @@ class AutoTrackQueue {
_queue
.
clear
();
final
config
=
AutoTrackConfigManager
.
instance
.
config
;
final
host
=
config
.
host
;
if
(
config
.
samplingRate
!=
1
)
{
if
(
Random
().
nextDouble
()
>
config
.
samplingRate
)
{
// 不在采样范围不上传
return
;
}
}
if
(
host
!=
null
)
{
final
t
=
DateTime
.
now
().
millisecondsSinceEpoch
;
dio
.
post
(
host
,
data:
{
...
...
lib/auto_track/index.dart
View file @
6a11deb
...
...
@@ -19,6 +19,10 @@ class AutoTrack {
AutoTrackConfigManager
.
instance
.
updateUserId
(
id
);
}
void
updateSampleRate
(
double
rate
)
{
AutoTrackConfigManager
.
instance
.
updateSampleRate
(
rate
);
}
AutoTrack
config
(
AutoTrackConfig
?
config
)
{
if
(
config
!=
null
)
{
AutoTrackConfigManager
.
instance
.
updateConfig
(
config
);
...
...
pubspec.yaml
View file @
6a11deb
name
:
auto_track
description
:
"
Auto
Track
Plugin"
version
:
0.0.
5
version
:
0.0.
6
homepage
:
https://github.com/epoll-j/auto_track_plugin
environment
:
...
...
Please
register
or
login
to post a comment