Add gtag.js to your site

The Global Site Tag (gtag.js) provides a framework for streamlined web page tagging – giving you better control while making implementation easier. Using gtag.js lets you benefit from the latest tracking features and integrations as they become available.

Install the global tracking snippet

To use gtag.js to track your site, install it by copying the following snippet and pasting it immediately after the <head> tag on every page of your site. Replace GA_TRACKING_ID with the tracking ID of the Google Analytics property you want to send data to. 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_TRACKING_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments)};
  gtag('js', new Date());

  gtag('config', 'GA_TRACKING_ID');
</script>

The following code in the snippet:

gtag('config', 'GA_TRACKING_ID');

serves these purposes:

  • It configures the Google Analytics property with the tracking ID GA_TRACKING_ID as the default property to receive data sent by the event command.

  • It sends a pageview to the default property.

If you don't want gtag.js to send the pageview to Google Analytics, set the send_page_view parameter to false:

gtag('config', 'GA_TRACKING_ID', { 'send_page_view': false });

To configure a second Google Analytics property with the tracking ID GA_TRACKING_ID_2, add the following code to the bottom of the snippet:

gtag('config', 'GA_TRACKING_ID_2');

Note that the following code in the snippet:

gtag('config', 'GA_TRACKING_ID');
gtag('config', 'GA_TRACKING_ID_2');

serves these purposes:

  • It configures the Google Analytics properties with the tracking IDs GA_TRACKING_ID and GA_TRACKING_ID_2 as the default properties to receive data sent by the event command.

  • It sends a pageview to the default properties.

Next step

Learn how to send data to Google Analytics.