Hooks and page views
Use the React context API and understand automatic route events and provider cleanup.
@askdepth/reactv0.1.0On this page
useAskdepth
useAskdepth() returns an AskdepthContextValue. Outside a provider it returns safe no-op functions, so an accidentally unwrapped component does not throw but also does not collect data.
| Method | Result |
|---|---|
track(name, properties?) | void; forwards to core. |
identify(userId, traits?) | void; forwards to core. |
getTraceparent() | Header string or null. |
isReady() | Boolean indicating whether core collectors are listening. |
getSessionId() | Session UUID or null. |
'use client';
import { useAskdepth } from '@askdepth/react';
export function SaveButton() {
const analytics = useAskdepth();
return (
<button
type="button"
onClick={() => {
analytics.track('draft_save_requested', { source: 'toolbar' });
}}
>
Save draft
</button>
);
}isReady is a function, not a reactive status field. Calling it reads current core state; consent changes do not guarantee a context re-render. Subscribe with Askdepth.onConsentChange from core if your interface needs its own consent state.
The context does not include setConsent or revokeConsent. Import those from @askdepth/core when connecting a consent control.
Automatic page views
The provider emits a track event named page_view with a url property on initial mount and history changes. The URL is a sanitized pathname, without origin, query string, or fragment.
Common static route segments such as dashboard, settings, checkout, and products are retained. Other segments become :redacted, including product-specific static names that are not in the built-in allowlist.
/dashboard/users/user_47?tab=profile#details
→ /dashboard/users/:redactedThe input pathname is bounded to 4,096 characters and twelve segments, with :truncated appended when needed. There is no public custom pathname allowlist in this release. Use your own fixed event name/property when you need a product-specific view classification.
When mounted without active collection, the adapter remembers that its initial page view is pending and attempts it when consent becomes granted. Other events that occur before consent are not buffered for later collection.
Mounts and cleanup
Equivalent core configuration does not attach another core listener set. The adapter reference-counts route tracking, while each provider registers its component resolver and interaction tracking.
Unmounting a provider unregisters those adapter listeners. It does not stop or revoke consent for the core singleton, which can remain active after the provider unmounts. Keep the provider mounted at a stable application boundary. Apply the visitor's consent withdrawal through the core API.
Multiple providers with different options reconfigure the same core runtime; they are not independent projects or independently sampled collectors. React Strict Mode and your router can cause lifecycle sequences beyond a single mount, so check for duplicate page views in your application.
