1. Integrate Sensors Analytics SDK

1.1. Integration of SDK

  • From GitHub mp-jd directory to obtain small program SDK source code
  • Put the index.esm.js file into the Mini Program project;
  • In the app.js file, import the SDK with import;
import sensors from '/dist/mp-jd/index.esm';
JS

1.2. Initialize the SDK

Call the init() method to initialize the SDK

sensors.init({ 	name: 'sensors', 	server_url: '您的数据接收地址', 	// 全埋点控制开关 	autoTrack: {}, 	// 自定义渠道追踪参数,如source_channel: ["custom_param"] 	source_channel: [], 	// 是否允许控制台打印查看埋点数据(建议开启查看) 	show_log: true });
JS

2. SDK basic configuration

2.1. Configure the project data receiving URL

For Sensors Analytics v2.2 or above, you can obtain the data receiving URL as shown in the following figure. For other versions, you can switch document versions to view the data receiving URL:

After obtaining the data receiving address setPara Interface Set server_urlserver_url the domain name needs to follow the requirements of the Bytedance applet(https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/guide/basic-ability/network)configure the request legal domain name of the applet.

2.2. Enable automatic tracking

autoTrack can be used to configure the types of automatic tracking that need to be enabled (the default is false):

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


2.3. Set event common properties

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

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

2.4. User Login

When a user successfully registers or logs in, the login() method needs to be called and the login ID needs to be passed as a parameter.

sensors.login("登录 ID");
JS
  • For users who have automatic login enabled, the login ID can be obtained and the login() method can be called before initializing the SDK.

2.5. Tracking events with code instrumentation

After initializing the SDK, user behavior events can be tracked and custom properties can be added to the events using the track() method.

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

Configuration parameters:

ParameterRequiredDescription
event_nameYesThe name of the event
propertiesNoCustom properties added to the user behavior event, type: Object.

Example:

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

For the format specification of event names and event properties, please refer to Data format.

3. Debugging event information

3.1. Event trigger logs

When configuring the initialization parameters with the setPara() method, the Log function can be enabled by setting show_log: true. After the SDK is initialized (i.e., after the init() method is called), the ByteDance developer tool console will print the collected data information.

3.2. Event Dispatch

When the event data is successfully sent, you can view the request of 'sa' in the 'Network' module of the TikTok Developer Tools:

4. Optional SDK Configuration

4.1. Set User Properties

setProfile( properties ): You can set user properties. When setting the same key multiple times, the value will be overwritten:

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 to set user properties, please refer to User Property Settings.

4.2. Channel Tracking

  1. When a user accesses the Mini Program through a path that contains utm-related parameters, the preset events $MPLaunch, $MPShow will parse the utm-related parameters in the launch path as their own properties and values. It will also set the related properties $latest_utm for all events. This feature is valid during the lifecycle of Mini Programs.
  2. Custom Channel Tracking Support