The sa-sdk-alipaymini repository has been upgraded to the sa-sdk-mini repository for unified code iteration and management.
  • The sa-sdk-mini repository supports ID3 universal user association, and sa.appLaunch, sa.appShow, sa.appHide, sa.pageShow, sa.pageLoad API have been taken offline.

  • sa-sdk-smartprogram repositoryNo longer updated or maintained, if you need to use the old version SDK, pleaseGo to the old version SDK.

1. Integrate Sensors Analytics SDK

1.1. import SDK

  • From GitHub get the source code for baidu Mini Program SDK.
  • Place index.esm.js file into the Mini Program project;
  • Fromapp.js file By require() import SDK.
import sensors from '/dist/mp-baidu/index.esm';
JAVASCRIPT
  • From GitHub get the source code for Baidu Mini Program SDK.
  • Place the index.esm.jsfile into the Mini Program project;
  • In app.jsx By importimport SDK
import sensors from '/dist/mp-baidu/index.esm';
JAVASCRIPT

1.2. Initialize the SDK

Call init() function to initialize SDK

sensors.init({ name: 'sensors', server_url: '您的数据接收地址', // 全埋点控制开关 	autoTrack: {}, 	// 是否允许控制台打印查看埋点数据(建议开启查看) 	show_log: true })
JS

2. SDK Basic Configuration

2.1. Configure the data receiving address for the project

Data Receiving URL:

After obtaining the Data Receiving URL, set the server_url in the setPara interface. The domain name of server_url should be configured according to the requirements of Baidu Mini Program (https://smartprogram.baidu.com/docs/third/info/set_server_domain/) to configure the Mini Program server domain name.

2.2. Enable full point burial

In the init() function, in the autoTrack method, configure the types of full point burial to be enabled:

sensors.init({ 	autoTrack:{ 		appLaunch:true, // 默认为 true,false 则关闭 $MPLaunch 事件采集, 		appShow:true, // 默认为 true,false 则关闭 $MPShow 事件采集 		appHide:true, // 默认为 true,false 则关闭 $MPHide 事件采集 		pageShow:true, // 默认为 true,false 则关闭 $MPViewScreen 事件采集 		mpClick: true // 默认为 true,false 则关闭 $MPClick 事件采集 	} 	/** 	 * 其他配置 	 */ });
JS

2.3. Set event common attributes

In order to add attributes to all events, you can call registerApp() before initializing the SDK to register attributes as common attributes:

sensors.registerApp({ 	userLever: 'VIP3', 	userSex: '男' });
JS

2.4. User login

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

sensors.login("登录 ID");
JS

For users who automatically log in, you can obtain the login ID and call the login() method before initializing the SDK.

2.5. Code Point Tracking

After initializing the SDK, you can use the track() method to track user behavior events and add custom attributes to the events:

sensors.track(event_name [,properties]);
JS

Configure parameters:

ParameterRequiredDescription
event_nameTrueEvent name
propertiesfalseAdd custom properties to user behavior events, type: Object.

For example:

// 为 click 事件添加自定义属性 name 值为点击。 sensors.track('click',{ 	name: '点击' }); 
JS

Event Name and Event Property format standards, please refer toData format.

3. Debugging and viewing event information.

3.1. Event Trigger Logs

setPara() When configuring the initial parameters, use the "Configure" function.show_log: true Function (enabled by default) and SDK initialization completed (i.e. After the API call, the baidu Developer Tool.console Prints the collected data information:

3.2. Event sending status

When event data is successfully sent, you can see the sa request in the module of the baidu developer tool.

4. SDK optional configuration

4.1. User attributes can be set.

 setProfile( properties ): When the same key is set multiple times, the value will be overwritten and replaced.

sensors.setProfile({ 	email:'xxx@xx', 	favoriteFruits: ['苹果', '油桃'], 	subscribers: 7277 });
JS
  • Regarding the difference between event properties and user properties, please refer toData model
  • User attribute naming constraints, please refer toData format
  • User Attribute Other Setting Methods, please refer toUser Attribute Set

4.2. Channel Track

  • When users access the mini-program through a path containing utm parameters, pre-set events are triggered. $MPLaunch、$MPShow can parse the launch path inutm relevant parameters are set as their own attributes and attribute values.$latest_utm related attributes are valid for all events throughout the lifecycle of the program.
  • Support custom channel tracking.