Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dynamically load Google Tag Manager script after app render #1117

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<meta charset="utf-8" />
<link rel="icon" href="/favicon.svg" />
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-8T22XRLHXY"></script>
<!-- <script async src="https://www.googletagmanager.com/gtag/js?id=G-8T22XRLHXY"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-8T22XRLHXY');
</script>
</script> -->
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<title>%VITE_APP_SITE_TITLE%</title>

Expand Down
23 changes: 23 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,26 @@ const initApp = async () => {
};

initApp();

// Dynamically load the Google Tag Manager script after the app has rendered
const loadGTM = () => {
const script = document.createElement('script');
script.src = 'https://www.googletagmanager.com/gtag/js?id=G-8T22XRLHXY';
script.defer = true;
document.head.appendChild(script);

script.onload = () => {
// @ts-ignore
window.dataLayer = window.dataLayer || [];
function gtag() {
// @ts-ignore
window.dataLayer.push(arguments);
}
// @ts-ignore
gtag('js', new Date());
// @ts-ignore
gtag('config', 'G-8T22XRLHXY');
};
};

loadGTM();
Loading