Google Analytics with newer script
See original GitHub issueI am trying to add Google Analytics to my site. I see how the documentation suggests to define a Google Analytics plugin, but Google told me to copy-paste code that isn’t the same as in that documentation. Here is the code Google told me to copy.
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXX-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXX-1');
</script>
By looking at the previous documentation and looking at this documentation for Google’s data layer, I wrote this code.
export function googleAnalytics(gaCode: string) {
let url = `'https://www.googletagmanager.com/gtag/js?id=UA-${gaCode}-1'`
function addToDataLayer() {
let newData = [{
'js': new Date(),
'config': `'UA-${gaCode}-1'`
}];
window.dataLayer = window.dataLayer || [];
window.dataLayer.push(newData);
}
return function(): ConfigOverride {
return {
page: {
scripts: [
<script async src={url}></script>,
<script>{addToDataLayer()}</script>
]
}
}
};
}
I think this is nearly correct, but I don’t know how to deal with window.dataLayer
not being on defined on the type Window
.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Migrate from analytics.js to gtag.js (Universal Analytics)
This guide walks you through the process of migrating an existing analytics.js Universal Analytics implementation to use gtag.js .
Read more >[UA→GA4] Tips for switching from analytics.js to gtag.js
This article is for website owners who want to understand how analytics.js for a Universal Analytics (UA) property maps to the gtag.js for...
Read more >How To Set Up Google Analytics With Next.JS Script Component
In this blog post, we'll go through how to set up Next.JS with Google Analytics using the latest versions (Next.JS 11 and Google...
Read more >New vs Old Google Analytics script - Stack Overflow
Looks like some things aren't yet supported under "Universal Analytics", the new default.
Read more >A Guide to Google Analytics Snippet Versions from 2005 to ...
In this blog post from MoreVisibility, discover all different kinds of code that are used to deploy Google Analytics.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
it can be. right now I push, test, if everything works, I will bump the version and push (typically via a commit called
bump version
), and thennpm publish
. this can be further automated of course, but its not much of a hassle (or particularly error-prone) to demand an automated workflow.I have written up all my thoughts here about why I think my code to communicate with Google Analytics from a CODEDOC site is correct.
Thanks for all your help @loreanvictor 😃