feat(metadata): send meta events via insights#6974
Conversation
Persist a telemetry session ID in sessionStorage so it survives page reloads within the same browser session.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 29 |
TIP This summary will be updated as you push new changes.
More templates
algoliasearch-helper
instantsearch-ui-components
instantsearch.css
instantsearch.js
react-instantsearch
react-instantsearch-core
react-instantsearch-nextjs
react-instantsearch-router-nextjs
vue-instantsearch
commit: |
Telemetry events now call insightsClientWithLocalCredentials directly to avoid triggering user-provided onEvent callbacks. Update call count expectations in shared insights tests to account for telemetry events.
b197b2a to
08f42af
Compare
Bootstrap telemetry event now includes two performance metrics: - timeSincePageLoad: ms since page navigation start (browser only) - timeSinceInit: ms since instantsearch() constructor was called
Aligns with the InstantSearch.start() method name.
84eb0c3 to
0234c11
Compare
| ), | ||
| application_id: appId, | ||
| performance: { | ||
| bootstrap_ms: Math.round( |
There was a problem hiding this comment.
explanation that this number is time between the constructor starting and the metadata start event being able to be sent, therefore measuring the time it takes to add widgets if they are added right at start.
…ware.ts Co-authored-by: Haroen Viaene <hello@haroen.me>
# Conflicts: # bundlesize.config.json # packages/instantsearch.js/src/middlewares/createMetadataMiddleware.ts
| return id; | ||
| } catch { | ||
| // sessionStorage unavailable (SSR, privacy mode, etc.) | ||
| return createUUID(); |
There was a problem hiding this comment.
I know this is something I wrote, just second guessing myself and would like your opinion.
Ideally, even on a private session/ssr, you'd like getUsageSessionId to always return the same uuid. Which isn't the case currently.
Also reading it from the session multiple while on the same runtime is unecessary slower.
Would it make sense to store it locally in a variable?
export function getUsageSessionId(): string {
let uuid = null
return (() => {
if (uuid) return uuid
....
})()
}
|
|
||
| import type { InsightsClientMethod, InsightsClientPayload } from '../types'; | ||
|
|
||
| const USAGE_SESSION_KEY = 'ais.usage.sessionId'; |
There was a problem hiding this comment.
@Haroenv I think you proposed to change the term USAGE to something different?
Summary
__start__and__render__events through the existing insights events pipeline, replacing the need to crawl every InstantSearch implementationextractWidgetPayloadinto a shared utility so both the old metadata middleware (pull-based) and the new telemetry (push-based) can reuse widget collection logicsessionStorageso it survives page reloads within the same browser sessiontimeSincePageLoad(browser) andtimeSinceInit(universal)How it works
Telemetry is embedded in the insights middleware. When insights is active, two events are sent:
__start__started()__render__emit('render')Events flow through
sendEventToInsightsusinginsightsMethod: 'sendEvents', leveraging the search-insights library transport. This avoids direct HTTP calls and provides ad-blocker resilience.The old metadata middleware (crawler-based) is unchanged and still active.
Event shape
{ "eventType": "instantsearch_telemetry", "eventName": "__start__", "timestamp": 1710691200000, "session_id": "f47ac10b-...", "userToken": "...", "performance": { "timeSincePageLoad": 1234, "timeSinceInit": 87 }, "widgets": [...] }Performance metrics (
__start__only)timeSincePageLoadperformance.now()timeSinceInitDate.now()deltainstantsearch()constructor to boot