1. Integrate with Sensors Analytics SDK

1.1. Import SDK

  • Get the Jingdong Mini Program SDK source code from GitHub;
  • Put the taobao-mp.min.js file into the mini program project;
  • In the app.js file, import the SDK using the import statement.
import sensors from './utils/mp-taobao/taobao-mp.min.js'; // 初始化 SDK 参数 sensors.init({ 	server_url: '您的数据接收地址', 	// 是否允许控制台打印查看埋点数据(建议开启查看) 	show_log: true, 	autoTrack: { 	appLaunch: true, //是否采集 $MPLaunch 事件,true 代表开启。 	appShow: true, //是否采集 $MPShow 事件,true 代表开启。 	appHide: true, //是否采集 $MPHide 事件,true 代表开启。 	pageShow: true, //是否采集 $MPViewScreen 事件,true 代表开启。 	mpClick: true, // 是否采集 $MPClick 事件,true 代表开启。 	pageLeave: false //是否采集 页面停留时长 	}, 	batch_send: false //是否开启批量数据上报 });
JS
  • batch_send defaults to true. It needs to be changed to false.
    • If you want to set it to true, which means enabling batch sending, you need to meet the requirement of Sensors Analytics backend Edge >= 0.4.2.125. Please check carefully, otherwise the data reporting will fail (consult the technical advisor).
    • If set to false, which means using single sending, there are no requirements.

1.2. Introduce SDK data sending plugin

  • Get the Jingdong Mini Program SDK source code from GitHub;
  • Put the taobao-send-adapter.min.js file into the mini program project;
  • In the app.js file, import the SDK using the import statement.
import sensors from './utils/mp-taobao/taobao-mp.min.js'; import sensors from './utils/mp-taobao/taobao-send-adapter.min.js';   //云函数配置方式: sensorsdata.use(TaobaoSendAdapter,{ cloudFuncSend: true, // 是否使用云函数进行数据发送,默认为 false cloudFuncName: '', // 调用的云函数名称 cloudFuncHandler: '' // 指定云函数的 handler,默认为 main }); //云应用配置方式: sensors.use(TaobaoSendAdapter,{ exts: { domain:'', xxx:'' }, cloudAppId:'',// 云应用 Id,无默认值 path: '', // 云应用数据接口,无默认值 }); sensors.init({...});
JS
  • The Taobao Mini Program taobao-send-adapter plugin needs to be registered before sensors.init().
  • It needs to be specified as cloud function or cloud application configuration.

2. Reporting Method

2.1. Using cloud function to report

Taobao small programs are strict in the use of cloud functions to report to the outside, so the specific domain names and fields need to be communicated with us and confirmed by the development subject to apply for the white list Reference document

2.1.1. User function reporting

Here we provide the implementation of user function reporting in the code:

Official Example

// 1、server目录中创建自定义云函数。注意该云函数名称需要在初始化时填写 cloudFuncName。 // 2、index.js // 注意这里的 main 为示例,名称请自行确定,该名称需要在初始化时填写 cloudFuncHandler。 exports.main = async (context) => { const { params, body } = context.data; const result = await context.cloud.httpApi.invoke({ method: 'POST', domain:'', // HTTP 请求参数,与申请通过的 HTTP 的白名单中请求域名必须一致 headers: { 'Content-Type': 'application/json;charset=UTF-8' }, path:'', // HTTP 请求 path params, // URL 参数 body: body // 放置在 BODY 中的参数 }); return { success: true, msg: result }; };
JS

2.2. Use cloud application reporting

No need to apply for a whitelist. Because this function is provided by Taobao's own cloud API, we cannot guarantee the stability of Taobao's service. Please conduct sufficient testing before going live, and communicate with us promptly if there are any issues.

3. SDK basic configuration

3.1. Configure project data receiving address

Obtain the data receiving address as shown in the following figure:

3.2. Set event common attributes

For attributes that need to be added to all events, you can call registerApp() to register the attributes as common attributes before initializing the SDK:

// 在需要使用 SDK 接口的页面逻辑文件中,需要通过 import 将 SDK 引入,再调用相应接口 import sensors from './utils/mp-taobao/taobao-mp.min.js'; sensors.registerApp({ 	userLever: 'VIP3', 	userSex: '男' });
JS

3.3. User login

When a user registers or logs in successfully, you need to call the login() method and pass in the login ID:

// 在需要使用 SDK 接口的页面逻辑文件中,需要通过 import 将 SDK 引入,再调用相应接口 import sensors from './utils/mp-taobao/taobao-mp.min.js'; sensors.login("登录 ID");
JS
  • For users who log in automatically, you can obtain the login ID and call the login() method before initializing the SDK.

3.4. Code point tracking events

You can track user behavior events and add custom attributes to events using the track() method:

// 在需要使用 SDK 接口的页面逻辑文件中,需要通过 import 将 SDK 引入,再调用相应接口 import sensors from './utils/mp-taobao/taobao-mp.min.js'; Page({ 	onShow: function(){ 		sensors.track('pageview',{ 			title: '首页' 		}); 	} }); 
JS

4. Debug and view event information

4.1. Event triggering logs

5. SDK optional configuration

5.1. Set User Properties

setProfile( properties ):  You can set user properties, and when the same key is set multiple times, the value will be overwritten:

// 在需要使用 SDK 接口的页面逻辑文件中,需要通过 import 将 SDK 引入,再调用相应接口 import sensors from './utils/mp-taobao/taobao-mp.min.js'; sensors.setProfile({ 	email:'xxx@xx', 	favoriteFruits: ['苹果', '油桃'], 	subscribers: 7277 });
JS
  • For the difference between event properties and user properties, please refer to the data model.
  • For the naming constraints of user properties, please refer to the data format.
  • For other methods of setting user properties, please refer to user property settings.