Skip to content

Commit

Permalink
fix: forward google events to partytown
Browse files Browse the repository at this point in the history
  • Loading branch information
steveoh committed Apr 11, 2024
1 parent 46fd8d3 commit d71d9c7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"Grantsville",
"Greendale",
"GSTC",
"gtag",
"Guestblog",
"Gunnison",
"HAFB",
Expand Down
4 changes: 3 additions & 1 deletion src/components/BaseHead.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
import type { ImageMetadata } from 'astro';
import GoogleAnalytics from '@components/page/GoogleAnalytics.astro';
interface Props {
Expand Down Expand Up @@ -44,4 +46,4 @@ const { title, description, image = '/default-social-card.png' } = Astro.props;
<meta property="twitter:description" content={description} />
<meta property="twitter:image" content={new URL(typeof image === 'string' ? image : image.src, Astro.url)} />

<GoogleAnalytics id={GA_ID} />
{import.meta.env.PROD && (<GoogleAnalytics id={GA_ID} />)}
23 changes: 16 additions & 7 deletions src/components/page/GoogleAnalytics.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ type Props = {
const { id } = Astro.props;
---

<script is:inline>
partytown = {
forward: [
'dataLayer.push',
'analytics.track',
'analytics.identify',
'analytics.group',
'analytics.reset',
'analytics.user',
'analytics.alias',
]
};
</script>
<script type="text/partytown" is:inline async src={`https://www.googletagmanager.com/gtag/js?id=${id}`}></script>
<script type="text/partytown" is:inline define:vars={{ id }}>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());

gtag('config', id);
window.gtag = () => window.dataLayer.push(arguments);
window.gtag('js', new Date())
window.gtag('config', id);
</script>

0 comments on commit d71d9c7

Please sign in to comment.