Add gtag.js to your site
The global site tag (gtag.js) is a JavaScript tagging framework and API that allows you to send event data to Google Analytics, Google Ads, and Google Marketing Platform. This page describes how to use gtag.js to configure a site for Google Analytics. For general (i.e. non-product specific) gtag.js documentation, read the gtag.js developer guide.
Install the global site tag
To install the global site tag, copy the following code and paste it immediately after the <head>
tag on every page of your site. Replace GA_MEASUREMENT_ID
with the ID of the Google Analytics property to which you want to send data. You need only one global snippet per page.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){window.dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
</script>
This snippet loads the gtag.js library, establishes GA_MEASUREMENT_ID
as the default Google Analytics property ID, and sends a pageview hit to Google Analytics.
这个里面有github的两个repository的示例代码