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(){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.

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_MEASUREMENT_ID', { 'send_page_view': false });

Configure additional Google Analytics properties

By default, the snippet configures a single Google Analytics property. To configure a second Google Analytics property with the measurement ID GA_MEASUREMENT_ID_2, add an additional config command:

gtag('config', 'GA_MEASUREMENT_ID_2');