Skip to content
简体中文

WeChat Mini Program SDK

Terminal window
# Using npm
npm i @mitojs/wx-mini
# Using Yarn
yarn add @mitojs/wx-mini
// main.js
import { init } from '@mitojs/wx-mini'
// Multiple SDK instances
const MitoInstance = init({
// Enable debug during development; set it to false in production.
debug:true,
dsn: 'https://test.com/yourInterface',
maxBreadcrumbs: 100,
// Configure the request headers for reports.
configReportWxRequest(data) {
return {
header: {
'Content-Type': 'text/plain; charset=UTF-8'
},
dataType: 'text'
}
},
})

For a native WeChat Mini Program, download the SDK bundle as a JavaScript file, then import it in app.js as shown below.

The local SDK file imported in a WeChat Mini Program

The SDK also works with Vue-based frameworks such as uni-app.

Terminal window
npm i @mitojs/wx-mini @mitojs/vue
import { init } from '@mitojs/wx-mini'
import { vuePlugin } from '@mitojs/vue'
import Vue from 'vue'
const MitoInstance = init({
dsn: 'https://test.com/yourInterface',
maxBreadcrumbs: 100,
// Configure the request headers for reports.
configReportWxRequest(data) {
return {
header: {
'Content-Type': 'text/plain; charset=UTF-8'
},
dataType: 'text'
}
},
vue:Vue
},[vuePlugin])

See the Vue integration guide for more examples.

Terminal window
npm i @mitojs/wx-mini @mitojs/react

See the React integration guide for more examples.

These options and hooks are specific to @mitojs/wx-mini. The basic configuration applies to both Mini Programs and the browser SDK.

Option Type Default Effect when enabled
silentRequest boolean false Stops monitoring requests and downloads made by the Mini Program.
silentConsole boolean false Disables console monitoring. The SDK wraps the native console. The original guide recommends enabling this option during testing and leaving it off in production.
silentDom boolean false Stops recording tap and touch events. By default, elements with an ID or class can appear in the breadcrumb trail.
silentRoute boolean false Stops monitoring page navigation.
silentAppOnError boolean false Stops collecting App.onError events.
silentAppOnUnhandledRejection boolean false Stops collecting App.onUnhandledRejection events.
silentPageOnShareAppMessage boolean false Stops monitoring page shares. The default enables the page share feature.
silentPageOnShareTimeline boolean false Stops monitoring shares to the timeline. The default enables the page share feature.

configReportWxRequest(event: TransportDataType | any): Partial<WechatMiniprogram.RequestOption>

Section titled “configReportWxRequest(event: TransportDataType | any): Partial<WechatMiniprogram.RequestOption>”
/**
* Configure wx.request when a Mini Program sends a report.
*
* @param {(TransportDataType | any)} event Data about to be reported.
* @return {*} {Partial<WechatMiniprogram.RequestOption>}
* @memberof WxHookOptionsType
*/

Set the Content-Type header on the outgoing report.

MITO.init({
...
configReportWxRequest(event){
return {
header: {
'Content-Type': 'text/plain; charset=UTF-8'
},
dataType: 'text'
}
}
})

appOnLaunch(options: WechatMiniprogram.App.LaunchShowOption): void

Section titled “appOnLaunch(options: WechatMiniprogram.App.LaunchShowOption): void”
/**
* Runs after the native App.onLaunch callback.
*
* @param {WechatMiniprogram.App.LaunchShowOption} options
* @memberof WxHookOptionsType
*/

Log the arguments received at application launch.

MITO.init({
...
appOnLaunch(options){
console.log('mito appOnLaunch', options)
}
})

appOnShow(options: WechatMiniprogram.App.LaunchShowOption): void

Section titled “appOnShow(options: WechatMiniprogram.App.LaunchShowOption): void”
/**
* Runs after the native App.onShow callback.
*
* @param {WechatMiniprogram.App.LaunchShowOption} options
* @memberof WxHookOptionsType
*/

Inspect the options passed when the application becomes visible.

MITO.init({
...
appOnShow(options){
console.log('mito appOnShow', options)
}
})
/**
* Runs after the native App.onHide callback.
*
* @memberof WxHookOptionsType
*/

Log when the application is hidden.

MITO.init({
...
appOnHide(){
console.log('mito appOnHide', options)
}
})

appOnPageNotFound(data: WechatMiniprogram.OnPageNotFoundCallbackResult): void

Section titled “appOnPageNotFound(data: WechatMiniprogram.OnPageNotFoundCallbackResult): void”
/**
* Runs after the native onPageNotFound callback.
*
* @param {WechatMiniprogram.OnPageNotFoundCallbackResult} data
* @memberof WxHookOptionsType
*/

Inspect the missing-page event.

