Appearance
Amplitude Plugin ​
File Information ​
- Path:
app/plugins/amplitude.client.ts - Purpose: Initialise the Amplitude browser SDK, enrich every event with portal context, keep sign-in tokens and personal data out of analytics, and expose a
$amplitudetracker - Type: Client-only (
.client.ts),name: 'amplitude',enforce: 'post'
Overview ​
The plugin reads runtimeConfig.public.amplitudeApiKey. With an empty key nothing is initialised and every tracker method is a no-op. Otherwise it calls amplitude.init, registers one enrichment plugin (collage-context-enrichment) that runs on every event (including autocaptured ones), starts session replay once the SDK has a device and session id, and provides $amplitude.
Since commit 9704134 (security audit, PR #144) the enrichment plugin also redacts sign-in tokens from event_properties and user_properties as its last step. Before that, autocaptured page views and network events could carry a full sign-in link such as /acme/<token> or ?token=<token> into Amplitude.
Key Features ​
- Context enrichment:
workspace_id,workspace_name,brand_name,instance_id,page_name,page_path,origin_url,environmenton every event (caller values win), thennormalizeIdProperties(see analyticsIds.ts). - Token redaction:
redactLoginTokensonevent_propertiesanduser_propertiesof every event. - Anonymous public portals: the shared public-portal account (
[email protected]) is never identified. - PII hashing:
identifyreplacesemailandnamewith SHA-256email_hashandname_hash. - In-app referrer:
referrerandreferring_domainfollow client-side route changes instead of the frozendocument.referrer. - Session replay: see amplitudeSessionReplay.ts.
SDK configuration ​
| Option | Value |
|---|---|
serverZone | US |
enableDiagnostics | false (and _setDiagnosticsSampleRate(0) when present) |
minIdLength | 1 |
sessionTimeout | SESSION_TIMEOUT_MS = 30 minutes |
trackingOptions | ipAddress, language, platform: true |
autocapture.pageViews, sessions, attribution, frustrationInteractions, webVitals | true |
autocapture.elementInteractions | cssSelectorAllowlist: a, button, input, select, textarea, label, [role="button"], [data-amp-track]; viewportContentUpdated.enabled: false |
autocapture.formInteractions, fileDownloads, pageUrlEnrichment | false |
autocapture.networkTracking | ignoreAmplitudeRequests: true; one capture rule for the app host and the apiBaseUrl host with statusCodeRange: '0,400-599' (failed requests only) |
logLevel | Warn |
Enrichment pipeline ​
An error in the context step is swallowed ("enrichment failure must never drop the event"). The redaction runs after the try block, so it is applied even when enrichment failed.
Token redaction (redactLoginTokens) ​
Walks the value recursively: arrays item by item, plain objects value by value (keys are kept), other non-string values unchanged. Every string gets two replacements, in this order:
| # | Pattern | Replacement |
|---|---|---|
| 1 | /([?&](?:token|invitation_token|reset_token)=)[^&#]*/gi | the parameter name is kept, its value becomes [redacted] |
| 2 | /[^/?#&=]+/g (each run of characters between /, ?, #, &, =) | [redacted] when isLoginTokenSegment is true for that run, otherwise unchanged |
Query parameters redacted by name: token, invitation_token, reset_token. Path segments redacted by shape: the signed token (<n>.<n>.<n>. plus 64 hex) and the legacy token (32 or more hex characters).
| Input string | After redaction |
|---|---|
https://portal.example.com/acme/12.34.1767225600.<64 hex> | https://portal.example.com/acme/[redacted] |
/reset-password?token=abc123&lang=en | /reset-password?token=[redacted]&lang=en |
/generate-password?invitation_token=xyz#top | /generate-password?invitation_token=[redacted]#top |
/acme/folders/42 | unchanged |
Rule 2 is shape-based, so any other value that happens to be 32 or more hex characters (for example an MD5-style checksum stored in a property) is also replaced.
Identity rules ​
identifyFromUser(source)does nothing withoutsource.id, and does nothing foremail === '[email protected]'(every public-portal visitor shares that account and would merge into one Amplitude user).environmentisportalon/shared-assets/paths,anonymouswhen there is no user or the user is the public-portal account, otherwiseportal.identify(userId, props)callssetUserId, hashesemailandnamewith SHA-256 (crypto.subtle; a 32-bit string hash is the fallback when that fails) and sends them asemail_hash,name_hash.- A
watchon the auth store user identifies on login and callsreset()(session replay shutdown,amplitude.reset()) when the user goes from set tonull. pagehideflushes the queue and shuts down session replay.
$amplitude API ​
| Method | Behaviour |
|---|---|
track(eventName, props?, { immediate? }) | No-op before init or without a device id. Adds the context fields plus referrer and referring_domain, sanitises (depth 5, strings cut to 1000 characters, NaN and Infinity become null, functions dropped), cuts the name to 255 characters (unknown event when empty), then amplitude.track. immediate: true also flushes. |
identify(userId, userProperties?) | See identity rules. |
identifyFromUser(user) | Builds email, name, user_type: 'dealer', workspace_id, instance_id, brand_name, dealer_user_id, is_backend_user, screen_resolution and calls identify. |
setUserProperties(props) | Identify().set for every defined value. |
reset() | shutdownSessionReplay() then amplitude.reset(). |
flush(), getSessionId(), getDeviceId(), syncSessionReplaySessionId() | Thin wrappers. |
typescript
const { $amplitude } = useNuxtApp()
$amplitude?.track(AMPLITUDE_EVENTS.USER_PASSWORD_SET, { flow: 'reset' }, { immediate: true })Related Documentation ​
- isLoginTokenSegment - token shapes used for redaction
- Utils overview -
analyticsIds.ts,amplitudeSessionReplay.ts - Plugins overview