Skip to content

Commit 7bedc8e

Browse files
committed
refactor: extract Google Analytics into separate Analytics.astro component
1 parent 5357916 commit 7bedc8e

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
const GA_ID = import.meta.env.PUBLIC_GA_ID;
3+
---
4+
5+
{GA_ID && (
6+
<>
7+
<script
8+
async
9+
src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`}
10+
></script>
11+
<script define:vars={{ GA_ID }}>
12+
window.dataLayer = window.dataLayer || [];
13+
function gtag() { dataLayer.push(arguments); }
14+
gtag('js', new Date());
15+
gtag('config', GA_ID);
16+
</script>
17+
</>
18+
)}

docs/src/layouts/BaseLayout.astro

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import '../styles/globals.css';
3+
import Analytics from '../components/Analytics.astro';
34
45
interface Props {
56
title?: string;
@@ -11,8 +12,6 @@ const {
1112
description = 'High-performance banking API for the Rinha de Backend challenge. Built with Rust, Actix-web 4, and SQLx. ~140 lines under strict resource constraints (1.5 CPU, 550MB RAM).'
1213
} = Astro.props;
1314
14-
const GA_ID = import.meta.env.PUBLIC_GA_ID;
15-
1615
const jsonLd = {
1716
'@context': 'https://schema.org',
1817
'@type': 'SoftwareSourceCode',
@@ -61,17 +60,9 @@ const jsonLd = {
6160
<script type="application/ld+json" set:html={JSON.stringify(jsonLd)} />
6261

6362
<title>{title}</title>
63+
<Analytics />
6464
</head>
6565
<body>
66-
<!-- Google Analytics -->
67-
<script is:inline async src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`}></script>
68-
<script is:inline define:vars={{ GA_ID }}>
69-
window.dataLayer = window.dataLayer || [];
70-
function gtag() { dataLayer.push(arguments); }
71-
gtag('js', new Date());
72-
gtag('config', GA_ID);
73-
</script>
74-
7566
<div class="particles">
7667
<div class="particle"></div>
7768
<div class="particle"></div>

0 commit comments

Comments
 (0)