1. SDK Initialization Parameters

ParameterTypeDefault ValueMeaningRemarks
nameStringsensorsA 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_urlStringData Receiving URL
  • Please add this address to WeChat Official Account Platform -> Development -> Development Settings -> Server Domain.
autoTrackObjectNoneWhether to enable automatic collection
  • Six attribute parameters (appLaunch, appShow, appHide, pageShow, pageShare, mpClick, pageLeave), among which mpClick, pageLeave are false by default, and others are true. That means the default collection of five events $MPLaunch, $MPShow, $MPHide, $MPViewScreen, $MPShare (supported by SDK version 0.9 and above), among which $MPShare event is supported by version 1.9 and above, and $MPPageLeave event is supported by version 1.14.20 and above. By default, $MPClick, $MPPageLeave events are not collected, for details, please refer to preset events;
  • WeChat, Alipay, Baidu mini program SDK support.
show_logBooleantrueWhether to print logs
  • Set true to print logs in the simulator console, which will display the sent data. Set false to hide the logs.
send_timeoutNumber1000Request send timeout (If a request is sent and there is no response within the specified time, the next data will be sent)
  • Unit: milliseconds;
  • Alipay mini program SDK support.
use_client_timeBooleanfalseWhether to use client time
  • Because the inaccuracy of the client system time will lead to the wrong time of this event, so this Rimmer thinks false , indicates that the client time is not used and the server time is used. If the value is set totrue Indicates the system time of using the client. If you add a property{$time: new Date()} ,Notice this has to be Date type, then the data will use the time that you passed in the property;
  • Starting from version 1.14.6, SDK uses the client time to send data by default. This configuration is no longer supported.
  • Only WeChat Mini-Program SDK supports.
max_string_lengthNumber300The 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_pathBooleantrueSet to true to automatically modify the path attribute in Page.onShareAppMessage and add some parameters including the current user's Distinct ID, etc.
  • If you want to automatically collect sharing information, it must be set to true, and the default value is true.
  • Only WeChat Mini-Program SDK supports.
batch_sendBooleantrueWhether to use batch data sending function in Mini-Program
  • WeChat Mini-Program SDK version 1.14.11 and above supports by default;
  • Alipay Mini-Program SDK version 1.1.2 and above supports;
  • Baidu Mini-Program SDK version 0.15.0 and above supports;
  • ByteDance Mini-Program SDK version 0.14.0 and above supports;
  • QQ Mini Program SDK 0.12.0 and above are supported;
  • Quick app SDK 0.8.0 and above are supported.
datasend_timeoutNumber3000Request send cancellation time
  • Unit is milliseconds, if no result is returned within the specified time after the request is sent, the request will be cancelled;
  • Version 1.12.9 and above are supported;
  • Supported by WeChat and Alipay mini program SDKs.
source_channelArrayNoneChannel parameters to be parsed
  • By default, only parameters utm_source, utm_content, utm_campaign, utm_medium, utm_term will be parsed and set to pre-set events. This parameter can be configured to parse other custom parameters, for example ['channel_code'], the _channel_code attribute will be set as a pre-set property of the pre-set event, and _latest_channel_code will be reported as a common property for all events;
  • Version 1.13.8 and above are supported;
  • Supported by WeChat, Baidu, Bytedance, and QQ mini program SDKs.
is_persistent_saveObjectNoneWhether the latest channel information needs to be saved to wxStorage
  • 1.13.9 and above versions support;
  • Two attribute parameters (utm, share) default to false;
  • Only WeChat Mini Program SDK is supported;

preset_properties

ObjectNoneConfigure to collect specified preset properties
  • Attribute parameter (url_path);
  • WeChat Mini Program SDK supports configuring to collect url_path property;
autotrack_exclude_pageObjectNoneConfigure to exclude specified pages from collecting $MPViewScreen page view events
  • One parameter (pageShow);
  • For example: autotrack_exclude_page: { pageShow: ['pages/index/index'] }, after setting like this, the SDK will not collect the $MPViewScreen event for the page with the path 'pages/index/index', supported from version 1.14.2 and above;
  • Only WeChat Mini Program SDK is supported.
frameworkObjectNone

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.

  • An attribute parameter (taro);
  • Configurable as framework: {taro: Taro}.
preset_eventsObjectNoneCustom control of preset events
  • Two attribute parameters (moments_page, collect_element), supported in version 1.14.12 and above;
  • moments_page supports control of whether to enable data collection on the Moments page. Default is false, configure true to send data on the single page;
  • collect_element triggers this function when the user clicks on an element and returns the click event information to determine whether to collect the current element. Returns true for collection and false for no collection;
  • Supported only by WeChat Mini Program SDK.


2. Full-link tracking collection logic

Event nameLifecycleCollection timingDescription
$MPLaunch (Mini Program Launch)App.onLaunchTriggered when the Mini Program process is killed and reopened
  • Triggered once when the Mini Program is initialized globally;
$MPShow (Mini Program display)App.onShowMini Program startup or display when entering foreground from background
  • When Mini Program is started and enters foreground
$MPHide (Mini Program enters background)App.onHideWhen 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
  • When Mini Program goes from foreground to background
$MPViewScreen (Mini Program page view)Page.onShowTriggered when opening a page in Mini Program startup, opening a page within the Mini Program, or entering foreground from background
  • Called every time a page is opened
$MPShare (Mini Program sharing)Page.onShareAppMessageTriggered when the share button is clicked after setting this function
  • Currently, only the user-triggered share can be obtained, and the success feedback of the share cannot be monitored
$MPClick (Mini Program element click)
Collected when the event handling function defined in the Page is triggered
  • Currently, only tap/longtap/longpress events are supported
$MPAddFavorites (Mini Program Favorites)page.onAddToFavoritesTriggered when the Mini Program Favorites button is clicked


$MPPageLeave (Mini Program Page Leave)page.onHide or page.onUnloadTriggered 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();
JS

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"] // 返回元素是字符串的数组 } })
JS

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) } })
JS

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 }); 
JS

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 });
JS

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: ['苹果', '桃子'] });
JS

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);
JS

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();
JS
  • 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']);
JS

8. Get anonymous ID

The Mini Program SDK provides the getAnonymousID interface to obtain the anonymous ID.

sensors.getAnonymousID();
JS

9. Collect duration of Mini Program page views

The Mini Program SDK can automatically collect by enabling the $MPPageLeave preset event.

  1. Supported by WeChat Mini Program SDK 1.14.20 and above
  2. Supported by Alipay Mini Program SDK 1.1.7 and above
sensors.setPara({ 	autoTrack: { 		pageLeave: true // 默认不开启,为 false 	} }); // $MPPageLeave 预置事件中会采集 event_duration 预置属性来记录页面浏览时长,单位为秒
JS

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 // 是否开启本地加密存储 });
JS