Skip to content

Commit 44e567d

Browse files
authored
🚀 feat: integrate Google Analytics with Partytown for improved performance
1 parent 85cd479 commit 44e567d

5 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎astro.config.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mdx from "@astrojs/mdx";
2+
import partytown from "@astrojs/partytown";
23
import sitemap, { ChangeFreqEnum } from "@astrojs/sitemap";
34
import solid from "@astrojs/solid-js";
45
import tailwindcss from "@tailwindcss/vite";
@@ -16,6 +17,11 @@ export default defineConfig({
1617
integrations: [
1718
solid(),
1819
mdx(),
20+
partytown({
21+
config: {
22+
forward: ["dataLayer.push"],
23+
},
24+
}),
1925
sitemap({
2026
serialize(item) {
2127
// Enhanced sitemap with priority and changefreq

‎bun.lock‎

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"typescript": "6.0.2"
2929
},
3030
"dependencies": {
31-
"@astrojs/rss": "^4.0.18",
31+
"@astrojs/partytown": "2.1.6",
32+
"@astrojs/rss": "4.0.18",
3233
"astro": "6.0.8",
3334
"solid-js": "1.9.11"
3435
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
interface Props {
3+
id: string;
4+
}
5+
const { id } = Astro.props;
6+
---
7+
8+
<!-- Google tag (gtag.js) -->
9+
<script
10+
is:inline
11+
type="text/partytown"
12+
async
13+
src={`https://www.googletagmanager.com/gtag/js?id=${id}`}
14+
></script>
15+
16+
<script is:inline type="text/partytown" define:vars={{ id }}>
17+
window.dataLayer = window.dataLayer || [];
18+
window.gtag = function () {
19+
dataLayer.push(arguments);
20+
};
21+
window.gtag('js', new Date());
22+
window.gtag('config', id);
23+
</script>

‎src/layouts/Layout.astro‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import Footer from "@components/Footer.astro";
3+
import GoogleAnalytics from "@components/GoogleAnalytics.astro";
34
import Header from "@components/Header.astro";
45
import { SKIP_LINK_CLASSES } from "@lib/constants";
56
import "../styles/global.css";
@@ -26,6 +27,10 @@ const {
2627
<html lang="en" class="scroll-smooth">
2728
<head>
2829
<meta charset="UTF-8" />
30+
31+
<!-- Google Analytics -->
32+
<GoogleAnalytics id="G-LKVQYYL9XZ" />
33+
2934
<meta
3035
name="viewport"
3136
content="width=device-width, initial-scale=1.0, maximum-scale=5.0"

0 commit comments

Comments
 (0)