Skip to content
简体中文

Mini Program event tracking

Tracking depends on the product. The Mini Program SDK exposes lifecycle hooks and a reporting method so you can define the events your application needs.

The SDK wraps common native events. See the Mini Program hooks for signatures and examples.

Use pageOnShow and pageOnHide to count page views and measure how long a page stays visible. The original tracking guide describes pageOnShow as running after the native callback. Check callback ordering against the version you use.

Record startTime and the current route when the page becomes visible. When the user leaves, subtract the start time from Date.now() and report the duration with the previous route.

const MitoInstance = MITO.init({
pageOnShow,
pageOnHide
})
wx.MitoInstance = MitoInstance
const currentPage = {
startTime: 0,
page: null
}
function pageOnShow(page) {
// Track entry into the page.
wx.MitoInstance.trackSend({
// Application-defined value.
actionType: 'PAGE',
route: page.route
})
currentPage.startTime = Date.now()
currentPage.page = page
}
function pageOnHide(page) {
// Track departure from the page.
const endTime = Date.now()
const elapsedTime = endTime - currentPage.startTime
// Send the recorded values.
console.log('currentPage', currentPage)
wx.MitoInstance.trackSend({
// Application-defined value.
actionType: 'DURATION',
// Exposure time.
elapsedTime,
// Page route.
route: currentPage.page.route
})
}

WeChat Mini Programs do not expose a DOM tagName or a full domPath for the tapped element. This makes it difficult to identify buttons uniquely for automatic tracking. You can share approaches with the maintainer through the WeChat contact page.

Teams can define their own event taxonomy. This example distinguishes page views, interaction events, area impressions, time on a page, time in an area and custom events.

export enum TrackActionType {
// Page view.
PAGE = 'PAGE',
// Interaction event.
EVENT = 'EVENT',
// Area impression.
VIEW = 'VIEW',
// Duration event.
DURATION = 'DURATION',
// Duration of an area impression.
DURATION_VIEW = 'DURATION_VIEW'
// Other application-defined events.
OTHER = 'OTHER'
}

Call trackSend on the initialized SDK instance to submit an event.

// main.js
import { init } from '@mitojs/wx-mini'
const MitoInstance = init({
// Enable debug during development; set it to false in production.
debug:true,
dsn: 'https://test.com/yourInterface',
})
MitoInstance.trackSend({
// uuid
id?: string
// An event code assigned by the application.
trackId?: string
// An extensible event type.
actionType?: TrackActionType | any
// Event start time.
startTime?: number
// Time spent.
durationTime?: number
// Report time.
trackTime?: number
// Defaults to true for tracking events, which skip errorId generation.
isTrack?: boolean
// anything for developer
...
})

Ordinary error reports generate an errorId. The tracking method sets isTrack: true so the SDK skips error ID generation.