Mini-program docs

Download and initialize the mini-program SDK, auto-collected events, manual APIs and metric definitions.

Scope

Mini-program analytics does not reuse the website's URL, referrer, PV or bounce definitions: sources are the host platform's scenes, pages are page routes (pages/…), data goes to a separate mini_events table, and the console has its own 13 report pages.

Steps

1
Add a mini programAdd it under "My mini programs" in the console to get an APP_KEY.
2
Add the SDK and initializePut the SDK in your project and initialize it at the top of app.js, before App().
3
Allow the domain and verifyAdd the collector domain to the request allowlist; open the mini program once and the connection check turns green.
4
Report business eventsReport orders, payments and other events with fmeng.track, then set up goals and funnels.

Download

The SDK is published on npm as fmeng-mini-sdk. It has no vendor dependencies and detects the WeChat wx / Alipay my / Douyin tt / Baidu swan runtime automatically. Install it with npm, then run Tools → Build npm in WeChat DevTools:

bash
npm install fmeng-mini-sdk

Projects without npm can download the file into the mini program (e.g. libs/fmeng-mini-sdk.js) and require that path instead:

About 4 KB gzipped, no dependencies.

Initialize

Initialize at the very top of app.js. It must run before App() so the SDK can wrap App and Page lifecycles:

javascript
// app.js
const { initMiniAnalytics } = require('fmeng-mini-sdk') // or './libs/fmeng-mini-sdk.js' when using the downloaded file
const fmeng = initMiniAnalytics({
appKey: 'APP_KEY', // copy from Settings → Setup code
endpoint: 'https://your-stats-domain/collect/mini',
// platform is inferred from the runtime; autoTrack is on by default
})
App({
fmeng, // attach to App; use getApp().fmeng in pages
onLaunch() {},
})
In production, add the collector domain to the request domain allowlist in the mini program admin console; in DevTools you can skip domain checks while testing.

Options: autoTrack (false to disable, or per item { app, page, share, error, performance }), appVersion (defaults to the release version), batchSize (events per request, default 10), flushInterval (batching delay in ms, default 3000), sessionTimeout (default 30 minutes of inactivity).

Auto-tracking

With auto-tracking on (the default), these events need no code:

EventFired whenKey fields
app_launchApp.onLaunchscene, query, page_route
app_showApp.onShow (cold start and returning to foreground)scene, query, page_route
app_hideApp.onHide, then sent immediatelyduration (foreground time)
page_viewPage.onShowpage_route, query
shareonShareAppMessage / onShareTimeline (only if the page defines them)share_source, page_route
errorApp.onError / onUnhandledRejectionmessage, stack, source
performancePage.onLoad → onReadypage_ready

Each request also carries device model, OS version, network type and screen size. Visitor and session IDs are kept in local storage; a new visit starts after 30 minutes of inactivity.

Manual APIs

Call through getApp().fmeng in pages. Event names are up to 64 characters, with up to 30 properties whose values are strings, numbers or booleans:

javascript
const fmeng = getApp().fmeng
// Business events: used by event analysis, goals and funnels
fmeng.track('add_cart', { sku: 'A100', price: 99 })
// Link a business user ID; pass null on logout
fmeng.identify('user-10086')
// In-app performance: first screen, request, setData, FPS (render time is automatic)
fmeng.performance('pages/goods/detail', { first_screen: 820, request: 180, set_data: 24, fps: 58 })
// Report an error manually
fmeng.error('Payment callback failed', { source: 'pay.js' })
// Manual lifecycle calls when auto-tracking is off
fmeng.appLaunch({ scene: options.scene, query: options.query })
fmeng.pageView('pages/index/index')
fmeng.appHide()

Frameworks & platforms

In Taro, uni-app and similar frameworks the framework calls App() / Page() itself; when the SDK cannot patch the globals, wrap the option objects with wrapApp / wrapPage:

javascript
// Taro (React)
import { initMiniAnalytics } from 'fmeng-mini-sdk'
export const fmeng = initMiniAnalytics({ appKey: 'APP_KEY', endpoint: 'https://your-stats-domain/collect/mini' })
// Works natively too: App(fmeng.wrapApp({ ... })), Page(fmeng.wrapPage({ ... }))

Alipay, Douyin and Baidu mini programs use the same SDK and the platform is detected from the runtime. Their scene systems differ, so reports show raw scene values (scene names and categories currently cover WeChat only).

Metrics

  • Opens: times the mini program came to the foreground (app_show); cold starts and returning from background both count.
  • Users: distinct users; new users first opened within the selected range.
  • Visits: sessions; opening again after 30 minutes of inactivity starts a new visit.
  • Avg. visit time: foreground time per visit (sum of app_hide durations; first-to-last event span when missing).
  • Bounce rate: visits with a single page view ÷ visits with page views.
  • Time on page: time until the next page view or backgrounding in the same visit, capped at 30 minutes.
  • Retention: share of new users (first open within the range) who open again on day N (N = 1–7, 14, 30).
  • Performance: durations are P75 (75% of samples are no slower); FPS is P25 (75% of samples are no lower).
Didn't find an answer?After signing in, check the announcements under the bell at the top right, or find our contact details on the About page.