Basic API Introduction (WeChat Mini Program)
|
Collect
1. SDK Initialization Parameters
Parameter | Type | Default Value | Meaning | Remarks |
---|---|---|---|---|
name | String | sensors | A default global variable used by the SDK, which is registered in the App's global function. Can be used in the Page with app[name].track | |
server_url | String | Data Receiving URL |
| |
autoTrack | Object | None | Whether to enable automatic collection |
|
show_log | Boolean | true | Whether to print logs |
|
send_timeout | Number | 1000 | Request send timeout (If a request is sent and there is no response within the specified time, the next data will be sent) |
|
use_client_time | Boolean | false | Whether to use client time |
|
max_string_length | Number | 300 | The maximum length of a general string. Exceeding this length will result in truncation and discard of the excess part (a URL that is too long may cause data sending failure, so the length is limited). | |
allow_amend_share_path | Boolean | true | Set to true to automatically modify the path attribute in Page.onShareAppMessage and add some parameters including the current user's Distinct ID, etc. |
|
batch_send | Boolean | true | Whether to use batch data sending function in Mini-Program |
|
datasend_timeout | Number | 3000 | Request send cancellation time |
|
source_channel | Array | None | Channel parameters to be parsed |
|
is_persistent_save | Object | None | Whether the latest channel information needs to be saved to wxStorage |
|
preset_properties | Object | None | Configure to collect specified preset properties |
|
autotrack_exclude_page | Object | None | Configure to exclude specified pages from collecting $MPViewScreen page view events |
|
framework | Object | None | If using the Taro framework to develop the Mini Program, the element click event will be triggered multiple times. After configuring the Taro parameter, it can be collected only once. |
|
preset_events | Object | None | Custom control of preset events |
|
2. Full-link tracking collection logic
Event name | Lifecycle | Collection timing | Description |
---|---|---|---|
$MPLaunch (Mini Program Launch) | App.onLaunch | Triggered when the Mini Program process is killed and reopened |
|
$MPShow (Mini Program display) | App.onShow | Mini Program startup or display when entering foreground from background |
|
$MPHide (Mini Program enters background) | App.onHide | When clicking on the exit button in the upper right corner of the Mini Program, WeChat goes into the background, the phone is locked, or the Mini Program process is killed |
|
$MPViewScreen (Mini Program page view) | Page.onShow | Triggered when opening a page in Mini Program startup, opening a page within the Mini Program, or entering foreground from background |
|
$MPShare (Mini Program sharing) | Page.onShareAppMessage | Triggered when the share button is clicked after setting this function |
|
$MPClick (Mini Program element click) | Collected when the event handling function defined in the Page is triggered |
| |
$MPAddFavorites (Mini Program Favorites) | page.onAddToFavorites | Triggered when the Mini Program Favorites button is clicked | |
$MPPageLeave (Mini Program Page Leave) | page.onHide or page.onUnload | Triggered when the Mini Program page is hidden or unloaded |
3. Set Event Common Properties
3.1. Set Static Event Common Properties
All Mini Program SDKs support setting common properties for all events through the registerApp interface. You can set common properties using the registerApp method before calling the init method, after importing the sensorsdata.min.js file in the app.js file.
// 注册事件公共属性。 var sensors = require('/dist/wechat/sensorsdata.cjs.js'); sensors.setPara({ name: 'sensors', server_url: '数据接收地址' }); sensors.registerApp({ userLever: 'VIP3', userSex: '男' }); sensors.init();
3.2. Set Dynamic Event Common Properties
Version Requirements
WeChat Mini Program SDK version 1.13.27, Alipay Mini Program SDK version 1.1.3 and above. Other Mini Program SDKs are not supported.
When setting dynamic common properties, please use a function type as the property value. The return value of the function should be a data type supported by Sensors Analytics. Please refer to theData Format. Otherwise, it will be filtered out.
var i=0 sensors.registerApp({ index: function() { return ++i; // 返回数字 }, istrue: function() { return i<10 ? true : false; // 返回bool }, isEmptyString: function() { return ""; // 返回字符串 }, isDate: function() { return new Date('December 17, 1995 03:24:00'); // 返回日期类型 }, isArrayOfStr: function() { return ["1","2","3"] // 返回元素是字符串的数组 } })
Also, if the function returns a value in an asynchronous callback, it will also be filtered out.
sensors.registerApp({ num:function(){ setTimeout(()=>{ return 100 },500) } })
You need to call the registerApp() method to complete the registration of common properties before instantiating the App, otherwise some data may not be collected for the registered common properties.
4. Set User Properties
4.1. Keep the Initial Profile
For attributes that need to be effective only when they are first set, such as the amount of the user's first recharge or the nickname first set, you can use the setOnceProfile interface to record. Different from the setProfile method, if the user attribute being set already exists, this record will be ignored and will not overwrite the existing data. If the attribute does not exist, it will be automatically created.
// 设置用户属性 subscribers 为 7277 sensors.setOnceProfile({ email:'xxx@xx', favoriteFruits: ['苹果', '油桃'], subscribers: 7277 }); // 再次设置用户属性 subscribers 为 7278 不生效,属性值仍然是 7277 sensors.setOnceProfile({ subscribers: 7278 });
4.2. Numerical attribute accumulation
For some numerical attributes, such as total consumption and user points, we can use incrementProfile to accumulate the original value. Sensors Analytics will automatically calculate and save the accumulated value.
// 设置用户订阅次数属性 sensors.incrementProfile({ subscribers: 5 }); // 对用户订阅次数进行累加,此时在神策系统中查询 subscribers 属性值为 10 sensors.incrementProfile({ subscribers: 5 });
4.3. List attribute append
For list-type user attributes, such as users' favorite fruits and movies, you can use appendProfile to append new values.
// 设置用户喜爱的水果属性 sensors.appendProfile({ favoriteFruits: ['葡萄', '香蕉'] }); // 对用户喜爱的水果属性追加新值 sensors.appendProfile({ favoriteFruits: ['苹果', '桃子'] });
5. Custom anonymous ID
By default, the SDK generates an anonymous ID and ensures its uniqueness. If you need to replace the default anonymous ID assigned by Sensors Analytics, you can immediately call the identify method after configuring and initializing the SDK parameters.
// 自定义匿名 ID sensors.identify('自定义匿名 ID', true);
6. Get preset properties
In some cases, it may be necessary to obtain SDK preset properties on the front end. The SDK supports using the getPresetProperties method to obtain some event preset properties.
// 获取事件预置属性 sensors.getPresetProperties();
- This interface is only supported by the WeChat Mini Program SDK
7. Clear public properties
To clear the public properties set by the registerApp call, the SDK provides the clearAppRegister interface.
// 清除设置的 current_url 和 referrer 公共属性 sensors.clearAppRegister(['current_url', 'referrer']);
8. Get anonymous ID
The Mini Program SDK provides the getAnonymousID interface to obtain the anonymous ID.
sensors.getAnonymousID();
9. Collect duration of Mini Program page views
The Mini Program SDK can automatically collect by enabling the $MPPageLeave preset event.
- Supported by WeChat Mini Program SDK 1.14.20 and above
- Supported by Alipay Mini Program SDK 1.1.7 and above
sensors.setPara({ autoTrack: { pageLeave: true // 默认不开启,为 false } }); // $MPPageLeave 预置事件中会采集 event_duration 预置属性来记录页面浏览时长,单位为秒
10. Local storage encryption function
Currently, the information saved by the SDK in the storage contains the user information and the property information set by the register. The data in the storage can be deeply encrypted to ensure security
版本要求
- WeChat Mini Program SDK 1.14.9 and above
10.1. Function Configuration
// app.js var sensors = require('/dist/wechat/sensorsdata.cjs.js'); sensors.setPara({ name: 'sensors', server_url: '数据接收地址', encrypt_storage : true // 是否开启本地加密存储 });
Note: The content of this document is a technical document that provides details on how to use the Sensors product and does not include sales terms; the specific content of enterprise procurement products and technical services shall be subject to the commercial procurement contract.