1. Integrate Sensors Analytics SDK

1.1. Introduce SDK

  • Get the source code xiaohongshu-mp.min.js of小红书小程序 SDK from the build directory in the GitHub repository;
  • Put the xiaohongshu-mp.min.js file into the mini program project;
  • Import the SDK by using the import method in the app.js file.
import sensors from './build/xiaohongshu-mp.min.js'; // 初始化 sensors.init({  server_url: '您的数据接收地址', // 是否允许控制台打印查看埋点数据(建议开启查看) show_log: true, autoTrack: { appLaunch: true, // 默认为 true,false 则关闭 $MPLaunch 事件采集 appShow: true, // 默认为 true,false 则关闭 $MPShow 事件采集 appHide: true, // 默认为 true,false 则关闭 $MPHide 事件采集 pageShow: true, // 默认为 true,false 则关闭 $MPViewScreen 事件采集 mpClick: true, // 默认为 false,true 则开启 $MPClick 事件采集 pageLeave: true // 默认为 false, true 则开启 $MPPageLeave事件采集 }, name: 'sd' }); 
JS
  • The compressed file of the小红书小程序 SDK xiaohongshu-mp.min.js has a size of about 36.2 KB.
  • You need to call init() before instantiating the App to avoid missing some pre-set events.
  • Data collected is cached in memory before calling the init() interface; after calling the init() interface, the cached data will be sent via the network.

2. SDK basic configuration

2.1. Configure the data receiving URL for your project

Obtain the data receiving URL as shown below:

2.2. Set common properties for events

For attributes that need to be added to all events, you can register them as common properties before initializing the SDK by callingregisterApp().

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

2.3. User association

User association is used to uniquely identify users and improve the accuracy of user behavior analysis. Currently, Sensors Analytics provides two types of user association methods: simple user association and cross-domain user association, which are used to support different business scenarios.

2.4. Code Tracking Events

After SDK initialization, you can use the track() method to track user behavior events and add custom properties to them:

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

Configuration parameters:

ParameterRequiredDescription
event_nameYesEvent name
propertiesNoAdd custom properties to user behavior events, type: Object.

Example:

// 为 pageview 事件添加自定义属性 title 值为首页。 // 在需要使用 SDK 接口的页面逻辑文件中,需要通过 import 将 SDK 引入,再调用相应接口 import sensors from './build/xiaohongshu-mp.min.js'; Page({ 	onShow: function(){ 		sensors.track('pageview',{ 			title: '首页' 		}); 	} }); 
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

4. SDK Optional Configuration

4.1. Set User Attributes

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

// 在需要使用 SDK 接口的页面逻辑文件中,需要通过 import 将 SDK 引入,再调用相应接口 import sensors from './build/xiaohongshu-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 naming constraints of user properties, please refer to the data format.
  • For other methods of setting user properties, please refer to the user property settings.