# Weixin Mini Program API
# API Dxplaination
All of the following APIs can be called directly bywx.obsor by introducing npm packages to be compatible with lower versions of base library callswx.obs。
npm package access method:
- Install: Execute [Weixin Mini Program in the root directory
npm install @wxobs/miniprogram-helper - Build: In WeChat Developer Tools, click "Tools" - > "Build npm" in the upper-left menu bar
- Use: There will be differences depending on the choice of Startup Mode in Project Settings > Collect Configuration:
- If you use the default
to startautomatically: You can call custom user properties, events, etc. directly without setup - If it is
custom start: Introduce the setup function in the apagejs file and execute it in onLaunch to start the collection.
Note: npm package size 10k, do not worry about affecting the Weixin Mini Program package size, this package is only a simple wrapper for the base library interface
Polyfill sample code:
import '@wxobs/miniprogram-helper';
App({
onLaunch: async function () {
...
},
});
# List of interfaces
| Helper interface | Introductions |
|---|---|
| setup | Start the collection |
| getSessionId | Get the current session ID |
| getStatus | Get the current collection status |
| teardown | Stop collecting |
| event | Report custom events |
| setCustomId | Custom Business Markup |
| setCustomProperties | Customizing business attributes |
# Start the collection
Interface:setup
Important :setupThe method is only available when Custom Startup is selected in Startup Mode, and calls in Automatic Startup mode will fail.
await wx.obs.setup({
maskMode: 'no-mask',
})
Configure and start data acquisition. Note : All other helper APIs must be executed after the setup returns a promise resolve
parameter
{
maskMode?: 'all-mask' | 'no-mask' // 可选,默认值 'all-mask','all-mask'表示采集时默认遮罩所有元素,'no-mask'表示不遮罩。使用 'no-mask' 模式时,请注意遵守《个人隐私保护法》等法规,在用户同意的情况下采集隐私数据。
isWhitelistMaskMode?: boolean // [已弃用] 可选,默认值 true。true 表示以白名单模式遮罩小程序中的元素,false表以黑名单模式遮罩小程序中的元素。不可和 maskMode 同时使用。
captureDataAttrs?: boolean // 可选,默认为 true。采集 webview 中的元素时,是否采集以 data-* 为开头的属性
shouldCapturePage?: (page: string, query: Record<string, string>) => boolean // 可选。用于控制是否采集某页面的回调,page 和 query 是该页面的属性,默认为全部采集。
shouldCaptureNetwork?: (info: INetworkRequestInfo) => boolean | INetworkRequestInfo // 可选。用于自定义网络请求采集,默认只采集失败的网络的请求的 URL 和状态码。基础库需 >= 3.4.1
newSessionCallback?: (param: { sessionId: string }) => void // 产生新的会话时的回调
autoSplitSession?: boolean // 是否自动分片
onReport?: (param: { timestamp: number; from: 'appservice' | 'webview' }) => void // 上报数据时的回调
errorCallback?: (param: {
type: number;
errMsg: string;
extra?: string;
}) => void; // 采集器内部发生错误时的回调
canvas?: boolean // 是否采集 canvas. default: false。 基础库 >= 3.2.2
canvasCollectInterval?: number // canvas 采集间隔,单位 ms. default: 100
}
ShouldCaptureNetworkParameter Dxplaination
Base Library > > 3.4.1
This parameter is an optional parameter:
- If not, the default collection of failed network request (initiated request failed or response status code > = 400 as a failure) URL and status code
- If transmitted, it is up to the developer to decide whether a web request collects & what content it collects
- Example of scenarios to customize: an interface needs to determine whether it succeeds or fails by packaging a specific field back; Additional response information is required, etc.
An incoming custom callback is triggered at the end of each network request. The callback receives a network request information as a parameter, typedINetworkRequestInfo(Structure below), the return value of the developer's callback must be one of three types:
False: Indicates that the network request is not collectedTrue: Indicates that the network request is captured by default (i.e. only on failure, only URL and status code)- An object of type
INetworkRequestInfo: Represents a developer's custom submission
INetworkRequestInfoStructure:
interface INetworkRequestInfo {
// 请求成功还是失败
success: boolean
// 请求的 URL
url: string
// 请求体
requestData: string
// 响应体
responseData: string
// 状态码
statusCode?: number
// 耗时
cost: number
// 请求失败时的错误信息
errMsg?: string
// 回调函数的返回值可传入的自定义标签,基础库 >= 3.4.2
custom?: Record<string, string>
}
If additional information is needed, the return value can be populated withcustomobjects, with the key-value pairs of type character string.
Return value
Promise<Result>
ResultStructure structure:
| field | type | Introductions |
|---|---|---|
| sessionId | string | Visual log Id, not empty at normal start of collection |
| errMsg | string | Error messages when startup exceptions |
# Get the current session ID
Interface:getSessionId
Gets the current session ID only when there is a value after a successful start of collection (you can callgetStatusto see the current collection status), and can be called in both automatic start and custom start modes.
parameter
nothing
Return value
Session ID (character string)
# Get the current collection status
Interface:getStatus(npm helper version > = 0.4.8)
parameter
nothing
Return value
A character string representing the collection status, enumerated as follows:
| value | Introductions |
|---|---|
| NotSetup | Not setup |
| SettingUp | Turn on the collection |
| WaitingForFirstReport | Among the first reported data collected |
| Collecting | Normal collection. |
| TearingDown | Stop collecting. |
# Stop collecting
Interface:teardown
await wx.obs.teardown()
parameter
nothing
return
Promise<void>
# Report custom events
Interface:event
Report a business-custom event, and after reporting, you can find the corresponding visualization log based on the event in the console, or perform crowd analysis / contrast analysis based on an event in thermodynamics & transformation analysis, etc.
wx.obs.event('add_cart', {
goods_id: '123',
price: 10,
})
parameter
| field | type | Introductions |
|---|---|---|
| eventName | string | The name of the incident. Length limit of 50 characters. |
| eventProperties | Object whose field value is a character string or integer | Event properties. A report attribute limit of 10, each attribute key needs to be less than 50 characters, value needs to be less than 255 characters, value is a number, please pass in the integer |
Tip: If you want to incorporate decimals, you can first convert them to integers, such as the product price is9.99yuan, multiplied by 100 to999reports in "fractions."
Return value
The return value of the function is whether it passes the argument check, if it does not pass it will returnfalse, andtrue
Example
Assuming that a user makes a purchase on your Weixin Mini Program, you can trigger a custom event (purchase) escalation with the following code
wx.obs.event('buy_goods',{
good_id: 'abcdefg123',
order_id: 'xxxxxxx',
price: 1024,
})
# Set custom id
Interface:setCustomId
Custom business id. After setting the statistical results can be more fit business situation, not set, the default is randomly generated and cached by the id statistics, the cache will be empty when the cache is clear or log out.
parameter
The custom id must be a character string and be less than 80 characters long.
Example
wx.obs.setCustomId('xxxx')
# Set custom properties
Interface:setCustomProperties
Customize the business attributes.Associated with a custom business id, so callsetCustomIdbefore calling this method.
parameter
Where key is a character string and value must be either a string or an integer. A limit of 10 attributes at a time, each attribute key must be less than 50 characters, value must be less than 255 characters, value is a number, please pass in the integer.
Example
wx.obs.setCustomProperties({
xxx: 'abc',
yyy: 100
})