The Google 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 Google tag
To install the Google tag, copy the following code and paste it immediately
after the
<head>
tag on every page of your site. Replace
GA_TRACKING_ID
with the ID of the Google Analytics property to which
you want to send data. You need only one snippet per page.
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){window.dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_TRACKING_ID');
</script>
This snippet loads the gtag.js library, establishes
GA_TRACKING_ID
as the
default Google Analytics property ID, and sends a
pageview hit
to Google
Analytics.
Disable pageview measurement
The default behavior of this snippet is to send a pageview hit to Google
Analytics. This is the desired behavior in most cases; pageviews are
automatically recorded once you add the snippet to each page on your site.
However, if you don’t want the snippet to send a pageview hit to Google
Analytics, set the
send_page_view
parameter to
false
:
gtag('config', 'GA_TRACKING_ID', { 'send_page_view': false });
By default, the snippet configures a single Google Analytics property. To
configure a second Google Analytics property with the Tracking ID
GA_TRACKING_ID_2
, add an additional
config
command:
gtag('config', 'GA_TRACKING_ID_2');