WeChat
Install
The package is packages/miniprogram. Depend on it from the workspace. The import path is the export in package.json:
import { WebCount } from '@webcount/miniprogram/wx';The sample project is examples/miniprogram/weixin/.
Initialize
Set appId to the mini program appid and register that id in the console first. endpoint is the host only; the SDK appends the path.
WebCount.preInit({
siteKey: 'YOUR_SITE_KEY',
endpoint: 'https://YOUR_ORIGIN',
appId: 'YOUR_APPID',
});
App({
onLaunch() {
WebCount.init();
},
});For China mode, pass region: 'cn'. Call init after the user agrees, and call grantConsent(['analytics']), or no events are sent.
Pageviews
After preInit, a cold start sends $mp_launch and $mp_show, and showing and hiding a page sends $mp_view_screen and $mp_page_leave. If the framework must not patch global Page, wrap the page options with wrapPage from the same entry. Send the current screen yourself with trackScreen.
Events
WebCount.track('purchase', { revenue: 19.9, currency: 'CNY' });Event names are at most 100 characters. Custom properties are at most 30, and string values at most 200 characters.
Identity
WebCount.login(openid, undefined, { type: 'openid' });
WebCount.logout();Login ids are at most 200 characters. A device id is written only in identify mode. See Identify users.
Limits
| Item | Default | What it means |
|---|---|---|
| Local queue | 10,000 events | Also capped at 7 days and 1 MB |
| Clicks | Off | Sent only when autoTrack: true |
| Application id | Required | An unregistered appid is dropped by the collector |
Errors
A missing siteKey fails init, records an internal error, and does not send a launch event. An unregistered appid is dropped by the collector, with no exception at the call. If the collection host is not in the mini program's allowed request domains, requests never leave the device. 429, 5xx, and network failures back off from 1 second, capped at 300 seconds, while events wait in the local queue. login is ignored when the id is longer than 200 characters or is a placeholder. Capabilities are compared in SDK overview.
Alipay
Install
import { WebCount } from '@webcount/miniprogram/my';The source is packages/miniprogram. For call order, see the WeChat sample in examples/miniprogram/weixin/.
Initialize
WebCount.preInit({
siteKey: 'YOUR_SITE_KEY',
endpoint: 'https://YOUR_ORIGIN',
appId: 'YOUR_APPID',
});
App({
onLaunch() {
WebCount.init();
},
});appId is the Alipay mini program appid, registered in the console. For China mode, pass region: 'cn', call init after the user agrees, and call grantConsent(['analytics']).
Pageviews
The lifecycle bound in preInit sends $mp_launch, $mp_show, and $mp_hide. Showing and hiding a page sends $mp_view_screen and $mp_page_leave, and sharing sends $mp_share. Do not send these names yourself. If global Page must not be patched, use wrapPage from the same entry. Send a screen yourself with trackScreen.
Events
WebCount.track('purchase', { revenue: 19.9 });Event names are at most 100 characters. Custom properties are at most 30, and string values at most 200 characters.
Identity
WebCount.login(userId, undefined, { type: 'openid' });type may be login, email, mobile, unionid, openid, or custom. Email and mobile are hashed first. Ids are at most 200 characters, and placeholders are rejected. Sign-out is logout.
Limits
| Item | Default | What it means |
|---|---|---|
| Local queue | 10,000 events | Also cut at 7 days or 1 MB |
| Clicks | Off | On only with autoTrack: true |
| Application id | Required | Unregistered ids are dropped |
Errors
Without siteKey, collection does not start and there is no $mp_launch. An unregistered appid is dropped. Add the collection host to the mini program's allowed request domains. Failed requests stay in the local queue and retry with backoff. A page that never hits the lifecycle bound in preInit has no automatic $mp_view_screen. Use wrapPage or trackScreen then. See SDK overview.
Douyin
The core matches the WeChat and Alipay entries.
Install
import { WebCount } from '@webcount/miniprogram/tt';The package directory is packages/miniprogram.
Initialize
WebCount.preInit({
siteKey: 'YOUR_SITE_KEY',
endpoint: 'https://YOUR_ORIGIN',
appId: 'YOUR_APPID',
});
App({
onLaunch() {
WebCount.init();
},
});appId is the Douyin mini program appid, registered in the console. endpoint is the host only; the SDK appends the path. For China mode, pass region: 'cn', call init after the user agrees, and call grantConsent(['analytics']).
Pageviews
Launch sends $mp_launch then $mp_show. Going to the background sends $mp_hide. Entering a page sends $mp_view_screen and leaving sends $mp_page_leave. Sharing sends $mp_share and adding to favorites sends $mp_add_favorites. The lifecycle sends these preset names, so business code should use its own event names. Use wrapPage when global Page must not be patched. You can also call trackScreen for the current screen.
Events
WebCount.track('purchase', { revenue: 19.9, currency: 'CNY' });Names are at most 100 characters. Custom properties are at most 30, and string values at most 200 characters.
Identity
WebCount.login(openid, undefined, { type: 'openid' });
WebCount.logout();Login ids are at most 200 characters, and placeholders are ignored. See Identify users.
Limits
| Item | Default | What it means |
|---|---|---|
| Local queue | 10,000 events | Also capped at 7 days and 1 MB |
| Clicks | Off | Collected only when autoTrack is on |
| Application id | Required | An unregistered appid is dropped |
Errors
An empty siteKey records an internal error at init and sends nothing, so realtime has no $mp_launch. If there is no launch, first check that init actually ran. An unregistered appid is dropped. Add the collection host to the request allowlist. A failed batch stays on device. 429 and 5xx back off from 1 second, capped at 300 seconds. If global Page cannot be patched and you also skip wrapPage, entering a page does not send $mp_view_screen automatically. See SDK overview.