Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Elastic.Documentation.Site/Assets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
ATTR_CTA_URL,
ATTR_CTA_LABEL,
ATTR_CTA_LOCATION,
ATTR_EVENT_NAME,
ATTR_URL_PATH,
ATTR_URL_FULL,
} from './telemetry/semconv'
import { initTocNav } from './toc-nav'
import 'htmx-ext-head-support'
Expand Down Expand Up @@ -143,9 +145,19 @@ function ctaAttributes(cta: HTMLAnchorElement) {
[ATTR_CTA_LABEL]: cta.dataset.ctaLabel ?? '',
[ATTR_CTA_LOCATION]: cta.dataset.ctaLocation ?? '',
[ATTR_URL_PATH]: window.location.pathname,
[ATTR_URL_FULL]: window.location.href,
}
}

// Emit a CTA event: event.name mirrors the body so records can be filtered by
// exact keyword (attributes.event.name) instead of a text match on body.
function logCtaEvent(eventName: string, cta: HTMLAnchorElement) {
logInfo(eventName, {
[ATTR_EVENT_NAME]: eventName,
...ctaAttributes(cta),
})
}

let ctaImpressionObserver: IntersectionObserver | null = null

// Fires 'cta_viewed' the first time a CTA becomes at least half visible, then stops
Expand All @@ -157,10 +169,7 @@ function initCtaImpressions() {
(entries, observer) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue
logInfo(
'cta_viewed',
ctaAttributes(entry.target as HTMLAnchorElement)
)
logCtaEvent('cta_viewed', entry.target as HTMLAnchorElement)
observer.unobserve(entry.target)
}
},
Expand Down Expand Up @@ -210,7 +219,7 @@ document.addEventListener('click', function (event: MouseEvent) {
'a[data-cta]'
)
if (!cta) return
logInfo('cta_clicked', ctaAttributes(cta))
logCtaEvent('cta_clicked', cta)
})

// Don't remove style tags because they are used by the elastic global nav.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
ATTR_ERROR_TYPE,
ATTR_EXCEPTION_MESSAGE,
ATTR_URL_PATH,
ATTR_URL_FULL,
} from '@opentelemetry/semantic-conventions'

// ============================================================================
Expand Down
Loading