Skip to content

Commit b191c04

Browse files
authored
feat: integrate analytics (#105)
This Pull Request configures Google Analytics on the project, enabling ease of usage and interaction tracking on project. ### Changes Made - Added/Configured `@astrojs/partytown` integration to move third-party script (google-tags) execution to the web worker away from main thread, aiding performance - Introduced new environmental variable `PUBLIC_GOOGLE_ANALYTICS_MEASUREMENT_ID` - Integrated the Google Tag tracking script to the `layout/base.astro` template reaching all web page for tracking; with the introduced `PUBLIC_GOOGLE_ANALYTICS_MEASUREMENT_ID` consumed in the measurement id area of the script. 📖
1 parent 8039bab commit b191c04

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

astro.config.mjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@ import { defineConfig } from "astro/config";
22
import mdx from "@astrojs/mdx";
33
import react from "@astrojs/react";
44
import tailwind from "@astrojs/tailwind";
5+
import partytown from "@astrojs/partytown";
56
import vercel from "@astrojs/vercel/serverless";
67

78
// https://astro.build/config
89
export default defineConfig({
9-
integrations: [tailwind(), mdx(), react()],
10+
integrations: [
11+
mdx(),
12+
react(),
13+
tailwind(),
14+
partytown({
15+
config: {
16+
forward: ["dataLayer.push"],
17+
},
18+
})
19+
],
1020
output: "server",
1121
adapter: vercel()
1222
});

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"license": "ISC",
1717
"dependencies": {
1818
"@astrojs/mdx": "^2.2.1",
19+
"@astrojs/partytown": "^2.1.2",
1920
"@astrojs/react": "^3.1.0",
2021
"@astrojs/tailwind": "^5.1.0",
2122
"@astrojs/vercel": "^7.5.3",

src/layouts/base.astro

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import "@fontsource/ibm-plex-mono";
44
import "../base.css";
55
import { SITE_META_KEYWORDS, SITE_META_DESCRIPTION } from "../../constants.js";
66
7+
const MEASUREMENT_ID = import.meta.env.PUBLIC_GOOGLE_ANALYTICS_MEASUREMENT_ID;
8+
79
const { props, url } = Astro;
810
const {
911
pageTitle,
@@ -36,9 +38,19 @@ const {
3638
<meta name="twitter:description" content={description}>
3739
<meta name="twitter:image" content={ogImageUrl}>
3840

41+
<!-- Google tag (gtag.js) -->
42+
<script async src=`https://www.googletagmanager.com/gtag/js?id=${MEASUREMENT_ID}`></script>
43+
<script>
44+
window.dataLayer = window.dataLayer || [];
45+
function gtag(){dataLayer.push(arguments);}
46+
gtag('js', new Date());
47+
48+
gtag('config', MEASUREMENT_ID);
49+
</script>
50+
3951
<title>{ pageTitle } - {subtitle}</title>
4052
</head>
4153
<body class={list}>
42-
<slot />
54+
<slot />
4355
</body>
4456
</html>

0 commit comments

Comments
 (0)