Skip to content

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 $amplitude tracker
  • 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 ​

  1. Context enrichment: workspace_id, workspace_name, brand_name, instance_id, page_name, page_path, origin_url, environment on every event (caller values win), then normalizeIdProperties (see analyticsIds.ts).
  2. Token redaction: redactLoginTokens on event_properties and user_properties of every event.
  3. Anonymous public portals: the shared public-portal account ([email protected]) is never identified.
  4. PII hashing: identify replaces email and name with SHA-256 email_hash and name_hash.
  5. In-app referrer: referrer and referring_domain follow client-side route changes instead of the frozen document.referrer.
  6. Session replay: see amplitudeSessionReplay.ts.

SDK configuration ​

OptionValue
serverZoneUS
enableDiagnosticsfalse (and _setDiagnosticsSampleRate(0) when present)
minIdLength1
sessionTimeoutSESSION_TIMEOUT_MS = 30 minutes
trackingOptionsipAddress, language, platform: true
autocapture.pageViews, sessions, attribution, frustrationInteractions, webVitalstrue
autocapture.elementInteractionscssSelectorAllowlist: a, button, input, select, textarea, label, [role="button"], [data-amp-track]; viewportContentUpdated.enabled: false
autocapture.formInteractions, fileDownloads, pageUrlEnrichmentfalse
autocapture.networkTrackingignoreAmplitudeRequests: true; one capture rule for the app host and the apiBaseUrl host with statusCodeRange: '0,400-599' (failed requests only)
logLevelWarn

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:

#PatternReplacement
1/([?&](?:token|invitation_token|reset_token)=)[^&#]*/githe 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 stringAfter 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/42unchanged

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 without source.id, and does nothing for email === '[email protected]' (every public-portal visitor shares that account and would merge into one Amplitude user).
  • environment is portal on /shared-assets/ paths, anonymous when there is no user or the user is the public-portal account, otherwise portal.
  • identify(userId, props) calls setUserId, hashes email and name with SHA-256 (crypto.subtle; a 32-bit string hash is the fallback when that fails) and sends them as email_hash, name_hash.
  • A watch on the auth store user identifies on login and calls reset() (session replay shutdown, amplitude.reset()) when the user goes from set to null.
  • pagehide flushes the queue and shuts down session replay.

$amplitude API ​

MethodBehaviour
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 })