Flutter插件ablaevent_flutter的使用方法
Flutter插件ablaevent_flutter的使用方法
使用方法
要使用此插件,在您的 pubspec.yaml
文件中添加 ablaevent_flutter
作为依赖项。
支持的方法
方法 | Android | iOS | Web |
---|---|---|---|
identify |
X | X | X |
capture |
X | X | X |
screen |
X | X | X |
alias |
X | X | X |
getAnonymousId |
X | X | X |
reset |
X | X | X |
disable |
X | X | |
enable |
X | X | |
debug |
X* | X | X |
setContext |
X | X | |
isFeatureEnabled |
X | X | X |
reloadFeatureFlags |
X | X | X |
* 调试必须在 AndroidManifest.xml
中作为配置参数设置(见下文)。Android。
示例
import 'package:flutter/material.dart';
import 'package:ablaevent_flutter/ablaevent_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
Posthog().screen(
screenName: 'Example Screen',
);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Ablaevent example app'),
),
body: Center(
child: FlatButton(
child: Text('TRACK ACTION WITH Ablaevent'),
onPressed: () {
Posthog().capture(
eventName: 'ButtonClicked',
properties: {
'foo': 'bar',
'number': 1337,
'clicked': true,
},
);
},
),
),
),
navigatorObservers: [
PosthogObserver(),
],
);
}
}
安装
设置您的 Ablaevent API 密钥,并更改自动事件跟踪(仅适用于 Android 和 iOS),如果您希望库为您处理它。
记得应用程序生命周期事件在初始化时不会有任何特殊上下文。如果您使用的是自托管的 Ablaevent 实例,您还需要拥有实例的公共主机名或 IP 地址。
Android
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.posthog.ablaevent_flutter_example">
<application>
<activity>
[...]
</activity>
<meta-data android:name="com.posthog.posthog.API_KEY" android:value="YOUR_API_KEY_GOES_HERE" />
<meta-data android:name="com.posthog.posthog.POSTHOG_HOST" android:value="https://e.abla.io" />
<meta-data android:name="com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS" android:value="false" />
<meta-data android:name="com.posthog.posthog.DEBUG" android:value="false" />
</application>
</manifest>
iOS
Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
[...]
<key>com.posthog.posthog.API_KEY</key>
<string>YOUR_API_KEY_GOES_HERE</string>
<key>com.posthog.posthog.POSTHOG_HOST</key>
<string>https://e.abla.io</string>
<key>com.posthog.posthog.TRACK_APPLICATION_LIFECYCLE_EVENTS</key>
<false/>
[...]
</dict>
</plist>
Web
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>example</title>
</head>
<body>
<script>
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
posthog.init("YOUR_WRITE_KEY_GOES_HERE", {api_host: 'https://e.abla.io'});
</script>
<script src="main.dart.js" type="application/javascript"></script>
</body>
</html>
示例代码
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:ablaevent_flutter/ablaevent_flutter.dart';
void main() {
/// 等待平台通道初始化以调用 `setContext`
WidgetsFlutterBinding.ensureInitialized();
/// `context.device.token` 是一个特殊属性。
/// 当你定义它时,再次设置上下文(例如 `{}`)不会清除设备令牌。
///
/// 这用于允许您设置字符串形式的设备令牌,这是与
/// Firebase Cloud Messaging (FCM) 集成的用例。
///
/// 该插件目前不支持 Apple 推送通知服务 (APNs) 令牌,它们是二进制结构。
///
/// 除了这种特殊情况外,任何其他需要定义或重新定义的上下文属性都可以这样做。
Posthog().setContext({
'device': {
'token': 'testing',
}
});
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
Posthog().screen(
screenName: 'Example Screen',
);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Posthog example app'),
),
body: Column(
children: <Widget>[
Spacer(),
Center(
child: TextButton(
child: Text('CAPTURE ACTION WITH POSTHOG'),
onPressed: () {
Posthog().capture(
eventName: 'ButtonClicked',
properties: {
'foo': 'bar',
'number': 1337,
'clicked': true,
},
);
},
),
),
Spacer(),
Center(
child: TextButton(
child: Text('Update Context'),
onPressed: () {
Posthog().setContext({'custom': 123});
},
),
),
Spacer(),
Center(
child: TextButton(
child: Text('Clear Context'),
onPressed: () {
Posthog().setContext({});
},
),
),
Spacer(),
Center(
child: TextButton(
child: Text('Disable'),
onPressed: () async {
await Posthog().disable();
Posthog().capture(
eventName: 'This event will not be logged',
properties: {});
},
),
),
Spacer(),
Center(
child: TextButton(
child: Text('Enable'),
onPressed: () async {
await Posthog().enable();
Posthog().capture(
eventName: 'Enabled capturing events!', properties: {});
},
),
),
Spacer(),
Center(
child: TextButton(
child: Text('Is Feature enabled'),
onPressed: () async {
Posthog().isFeatureEnabled('feature').then((value) {
print('Feature enabled: $value');
});
},
),
),
Spacer(),
Center(
child: TextButton(
child: Text('Reload feature flag'),
onPressed: () async {
Posthog().reloadFeatureFlags();
},
),
),
],
),
),
navigatorObservers: [
PosthogObserver(),
],
);
}
}
更多关于Flutter插件ablaevent_flutter的使用方法的实战教程也可以访问 https://www.itying.com/category-92-b0.html