useTracker()
useTracker is the unified tracking entry — it auto-routes to the group WebSDK or the self-hosted channel based on the system/info.tracker config (#228), transparently to business code. Business code / components call useTracker().track(event, payload) to report custom events, never touching window.WebSDK directly — bypassing it loses the framework's gate (auto no-op when config isn't ready) and degradation (lazy-load / fault tolerance), and a raw call throws when config is missing.
How It Works
- Dual gate — reporting only happens when both of the following hold; missing either → silent no-op (no error):
ModuleOptions.tracker.enabledis truthy (master switch, defaulttrue)system/info.trackerhas either channel configured:endpoint(self-hosted channel, #228) orappId+encryptedConfig(group WebSDK channel) — see Tracking · reporting channels for behavior and precedence
This makes the "backend config not ready" case zero-special-handling: component tracking is still written, nothing is sent at runtime, and reporting starts automatically once the backend provides config. - Lazy-load + fault tolerance — on the WebSDK channel the first report triggers SDK lazy-loading; an unready SDK / a single failed event are swallowed (
console.error), never interrupting business logic (fire-and-forget). The self-hosted channel never loads WebSDK — events queue in memory and flush in batches (sendBeaconfirst,fetch keepalivefallback); see Tracking · reporting channels.
Return Value
| Name | Type | Description |
|---|---|---|
isEnabled | () => boolean | Whether tracking is enabled (master switch + backend config both ready) |
track | (event: WebSdkEvent | (string & {}), payload?: Record<string, unknown>) => void | Report one event (fire-and-forget; silent no-op when not enabled) |
event: Preset Enum + Custom Event Names (channel-dependent semantics)
event accepts the WebSdkEvent preset enum (keeps IDE autocomplete) or any custom string. The two reporting channels treat non-enum names differently:
- Self-hosted channel (
system/info.tracker.endpoint): custom event names are passed through and stored as-is (your backend owns the schema) — business events likelogin/sign_up/favorite/likecan be reported directly. Naming convention: snake_case, and don't duplicate a preset enum's meaning. - Group WebSDK channel: the SDK only accepts the 28 preset enums below; non-enum names get a dev-time console.warn and are ignored (silent in prod) — the whitelist is the SDK's constraint.
The 28 preset events the SDK actually supports:
| Category | Events |
|---|---|
| Page / Navigation | navigation, appPageView, appInstall, pageClick, appPageitemExposure, appPageitemClick |
| Recommendation | recommendListView, recommendListClick |
| Content | videoEvent, novelEvent, comicEvent |
| Search | keywordSearch, keywordClick |
| Ads (self-rendered) | advertising, adImpression, adClick |
| Ads (AdSDK) | adRequest, adFill, adNoFill, adLoadSuccess, adImpressionSdk, adClickSdk, adClose, adError |
| IM | imEvent, imConversationView, imConnectionSuccess, imMessageContent |
payloadfields vary per event and are validated by the SDK's internal schema; the framework keeps a singleRecord<string, unknown>type rather than refining each event.
Usage Examples
// Example 1: report a page view
const { track } = useTracker()
track('appPageView', { page_key: 'home', page_name: 'Home' })
// Example 2: report a search keyword
function onSearch(keyword: string) {
track('keywordSearch', { keyword })
}
// Example 3: check enablement before expensive payload prep
const { isEnabled, track } = useTracker()
if (isEnabled()) {
const payload = buildExpensivePayload()
track('videoEvent', payload)
}
Caveats
- Don't call
window.WebSDKdirectly — it bypasses the gate / degradation and throws when config is missing. Always go throughuseTracker().track(). - Don't hand-assemble ad tracking fields — use
useAdTrack(slot-level + creative-level fields auto-assembled), or just useSwAd*components (built-in impression / click tracking). - Reporting is fire-and-forget and returns no Promise; failures only
console.errorand never affect the business flow.
Related
useAdTrack— ad impression / click tracking wrapper (calls this composable internally)- Tracking Foundation — full event list and usage
- Module Options · tracker — tracking foundation switch config