Skip to content

Commit eacb56a

Browse files
committed
Add Vercel Analytics
1 parent 39578d3 commit eacb56a

4 files changed

Lines changed: 52 additions & 22 deletions

File tree

_TODO.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,6 @@ Implement mitigations in test/e2e/specs/07-performance/PERFORMANCE.md
7171

7272
Right now we're using string literals to define HTML email templates for site mails. We should use Nunjucks with the rule-checking for valid CSS in HTML emails like we have in the corporate email footer repo.
7373

74-
## Analytics
75-
76-
Vercel Analytics
77-
78-
- Highlighter component
79-
- Social Shares component
80-
- Social Embeds: Track embed interactions
81-
- Cookie Consent
82-
- Download Form component
83-
84-
npm i @vercel/analytics
85-
import Analytics from '@vercel/analytics/astro'
86-
https://vercel.com/docs/analytics/quickstart#add-the-analytics-component-to-your-app
87-
8874
## Set up webmentions
8975

9076
Needs to add real API key and test
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
import VercelAnalytics from '@vercel/analytics/astro'
3+
import { isProd } from '@components/scripts/utils/environmentClient'
4+
5+
const prod = isProd()
6+
const mode = prod ? 'production' : 'development'
7+
const debug = !prod
8+
---
9+
10+
<script>
11+
window.webAnalyticsBeforeSend = (event) => {
12+
/** Ignore testing pages in tracking */
13+
if (event?.url?.includes('/testing/')) {
14+
return null
15+
}
16+
return event
17+
}
18+
</script>
19+
<VercelAnalytics
20+
debug={debug}
21+
mode={mode}
22+
/>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
/**
3+
* Prefetch offline page during idle time for service worker
4+
*/
5+
---
6+
7+
<script>
8+
import { handleScriptError } from '@components/scripts/errors/handler'
9+
10+
const prefetch = (url: string) => {
11+
try {
12+
fetch(url, { credentials: 'same-origin' }).catch((error) => {
13+
handleScriptError(error, { scriptName: 'BaseLayout', operation: `prefetch ${url}` })
14+
})
15+
} catch (error) {
16+
handleScriptError(error, { scriptName: 'BaseLayout', operation: `prefetch ${url}` })
17+
}
18+
}
19+
if (typeof requestIdleCallback === 'function') {
20+
requestIdleCallback(() => prefetch('/offline'))
21+
} else {
22+
setTimeout(() => prefetch('/offline'), 500)
23+
}
24+
</script>

src/layouts/BaseLayout.astro

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
---
2+
import Analytics from '@components/Analytics/index.astro'
23
import Breadcrumbs from '@components/Breadcrumbs/index.astro'
34
import Container from '@components/Layout/Container/index.astro'
45
import CookieConsent from '@components/Consent/Banner/index.astro'
56
import Footer from '@components/Footer/index.astro'
67
import HeadContent from '@components/Head/index.astro'
78
import Header from '@components/Header/index.astro'
89
import Content from '@components/Layout/Content/index.astro'
10+
import PrefetchOfflinePage from '@components/Pwa/PrefetchOfflinePage/index.astro'
911
import PwaTitleBar from '@components/Pwa/TitleBar/index.astro'
1012
import RegisterServiceWorker from '@components/Pwa/ServiceWorker/index.astro'
1113
import Skip from '@components/Layout/Skip/index.astro'
@@ -105,16 +107,12 @@ const {
105107
</Content>
106108
<Footer />
107109
</Container>
110+
{/** Vercel Analytics */ }
111+
<Analytics />
108112
{/* Register the service worker via @vite-pwa/astro virtual module */}
109113
<RegisterServiceWorker />
110-
<script>
111-
const prefetch = (url: string) => fetch(url, { credentials: 'same-origin' }).catch(() => {})
112-
if (typeof requestIdleCallback === 'function') {
113-
requestIdleCallback(() => prefetch('/offline'))
114-
} else {
115-
setTimeout(() => prefetch('/offline'), 500)
116-
}
117-
</script>
114+
{/* Prefetch offline page during idle time for service worker */}
115+
<PrefetchOfflinePage />
118116
</body>
119117
</>
120118
</html>

0 commit comments

Comments
 (0)