Google Analytics (GA4) library
pnpm i @aqzhyi/google-analytics
Insert the Google Tag Manager (GTM) script into the HTML using the Google Analytics ID you have configured (e.g.,
'G-G90JYXXXXX'
).
-
pages/_layout/BasicLayout.astro
--- import { GoogleAnalytics } from '@aqzhyi/google-analytics' --- <!doctype html> <html> <head> <GoogleAnalytics id='G-G90JYXXXXX' /> </head> <body> <slot /> </body> </html>
-
pages/index.astro
--- import BasicLayout from "./_layout/BasicLayout.astro" --- <BasicLayout> Page 1 </BasicLayout>
Preview
<GoogleAnalytics
id=''
debug={true}
/>
Preview