sensors_analytics_flutter_plugin 1.0.1 sensors_analytics_flutter_plugin: ^1.0.1 copied to clipboard
This is the official flutter plugin for Sensors Analytics,with this plugin you can easily collect your app data on Android and iOS.
神策 sensors_analytics_flutter_plugin
插件,封装了神策 iOS & Android SDK 常用 API ,使用此插件,可以完成埋点的统计上报。
1. 在项目中添加安装插件 #
在 Flutter 项目的 pubspec.yam
文件中添加 sensors_analytics_flutter_plugin
依赖
dependencies:
# 添加神策 flutter plugin
sensors_analytics_flutter_plugin: any
执行 flutter packages get 命令安装插件
flutter packages get
2. Android 端 #
在程序的入口 Application 的 onCreate()
中调用 SensorsDataAPI.sharedInstance()
初始化 SDK:
import io.flutter.app.FlutterApplication;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class App extends FlutterApplication {
// debug 模式的数据接收地址 (测试,测试项目)
final static String SA_SERVER_URL_DEBUG = "【测试项目】数据接收地址";
// release 模式的数据接收地址(发版,正式项目)
final static String SA_SERVER_URL_RELEASE = "【正式项目】数据接收地址";
// SDK Debug 模式选项
// SensorsDataAPI.DebugMode.DEBUG_OFF - 关闭 Debug 模式
// SensorsDataAPI.DebugMode.DEBUG_ONLY - 打开 Debug 模式,校验数据,但不进行数据导入
// SensorsDataAPI.DebugMode.DEBUG_AND_TRACK - 打开 Debug 模式,校验数据,并将数据导入到 Sensors Analytics 中
// TODO 注意!请不要在正式发布的 App 中使用 DEBUG_AND_TRACK /DEBUG_ONLY 模式! 请使用 DEBUG_OFF 模式!!!
// debug 时,初始化 SDK 使用测试项目数据接收 URL 、使用 DEBUG_AND_TRACK 模式;release 时,初始化 SDK 使用正式项目数据接收 URL 、使用 DEBUG_OFF 模式。
private boolean isDebugMode;
@Override
public void onCreate() {
super.onCreate();
// 在 Application 的 onCreate 初始化神策 SDK
initSensorsDataSDK(this);
}
/**
* 初始化 SDK 、开启自动采集
*/
private void initSensorsDataSDK(Context context) {
try {
// 初始化 SDK
SensorsDataAPI.sharedInstance(
context, // 传入 Context
(isDebugMode = isDebugMode(context)) ? SA_SERVER_URL_DEBUG : SA_SERVER_URL_RELEASE, // 数据接收的 URL
isDebugMode ? SensorsDataAPI.DebugMode.DEBUG_AND_TRACK : SensorsDataAPI.DebugMode.DEBUG_OFF); // Debug 模式选项
// 打开自动采集, 并指定追踪哪些 AutoTrack 事件
List<SensorsDataAPI.AutoTrackEventType> eventTypeList = new ArrayList<>();
eventTypeList.add(SensorsDataAPI.AutoTrackEventType.APP_START);// $AppStart(启动事件)
eventTypeList.add(SensorsDataAPI.AutoTrackEventType.APP_END);// $AppEnd(退出事件)
SensorsDataAPI.sharedInstance().enableAutoTrack(eventTypeList);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param context App 的 Context
* @return debug return true,release return false
* 用于判断是 debug 包,还是 relase 包
*/
public static boolean isDebugMode(Context context) {
try {
ApplicationInfo info = context.getApplicationInfo();
return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
3. iOS 端 #
在程序的入口(如 AppDelegate.m )中引入 SensorsAnalyticsSDK.h
,并在初始化方法(如 - application:didFinishLaunchingWithOptions:launchOptions
)中调用 sharedInstanceWithServerURL:andLaunchOptions:andDebugMode:
初始化 SDK。
#import "SensorsAnalyticsSDK.h"
#ifdef DEBUG
#define SA_SERVER_URL @"<#【测试项目】数据接收地址#>"
#define SA_DEBUG_MODE SensorsAnalyticsDebugAndTrack
#else
#define SA_SERVER_URL @"<#【正式项目】数据接收地址#>"
#define SA_DEBUG_MODE SensorsAnalyticsDebugOff
#endif
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self initSensorsAnalyticsWithLaunchOptions:launchOptions];
return YES;
}
- (void)initSensorsAnalyticsWithLaunchOptions:(NSDictionary *)launchOptions {
// 初始化 SDK
[SensorsAnalyticsSDK sharedInstanceWithServerURL:SA_SERVER_URL
andLaunchOptions:launchOptions
andDebugMode:SA_DEBUG_MODE];
// 打开自动采集, 并指定追踪哪些 AutoTrack 事件
//目前 flutter 只支持 $AppStart 和 $AppEnd
[[SensorsAnalyticsSDK sharedInstance] enableAutoTrack:SensorsAnalyticsEventTypeAppStart|
SensorsAnalyticsEventTypeAppEnd];
}
开启需要的 subspecs
修改 Pod 中 SensorsAnalyticsSDK 项目的编译选项. Pod -> Build Settings ->Preprocessor Macros
4. Flutter 中使用插件 #
在具体 dart 文件中导入 sensors_analytics_flutter_plugin.dart
import 'package:sensors_analytics_flutter_plugin/sensors_analytics_flutter_plugin.dart';
4.1 埋点事件 #
例如,触发事件名为 AddToFav ,对应的事件属性有:ProductID 和 UserLevel 的事件:
SensorsAnalyticsFlutterPlugin.track("AddToFav",{"ProductID":123456,"UserLevel":"VIP"});
4.2 设置用户属性 #
例如,设置用户 Age 属性:
SensorsAnalyticsFlutterPlugin.profileSet({"Age":18});