sensors_analytics_flutter_plugin 1.0.0

  • Example
  • Installing
  • Versions
  • new62

神策 sensors_analytics_flutter_plugin 插件,封装了神策 iOS & Android SDK 常用 API ,使用此插件,可以完成埋点的统计上报。

1. 在项目中添加安装插件

在 Flutter 项目的 pubspec.yam 文件中添加 sensors_analytics_flutter_plugin 依赖

  # 添加神策 flutter plugin 
  sensors_analytics_flutter_plugin: any

执行 flutter packages get 命令安装插件

  flutter packages get  

Flutter 官网文档

2. Android 端

在程序的入口 ApplicationonCreate() 中调用 SensorsDataAPI.sharedInstance() 初始化 SDK:

import android.content.Context;
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;

    public void onCreate() {
        // 在 Application 的 onCreate 初始化神策 SDK

     * 初始化 SDK 、开启自动采集
    private void initSensorsDataSDK(Context context) {
        try {
            // 初始化 SDK
                    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(退出事件)
        } catch (Exception e) {

     * @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) {
            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
#define SA_SERVER_URL @"<#【正式项目】数据接收地址#>"
#define SA_DEBUG_MODE SensorsAnalyticsDebugOff

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[self initSensorsAnalyticsWithLaunchOptions:launchOptions];
return YES;

- (void)initSensorsAnalyticsWithLaunchOptions:(NSDictionary *)launchOptions {

 // 初始化 SDK
 [SensorsAnalyticsSDK sharedInstanceWithServerURL:SA_SERVER_URL

// 打开自动采集, 并指定追踪哪些 AutoTrack 事件 
//目前 flutter 只支持 $AppStart 和 $AppEnd
[[SensorsAnalyticsSDK sharedInstance] enableAutoTrack:SensorsAnalyticsEventTypeAppStart|


开启需要的 subspecs

修改 Pod 中 SensorsAnalyticsSDK 项目的编译选项,如下图:

3. Flutter 中使用插件

在具体 dart 文件中导入 sensors_analytics_flutter_plugin.dart

import 'package:sensors_analytics_flutter_plugin/sensors_analytics_flutter_plugin.dart';

3.1 埋点事件

例如,触发事件名为 AddToFav ,对应的事件属性有:ProductID 和 UserLevel 的事件:


3.2 设置用户属性

例如,设置用户 Age 属性:



  • TODO: Describe initial release.



Demonstrates how to use the sensors_analytics_flutter_plugin plugin.

Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  sensors_analytics_flutter_plugin: ^1.0.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:sensors_analytics_flutter_plugin/sensors_analytics_flutter_plugin.dart';
Version Uploaded Documentation Archive
1.0.0 Dec 29, 2018 Go to the documentation of sensors_analytics_flutter_plugin 1.0.0 Download sensors_analytics_flutter_plugin 1.0.0 archive
0.0.7 Dec 28, 2018 Go to the documentation of sensors_analytics_flutter_plugin 0.0.7 Download sensors_analytics_flutter_plugin 0.0.7 archive
0.0.6 Dec 28, 2018 Go to the documentation of sensors_analytics_flutter_plugin 0.0.6 Download sensors_analytics_flutter_plugin 0.0.6 archive
0.0.5 Dec 28, 2018 Go to the documentation of sensors_analytics_flutter_plugin 0.0.5 Download sensors_analytics_flutter_plugin 0.0.5 archive
0.0.4 Dec 27, 2018 Go to the documentation of sensors_analytics_flutter_plugin 0.0.4 Download sensors_analytics_flutter_plugin 0.0.4 archive
0.0.3 Dec 26, 2018 Go to the documentation of sensors_analytics_flutter_plugin 0.0.3 Download sensors_analytics_flutter_plugin 0.0.3 archive
0.0.2 Dec 25, 2018 Go to the documentation of sensors_analytics_flutter_plugin 0.0.2 Download sensors_analytics_flutter_plugin 0.0.2 archive
0.0.1 Dec 25, 2018 Go to the documentation of sensors_analytics_flutter_plugin 0.0.1 Download sensors_analytics_flutter_plugin 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 15, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.10
  • Flutter: 1.1.7


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/sensors_analytics_flutter_plugin.dart.

Run flutter format to format lib/sensors_analytics_flutter_plugin.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8