Skip to content
Closed
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
12 changes: 12 additions & 0 deletions app/cloudflare-analytics.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CLOUDFLARE_WEB_ANALYTICS_TOKEN } from '#shared/rum-beacon'

export default defineNuxtPlugin({
env: { islands: false },
setup() {
useScript({
'src': 'https://static.cloudflareinsights.com/beacon.min.js',
'data-cf-beacon': JSON.stringify({ token: CLOUDFLARE_WEB_ANALYTICS_TOKEN, spa: true }),
'crossorigin': false,
}, { bundle: true, trigger: 'server' })
},
})
16 changes: 10 additions & 6 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { withBuildAssetMissFallthrough } from './scripts/lib/static-asset-fallth
import { externalCheckin } from './shared/checkin-external.ts'
import { iconifyCollections } from './shared/icon-collections.ts'
import { REGISTRY_SKILL } from './shared/llms-txt.ts'
import { CLOUDFLARE_WEB_ANALYTICS_TOKEN } from './shared/rum-beacon.ts'
import { SENTRY_DSN, sentryRelease, sentryReportingEnabled } from './shared/sentry.ts'
import { SESSION_NAME } from './shared/server/session-access.ts'
import { CLI_INSTALL_SCRIPTS } from './shared/skill-commands.ts'
Expand Down Expand Up @@ -249,6 +250,9 @@ export default defineNuxtConfig({
// first rule. Skill, repo and category pages follow once it holds.
},

// Keep the beacon in the production HTML and its registration out of the client bundle.
plugins: process.env.NODE_ENV === 'production' ? ['~/cloudflare-analytics.server.ts'] : [],

scripts: {
// Bundling a registry script downloads it at build time. When
// static.cloudflareinsights.com is unreachable, that download threw and
Expand All @@ -263,17 +267,17 @@ export default defineNuxtConfig({
// static.cloudflareinsights.com at transform time, so every vitest file
// fails on the network-less CI runner (#204). Telemetry serves no test,
// and vitest sets NODE_ENV=test before nuxt.config.ts is evaluated.
...(process.env.NODE_ENV === 'test'
? {}
: {
...(process.env.NODE_ENV === 'development'
? {
cloudflareWebAnalytics: {
token: 'fefd4b7eafe04d5f81621e43e5d5ef80',
token: CLOUDFLARE_WEB_ANALYTICS_TOKEN,
// RUM cannot collect Worker subrequests. Send events from the browser.
// https://developers.cloudflare.com/web-analytics/faq/#can-i-use-real-user-monitoring-rum-with-cloudflare-workers
proxy: false,
trigger: 'server',
trigger: 'client',
},
}),
}
: {}),
},
},

Expand Down
2 changes: 2 additions & 0 deletions shared/rum-beacon.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const CLOUDFLARE_WEB_ANALYTICS_TOKEN = 'fefd4b7eafe04d5f81621e43e5d5ef80'

/**
* Timing entries carry their page or resource URL in `name`, so it scrubs
* like any other URL key at every nesting depth.
Expand Down
Loading