From de7381f9e30c3c54df6fecdfa921bf6dc9d1823b Mon Sep 17 00:00:00 2001 From: Eusebio Date: Wed, 26 Jun 2024 17:07:50 +0200 Subject: [PATCH 1/2] Add Google Analytics --- astro.config.mjs | 22 ++++++++++++++-------- package-lock.json | 21 +++++++++++++++++++++ package.json | 1 + src/layouts/Layout.astro | 9 +++++++++ src/scripts/cookie-consent-config.js | 7 +++++++ src/scripts/google-analytics.js | 7 +++++++ 6 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 src/scripts/google-analytics.js diff --git a/astro.config.mjs b/astro.config.mjs index 2f694a6..739558d 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,14 +1,20 @@ import { defineConfig } from 'astro/config'; - import tailwind from "@astrojs/tailwind"; +import partytown from "@astrojs/partytown"; + // https://astro.build/config export default defineConfig({ - devToolbar: { enabled: false }, - integrations: [ - tailwind({ - applyBaseStyles: false, - }), - ], - base: process.env.BASE_URL ? process.env.BASE_URL : "/", + devToolbar: { + enabled: false + }, + integrations: [tailwind({ + applyBaseStyles: false + }), partytown({ + config: { + forward: ['dataLayer.push'], + }, + }), +], + base: process.env.BASE_URL ? process.env.BASE_URL : "/" }); diff --git a/package-lock.json b/package-lock.json index ac57cd6..78f35b7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.1", "dependencies": { "@astrojs/check": "^0.7.0", + "@astrojs/partytown": "^2.1.1", "@astrojs/tailwind": "^5.1.0", "@orestbida/iframemanager": "^1.2.5", "astro": "^4.10.1", @@ -138,6 +139,15 @@ "vfile": "^6.0.1" } }, + "node_modules/@astrojs/partytown": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/@astrojs/partytown/-/partytown-2.1.1.tgz", + "integrity": "sha512-KvecQuGGgrDrdTjC5awmtoQXqJFinChfeizKBJyr7F5EGQPx62xq8SIWkcXamLr5xZyNw0lryKeLtIzbxRH/zw==", + "dependencies": { + "@builder.io/partytown": "^0.10.2", + "mrmime": "^2.0.0" + } + }, "node_modules/@astrojs/prism": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/@astrojs/prism/-/prism-3.1.0.tgz", @@ -519,6 +529,17 @@ "node": ">=6.9.0" } }, + "node_modules/@builder.io/partytown": { + "version": "0.10.2", + "resolved": "https://registry.npmjs.org/@builder.io/partytown/-/partytown-0.10.2.tgz", + "integrity": "sha512-A9U+4PREWcS+CCYzKGIPovtGB/PBgnH/8oQyCE6Nr9drDJk6cMPpLQIEajpGPmG9tYF7N3FkRvhXm/AS9+0iKg==", + "bin": { + "partytown": "bin/partytown.cjs" + }, + "engines": { + "node": ">=18.0.0" + } + }, "node_modules/@emmetio/abbreviation": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/@emmetio/abbreviation/-/abbreviation-2.3.3.tgz", diff --git a/package.json b/package.json index 63c593e..d10b74d 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ }, "dependencies": { "@astrojs/check": "^0.7.0", + "@astrojs/partytown": "^2.1.1", "@astrojs/tailwind": "^5.1.0", "@orestbida/iframemanager": "^1.2.5", "astro": "^4.10.1", diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index 91c1ca7..c473c78 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -12,6 +12,15 @@ const { title } = Astro.props; + + diff --git a/src/scripts/cookie-consent-config.js b/src/scripts/cookie-consent-config.js index 47e9ee1..6f64a0a 100644 --- a/src/scripts/cookie-consent-config.js +++ b/src/scripts/cookie-consent-config.js @@ -53,6 +53,13 @@ run({ flipButtons: false, }, }, + onConsent: () => { + window.dispatchEvent(new CustomEvent("ptupdate")); + }, + + onChange: () => { + window.dispatchEvent(new CustomEvent("ptupdate")); + }, categories: { necessary: { readOnly: true, diff --git a/src/scripts/google-analytics.js b/src/scripts/google-analytics.js new file mode 100644 index 0000000..e2f2059 --- /dev/null +++ b/src/scripts/google-analytics.js @@ -0,0 +1,7 @@ +window.dataLayer = window.dataLayer || []; +function gtag() { + dataLayer.push(arguments); +} +gtag("js", new Date()); + +gtag("config", import.meta.env.MEASUREMENT_ID); From c10516f7fe7fcacda8824582336d2ccd318e6bfd Mon Sep 17 00:00:00 2001 From: Eusebio Date: Wed, 26 Jun 2024 17:18:22 +0200 Subject: [PATCH 2/2] Delete .env file --- src/layouts/Layout.astro | 2 +- src/scripts/google-analytics.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index c473c78..2342311 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -18,7 +18,7 @@ const { title } = Astro.props; data-category="analytics" data-service="Google Analytics" data-type="text/partytown" - data-src=`https://www.googletagmanager.com/gtag/js?id=${import.meta.env.MEASUREMENT_ID}` + data-src="https://www.googletagmanager.com/gtag/js?id=G-E95B4HTX6G" > diff --git a/src/scripts/google-analytics.js b/src/scripts/google-analytics.js index e2f2059..bfb4333 100644 --- a/src/scripts/google-analytics.js +++ b/src/scripts/google-analytics.js @@ -4,4 +4,4 @@ function gtag() { } gtag("js", new Date()); -gtag("config", import.meta.env.MEASUREMENT_ID); +gtag("config", "G-E95B4HTX6G");