Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

feat(docs): send copy and search events to Google Analytics (#5990)

Zayn Jarvis0f8c5c538ceb
diff --git a/docs/.vitepress/theme/CopyMarkdownButton.vue b/docs/.vitepress/theme/CopyMarkdownButton.vue
index 56cf471e6..0933ae712 100644
--- a/docs/.vitepress/theme/CopyMarkdownButton.vue
+++ b/docs/.vitepress/theme/CopyMarkdownButton.vue
@@ -2,6 +2,7 @@
import { useData, withBase } from 'vitepress'
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
import { hasPageLlmsTxt } from './llms-txt'
+import { trackEvent } from './track'
const { frontmatter, page } = useData()
@@ -36,6 +37,7 @@ async function copyToClipboard() {
const md = decodeURIComponent(frontmatter.value._rawMarkdownEncoded ?? '')
if (!md) return
await navigator.clipboard.writeText(md)
+ trackEvent('copy-markdown')
copied.value = true
setTimeout(() => { copied.value = false }, 2000)
}
diff --git a/docs/.vitepress/theme/components/AgentPrompt.vue b/docs/.vitepress/theme/components/AgentPrompt.vue
index 77035bf08..3784e4c96 100644
--- a/docs/.vitepress/theme/components/AgentPrompt.vue
+++ b/docs/.vitepress/theme/components/AgentPrompt.vue
@@ -3,6 +3,7 @@
// without opening it. The prompt stays in the page DOM (and in llms.txt).
import { computed, ref } from 'vue'
import { useData } from 'vitepress'
+import { trackEvent } from '../track'
const props = defineProps<{ label?: string }>()
const { lang } = useData()
@@ -30,6 +31,7 @@ async function copy() {
const code = root.value?.querySelector('pre code')
if (!code) return
await writeText((code.textContent ?? '').replace(/\n$/, ''))
+ trackEvent('copy-agent-prompt')
copied.value = true
clearTimeout(timer)
timer = setTimeout(() => { copied.value = false }, 2000)
diff --git a/docs/.vitepress/theme/index.ts b/docs/.vitepress/theme/index.ts
index 99d410c54..de96841a2 100644
--- a/docs/.vitepress/theme/index.ts
+++ b/docs/.vitepress/theme/index.ts
@@ -20,7 +20,7 @@ import CopyMarkdownButton from './CopyMarkdownButton.vue'
import OpenVikingSearch from './OpenVikingSearch.vue'
import ApiExampleTabsEnhancer from './ApiExampleTabsEnhancer.vue'
import { initVikingBotWidget, syncVikingBotLocale } from './vikingbot-widget'
-import { trackPageView } from './track'
+import { trackEvent, trackPageView } from './track'
import { startDocsAttribution } from './attribution'
import './custom.css'
import './reading.css'
@@ -295,6 +295,12 @@ export default {
}
}
trackPageView(window.location.pathname)
+ document.addEventListener('click', (event) => {
+ const block = (event.target as Element).closest?.('.vp-doc div[class*="language-"] > button.copy')?.parentElement
+ if (!block) return
+ const language = [...block.classList].find(name => name.startsWith('language-'))?.slice('language-'.length) ?? ''
+ trackEvent('copy-code', { language })
+ })
initVikingBotWidget()
const previousHook = router.onAfterRouteChanged
diff --git a/docs/.vitepress/theme/track.ts b/docs/.vitepress/theme/track.ts
index c261c1d97..507aa578f 100644
--- a/docs/.vitepress/theme/track.ts
+++ b/docs/.vitepress/theme/track.ts
@@ -14,13 +14,15 @@
*
* Beacons are fire-and-forget: failures are swallowed and never surface to the
* page.
+ *
+ * Events are also sent to Google Analytics when gtag is loaded (see config.ts).
*/
const COUNTER_URL = (
(import.meta.env.VITE_COUNTER_URL as string | undefined) ?? ''
).replace(/\/+$/, '')
-export type TrackEventName = 'docs-search'
+export type TrackEventName = 'docs-search' | 'copy-code' | 'copy-agent-prompt' | 'copy-markdown'
function resolveSite(): string {
if (typeof window === 'undefined') return 'docs'
@@ -82,7 +84,16 @@ export function trackPageView(pathname: string): void {
beacon(pathname, ref)
}
-/** Report a user action as an /e/ pseudo-page. */
-export function trackEvent(name: TrackEventName): void {
+/** Report a user action as an /e/ pseudo-page and a GA event (GA names allow no hyphens). */
+export function trackEvent(name: TrackEventName, params?: Record<string, string>): void {
beacon(`/e/${name}`)
+ try {
+ window.gtag?.('event', name.replaceAll('-', '_'), params)
+ } catch {
+ // Tracking must never break the page.
+ }
+}
+
+declare global {
+ interface Window { gtag?: (...args: unknown[]) => void }
}