Skip to content

埋点 SDK集成

gio-yanruixue edited this page Jul 19, 2021 · 1 revision

埋点 SDK只自动采集用户访问事件和APP关闭事件,其他事件均需要开发同学调用相应埋点 API 采集自定义事件。


Gradle编译环境:Android Studio
适配最低Android系统版本:Android 4.2


1. 添加依赖

在 project 级别下的 build.gradle 文件中添加仓库

    repositories {
        mavenCentral()
        //如果你的版本为 xxx-SNAPSHOT 版本,则需要加入该仓库。
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }

在 module 级别的 build.gradle 文件中添加tracker依赖。

...

dependencies {
    ...

    //GrowingIO 无埋点 SDK
    implementation 'com.growingio.android:tracker-cdp:x.x.x'
}

2. 添加 URL Scheme

URL Scheme 是您在 GrowingIO 平台创建应用时生成的该应用的唯一标识。把 URL Scheme 添加到您的项目,以便使用Mobile Debug等功能时唤醒您的应用。
将应用的 URLScheme 和应用权限添加到你的 AndroidManifest.xml 中的 LAUNCHER Activity 下。
代码示例:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.growingio.testdemo">
​
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
​
    <!--请注意<application/>标签中的name属性值(这里为android:name=".MyApplication")必须为您的Application类-->
    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!--请添加这里的整个 intent-filter 区块,并确保其中只有一个 data 字段-->
            <intent-filter>
                <data android:scheme="growing.您的URL Scheme" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
            <!--请添加这里的整个 intent-filter 区块,并确保其中只有一个 data 字段-->
        </activity>
    </application>
</manifest>

3. SDK初始化配置

请将以下 GrowingTracker.startWithConfiguration加在您的ApplicationonCreate 方法中
代码示例

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        GrowingTracker.startWithConfiguration(this,
                new CdpTrackConfiguration("exampleProjectId", "exampleUrlScheme")
                        .setDataCollectionServerHost("http://myhost.com/")
                        .setDataSourceId("exampleSourceId")
                        .setChannel("XXX应用商店")
                        .setDebugEnabled(BuildConfig.DEBUG)
        );
    }
}

4. SDK 初始化配置项

API 参数类型 是否必填 默认值 说明
projectId String null 官网的中您的项目ID
urlScheme String null 官网的中您的相应APP的UrlScheme
setDataSourceId String null 官网的中您的相应APP的DataSourceId
setDataCollectionServerHost String null 您部署服务的后端Host
setChannel String null APP的分发渠道
setDebugEnabled boolean false 调试模式,会打印SDK log,抛出错误异常,在线上环境务必关闭
setCellularDataLimit int 10 每天发送数据的流量限制,单位MB
setDataUploadInterval int 15 数据发送的间隔,单位秒
setSessionInterval int 30 每次访问会话的最大时长,单位秒
setDataCollectionEnabled boolean true 是否采集数据
setOaidEnabled boolean false 是否采集Android OAID,如果设置为采集的话,受OAID获取时间的限制,事件采集可能会延迟
setUploadExceptionEnabled boolean true 收集SDK内部异常上报服务端

5. 数据采集API

1. 数据采集开关 setDataCollectionEnabled

打开或关闭数据采集

参数说明

参数 参数类型 说明
enabled boolean true打开数据采集,false关闭数据采集

示例

GrowingTracker.get().setDataCollectionEnabled(true);

2. 设置登录用户ID setLoginUserId

当用户登录之后调用setLoginUserId API,设置登录用户ID

参数说明

参数 参数类型 说明
userId String 长度限制大于0且小于等于1000,如果大于长度1000将只截取前1000长度

示例

GrowingTracker.get().setLoginUserId("zhangsan");

3. 清除登录用户ID cleanLoginUserId

当用户登出之后调用cleanLoginUserId,清除已经设置的登录用户ID。

示例

GrowingTracker.get().cleanLoginUserId();

4. 设置用户的地理位置 setLocation

设置用户当前的地理位置,基于WGS-84坐标

参数说明

参数 参数类型 说明
latitude double 地理坐标点纬度
longitude double 地理坐标点经度

示例

GrowingTracker.get().setLocation(39.9, 116.3);

5. 清除用户的地理位置 cleanLocation

清除用户当前的地理位置

示例

GrowingTracker.get().cleanLocation();

6. 设置自定义事件 trackCustomEvent

发送一个自定义事件。在添加所需要发送的事件代码之前,需要在事件管理用户界面配置事件以及事件级变量。

参数说明

参数 参数类型 说明
eventName String 事件名,事件标识符
attributes Map<String, String> 事件发生时所伴随的维度信息
itemKey String 事件发生关联的物品模型Key
itemId String 事件发生关联的物品模型ID

示例

GrowingTracker.get().trackCustomEvent("registerSuccess");

Map<String, String> map = new HashMap<>();
map.put("name", "June");
map.put("age", "12");
GrowingTracker.get().trackCustomEvent("registerSuccess", map);

7. 设置登录用户变量 setLoginUserAttributes

以登录用户的身份定义用户属性变量,用于用户信息相关分析。

参数说明

参数 参数类型 说明
attributes Map<String, String> 用户属性信息

示例

Map<String, String> map = new HashMap<>();
map.put("gender", "male");
map.put("age", "12");
GrowingTracker.get().setLoginUserAttributes(map);

8. 获取设备ID getDeviceId

获取设备id,又称为匿名用户id,SDK 自动生成用来定义唯一设备。 如果没有初始化SDK 或者关闭采集开关可能返回值为null,且可能有IO操作。

示例

GrowingTracker.get().getDeviceId();