ga4 to separate js file

This commit is contained in:
PrestonT500 2024-09-30 20:31:08 -07:00
parent e0ee42e61e
commit a9c0ef318c
2 changed files with 50 additions and 27 deletions

View File

@ -10,33 +10,7 @@
<link rel="stylesheet" href="css/popup.css">
<link rel="stylesheet" href="css/searchbar.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Google tag (gtag.js) Github Pages -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-C44R8LD9MC"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-C44R8LD9MC');
</script>
<!-- Google tag (gtag.js) Netlify -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-0R2ZXFSYVT"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-0R2ZXFSYVT');
</script>
<!-- Google tag (gtag.js) Render -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-MND1TVBSXV"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-MND1TVBSXV');
</script>
<script src="./js/ga4.js"></script>
</head>
<body>

49
js/ga4.js Normal file
View File

@ -0,0 +1,49 @@
function addGoogleAnalyticsTags() {
// GitHub Pages
var githubAnalyticsScript = document.createElement('script');
githubAnalyticsScript.async = true;
githubAnalyticsScript.src = 'https://www.googletagmanager.com/gtag/js?id=G-C44R8LD9MC';
document.head.appendChild(githubAnalyticsScript);
var githubScriptContent = document.createElement('script');
githubScriptContent.innerHTML = `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-C44R8LD9MC');
`;
document.head.appendChild(githubScriptContent);
// Netlify
var netlifyAnalyticsScript = document.createElement('script');
netlifyAnalyticsScript.async = true;
netlifyAnalyticsScript.src = 'https://www.googletagmanager.com/gtag/js?id=G-0R2ZXFSYVT';
document.head.appendChild(netlifyAnalyticsScript);
var netlifyScriptContent = document.createElement('script');
netlifyScriptContent.innerHTML = `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-0R2ZXFSYVT');
`;
document.head.appendChild(netlifyScriptContent);
// Render
var renderAnalyticsScript = document.createElement('script');
renderAnalyticsScript.async = true;
renderAnalyticsScript.src = 'https://www.googletagmanager.com/gtag/js?id=G-MND1TVBSXV';
document.head.appendChild(renderAnalyticsScript);
var renderScriptContent = document.createElement('script');
renderScriptContent.innerHTML = `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-MND1TVBSXV');
`;
document.head.appendChild(renderScriptContent);
}
// Call the function to add Google Analytics tags
addGoogleAnalyticsTags();