admin管理员组文章数量:1323035
Setting up Google Analytics on one of the websites I have discovered that tracking code snippet provided for embedding in Analytics settings has different syntax and linked JS script than the one, that I have used when setting up another property couple of years ago.
Snippet #1 (gtag.js) - provided by Analytics settings
<script async src=""></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXX-Y');
</script>
Snippet #2 (analytics.js) - used in an older website
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','.js','ga');
ga('create', 'UA-XXXX-Y', 'auto');
ga('send', 'pageview');
Documentation refers to migration from analytics.js to gtag.js in general, but without any specific deadlines. So while I am still using analytics.js on my older website the questions arise:
What are the differences (if any) of using gtag.js over analytics.js, both in collected data and in website performance?
Also, when I migrate to gtag.js on older site, are there any known hiccups in collecting data by Google Analytics? Like some data could be lost for some "propagation" period or similar...
Thanks in advance!
Setting up Google Analytics on one of the websites I have discovered that tracking code snippet provided for embedding in Analytics settings has different syntax and linked JS script than the one, that I have used when setting up another property couple of years ago.
Snippet #1 (gtag.js) - provided by Analytics settings
<script async src="https://www.googletagmanager./gtag/js?id=UA-XXXX-Y"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXX-Y');
</script>
Snippet #2 (analytics.js) - used in an older website
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics./analytics.js','ga');
ga('create', 'UA-XXXX-Y', 'auto');
ga('send', 'pageview');
Documentation refers to migration from analytics.js to gtag.js in general, but without any specific deadlines. So while I am still using analytics.js on my older website the questions arise:
What are the differences (if any) of using gtag.js over analytics.js, both in collected data and in website performance?
Also, when I migrate to gtag.js on older site, are there any known hiccups in collecting data by Google Analytics? Like some data could be lost for some "propagation" period or similar...
Thanks in advance!
Share Improve this question asked Feb 8, 2019 at 12:58 AlisaDuAlisaDu 1152 silver badges9 bronze badges2 Answers
Reset to default 7gtag.js is not a replacement library for analytics.js, it is a new method of implementation of the analytics.js library. When you look at the javascript that's loaded on a page with gtag.js, you'll see analytics.js being loaded through gtag.js. So data collection is the same.
I would say only change it if there is a need, otherwise, leave as is. analytics.js is fine. I would say gtag.js might be more plicated in terms of implementation pared to analytics.js only because it is new and some people are more used to analytics.js and would confuse gtag() and ga() functions.
Gtag is a javascript library used to send events to various tags like google analytics, floadlight, google Ads etc on same time to reduce the burden of writting different code for different tags for sending data.
On the other hand analytics.js is a js library only used to send data only to google analytics.
Finally I can say that If you want to use only analytics.js in your project then analytics.js is fine but if there is a plan to add any other google services then its better to go with Gtag.js to make things easy.
Note: You can also have a eye on GTM (google tag manager) which will provide more supporting features like third party tags support whereas Gtag only support google services.
本文标签: javascriptDifference between Google Analytics script snippets analyticsjs and gtagjsStack Overflow
版权声明:本文标题:javascript - Difference between Google Analytics script snippets analytics.js and gtag.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742109463a2421176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论