MITO.init({
...
appOnPageNotFound(data){
console.log('mito appOnPageNotFound', data)
}
})
/**
* Runs before the native page load callback.
*
* @param {IWxPageInstance} page
* @memberof WxHookOptionsType
*/

Inspect the page instance on load.

MITO.init({
...
pageOnLoad(page){
console.log('mito pageOnLoad', page)
}
})
/**
* Runs before the native Page.onShow callback.
*
* @param {IWxPageInstance} page
* @memberof WxHookOptionsType
*/

Log the page instance when it becomes visible.

MITO.init({
...
pageOnShow(page){
console.log('mito pageOnShow', page)
}
})
/**
* Runs before the native page ready callback.
*
* @param {IWxPageInstance} page
* @memberof WxHookOptionsType
*/

Inspect the page instance when it becomes ready.

MITO.init({
...
pageOnReady(page){
console.log('mito pageOnReady', page)
}
})
/**
* Runs after the native page unload callback.
*
* @param {IWxPageInstance} page
* @memberof WxHookOptionsType
*/

Log the page being unloaded.

MITO.init({
...
pageOnUnload(page){
console.log('mito pageOnUnload', page)
}
})
/**
* Runs before the native Page.onHide callback.
*
* @param {IWxPageInstance} page
* @memberof WxHookOptionsType
*/

Inspect the page being hidden.

MITO.init({
...
pageOnHide(page){
console.log('mito pageOnHide', page)
}
})

pageOnShareAppMessage(options: WechatMiniprogram.Page.IShareAppMessageOption,page: IWxPageInstance): void

Section titled “pageOnShareAppMessage(options: WechatMiniprogram.Page.IShareAppMessageOption,page: IWxPageInstance): void”
/**
* Runs before the native page share callback.
*
* @param {(WechatMiniprogram.Page.IShareAppMessageOption & IWxPageInstance)} options
* @memberof WxHookOptionsType
*/

Inspect the share options and the current page.

MITO.init({
...
pageOnShareAppMessage(options, page){
console.log('mito pageOnShareAppMessage', options)
}
})

pageOnShareTimeline(options: WechatMiniprogram.Page.IShareAppMessageOption, page: IWxPageInstance): void

Section titled “pageOnShareTimeline(options: WechatMiniprogram.Page.IShareAppMessageOption, page: IWxPageInstance): void”
/**
* Runs before the native Page.onShareTimeline callback.
*
* @param {IWxPageInstance} page
* @memberof WxHookOptionsType
*/

Log the options and page for a timeline share.

MITO.init({
...
pageOnShareTimeline(options, page){
console.log('mito pageOnShareTimeline', options, page)
}
})

pageOnTabItemTap(options: WechatMiniprogram.Page.ITabItemTapOption,page: IWxPageInstance): void

Section titled “pageOnTabItemTap(options: WechatMiniprogram.Page.ITabItemTapOption,page: IWxPageInstance): void”
/**
* Runs before the native Page.onTabItemTap callback.
*
* @param {(WechatMiniprogram.Page.ITabItemTapOption & IWxPageInstance)} options
* @memberof WxHookOptionsType
*/

Inspect the tab item and the page receiving the tap.

MITO.init({
...
pageOnTabItemTap(options){
console.log('mito pageOnTabItemTap', options)
}
})

wxNavigateToMiniProgram(options: WechatMiniprogram.NavigateToMiniProgramOption): WechatMiniprogram.NavigateToMiniProgramOption

Section titled “wxNavigateToMiniProgram(options: WechatMiniprogram.NavigateToMiniProgramOption): WechatMiniprogram.NavigateToMiniProgramOption”
/**
* Wraps navigation to another Mini Program so you can change the query and extraData.
*
* @param {WechatMiniprogram.NavigateToMiniProgramOption} options
* @return {*} {WechatMiniprogram.NavigateToMiniProgramOption}
* @memberof WxHookOptionsType
*/

Modify navigation options before opening another Mini Program. The example adds a timestamp to the query.

MITO.init({
...
wxNavigateToMiniProgram(options){
options.path = `${options.path}?time=${Date.now()}`
return options
}
})

triggerWxEvent(e: WechatMiniprogram.BaseEvent): void

Section titled “triggerWxEvent(e: WechatMiniprogram.BaseEvent): void”
/**
* Wraps action handlers and passes their first argument to this hook.
*
* Check e.type to identify a tap.
*
* @param {WechatMiniprogram.BaseEvent} e The event passed to the handler.
* @memberof WxHookOptionsType
*/

Inspect a tap and read its currentTarget.

MITO.init({
...
triggerWxEvent(e){
// You can also check for touchmove and other events.
if (e.type === 'tap'){
// Read the tapped target and its dataset, including any class or ID recorded there.
console.log(e.currentTarget)
}
}
})