How to add custom code to body? #2108
-
Hello, I'm trying to configure gtag (https://webreaper.dev/posts/astro-google-tag-manager-ga4/) on my test page, there is any other way to add custom code to body instead of using custom Page component? Regards! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi @spokospace — you can add code using Starlight’s Based on the blog post you linked, this might look something like this: starlight({
// ...
head: [
{
tag: 'script',
attrs: {
src: 'https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX',
async: true,
},
},
{
tag: 'script',
content: `
window.dataLayer = window.dataLayer || [];
function gtag(){ dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'YOUR-GA-ID');
`,
},
],
}) |
Beta Was this translation helpful? Give feedback.
-
Great, thanks! BTW. if someone want to overwrite whole Page component, then we need to add below alias to astro.config.mjs:
because in Page.astro there are some imports with |
Beta Was this translation helpful? Give feedback.
In that case you might need a component override, but doesn’t need to override the full page, you can choose to override something smaller. And follow the “Reuse a built-in component” guide to avoid needing to reimplement anything.