Crash Course · Beginner → Advanced

Install Google Analytics 4: gtag.js and Google Tag Manager Step by Step

Connect GA4 to a website the right way. Method 1 adds the Google tag (gtag.js) directly to your pages; Method 2 uses Google Tag Manager (GTM). You will set up both step by step, track a button click and a form submission as GA4 events, push data with the dataLayer, mark key events, and verify everything with Tag Assistant and DebugView.

Beginner → Advanced14 min readInterview Q&A

Which Method Should You Use?

Method 1: gtag.js directlyMethod 2: Google Tag Manager
SetupPaste one snippet in the <head>Install GTM once, then add tags in a web interface
Custom eventsWrite JavaScript for each eventMostly point-and-click triggers
Other tags (Google Ads, Meta Pixel)Each needs code changesManaged in one place
Best forSmall, simple sitesGrowing sites, marketing teams, many tags

Use only one method for the same Measurement ID, or page views are counted twice. On WordPress you can also use Site Kit by Google, covered in the GA4 crash course.

Step 1: Create the GA4 Property and Web Stream

  • Go to analytics.google.com → Admin (gear icon) → Create → Property.
  • Enter the property name (e.g. “Bright Bakery Website”), reporting time zone and currency.
  • Add business details and objectives, then click Create.
  • Choose platform Web, enter the website URL and stream name, keep Enhanced measurement on (page views, scrolls, outbound clicks, site search, video, file downloads, form interactions).
  • Copy the Measurement ID, which looks like G-ABC123XYZ9. You need it in both methods.

Method 1: Install gtag.js Directly

  • In the web stream, open View tag instructions → Install manually.
  • Paste the code immediately after the opening <head> tag on every page (usually in a shared header template).
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABC123XYZ9"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-ABC123XYZ9');
</script>

WordPress without a plugin: add it to a child theme’s functions.php:

add_action( 'wp_head', function () { ?>
  <script async src="https://www.googletagmanager.com/gtag/js?id=G-ABC123XYZ9"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'G-ABC123XYZ9');
  </script>
<?php }, 1 );

Method 1: Send Custom Events With gtag

Track a click on the “Get My Free Quote” button and a completed quote form:

<a href="#quote" id="quote-btn">Get My Free Quote</a>

<script>
  document.getElementById('quote-btn').addEventListener('click', function () {
    gtag('event', 'select_promotion', {
      promotion_name: 'free_quote_hero'
    });
  });

  document.getElementById('quote-form').addEventListener('submit', function () {
    gtag('event', 'generate_lead', {
      form_name: 'cake_quote',
      value: 0,
      currency: 'USD'
    });
  });
</script>

Prefer Google’s recommended event names (generate_lead, sign_up, purchase) so reports understand them. Custom names use lowercase and underscores.

Method 2: Set Up Google Tag Manager

  • Go to tagmanager.google.com → Create account → container name = your domain, target platform Web.
  • GTM shows two snippets. Paste the first as high as possible in <head> and the second (<noscript> iframe) right after the opening <body> tag on every page. The container ID looks like GTM-XXXXXXX. (On WordPress you can use a GTM plugin or your theme’s header/body hooks.)
  • In GTM click Tags → New → Tag type Google Tag → Tag ID = G-ABC123XYZ9.
  • Trigger: Initialization – All Pages (or All Pages). Save as “GA4 – Google tag”.
  • Click Preview, enter your site URL; Tag Assistant opens your site and shows the tag as Fired.
  • Click Submit → add a version name (“GA4 base install”) → Publish. Nothing goes live until you publish.

Method 2: Track a Button Click in GTM

  • Variables → Configure → enable built-in Click ID, Click Classes, Click Text, Click URL.
  • Triggers → New → Click – All Elements → “Some Clicks” → Click ID equals quote-btn (or Click Text equals Get My Free Quote). Name it “Click – Quote button”. Use Just Links if the element is an <a> link.
  • Tags → New → Google Analytics: GA4 Event → Measurement ID G-ABC123XYZ9 → Event name select_promotion → Event parameter promotion_name = free_quote_hero → trigger “Click – Quote button”.
  • Preview, click the button, confirm the tag fired, then publish.

Method 2: Track Form Submissions With the dataLayer

GTM’s built-in Form Submission trigger does not work with every form (many submit by JavaScript). The reliable way is to have the site push an event to the dataLayer after a successful submission:

// run after the form is successfully sent
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'quote_form_success',
  form_name: 'cake_quote',
  cake_size: '1kg'
});
  • Variables → New → Data Layer Variable → name form_name (repeat for cake_size).
  • Triggers → New → Custom Event → event name quote_form_success.
  • Tags → New → GA4 Event → event name generate_lead → parameters form_name = {{DLV - form_name}}, cake_size = {{DLV - cake_size}} → trigger the custom event.
  • Many form plugins (Contact Form 7, WPForms, Gravity Forms) can push such events or have GTM integrations.

Step 3: Mark Key Events (Conversions)

  • After the event appears in GA4 (can take up to 24 hours in standard reports), go to Admin → Data display → Events and toggle Mark as key event for generate_lead. GA4 calls conversions “key events”.
  • To report on custom parameters such as cake_size, register them in Admin → Custom definitions → Create custom dimension (event scope).
  • Import key events into Google Ads as conversions after linking the accounts.

Step 4: Verify and Debug

  • Tag Assistant / GTM Preview — shows which tags fired on each event.
  • GA4 → Reports → Realtime — see your own visit within seconds.
  • GA4 → Admin → DebugView — events from Preview mode appear live with their parameters.
  • Browser DevTools → Network → filter collect to see hits sent to Google.
  • In regions such as the EEA and UK, ask for consent before analytics and ads cookies; use a consent management platform (CMP).
  • Implement Consent Mode v2 so Google tags respect choices (analytics_storage, ad_storage, ad_user_data, ad_personalization).
  • Set data retention in Admin → Data collection and modification → Data retention.

Common Mistakes

  • Double tagging — gtag in the theme and GTM and a plugin = duplicate page views.
  • Forgetting to Publish the GTM container.
  • Using an old Universal Analytics ID (UA-) instead of a GA4 ID (G-).
  • Not filtering internal traffic (Admin → Data streams → Configure tag settings → Define internal traffic, then activate the filter).
  • Tracking a button click as a “lead” instead of the confirmed form success.

Key Takeaways

  • Create property → web stream → Measurement ID.
  • Method 1: gtag.js in <head>; events with gtag('event', …).
  • Method 2: GTM container → Google tag → GA4 Event tags with triggers.
  • Use the dataLayer for reliable form tracking.
  • Mark key events; register custom dimensions.
  • Verify with Preview, Realtime and DebugView; never double-tag.

Interview Questions

What are the ways to install GA4 on a website?

Add the gtag.js snippet directly, deploy it through Google Tag Manager, or use a CMS integration such as Site Kit for WordPress.

What is the difference between gtag.js and Google Tag Manager?

gtag.js is code added directly to pages; GTM is a container installed once that lets you add and manage many tags and triggers from a web interface.

What is a Measurement ID?

The GA4 web stream identifier starting with G-, used to send data to the right property.

What is the dataLayer?

A JavaScript array that holds information and events the site pushes, which GTM reads to fire tags and fill variables.

How do you track a form submission reliably?

Push a custom event to the dataLayer after a successful submission and fire a GA4 event tag on a Custom Event trigger.

What is a key event in GA4?

An event marked as important for the business, previously called a conversion.

How do you test GTM tags before publishing?

Use Preview mode with Tag Assistant, check tags fire on the right events, and confirm data in GA4 DebugView.

What happens if both gtag.js and GTM send to the same GA4 ID?

Events such as page views are recorded twice, inflating metrics and distorting engagement data.

Exam Quick Revision

  • Admin → Create property → Web stream → G- ID.
  • Method 1: gtag.js after .
  • gtag(‘event’, ‘generate_lead’, {…}).
  • Method 2: GTM head + body snippets → Google tag → Initialization trigger.
  • GA4 Event tag + Click or Custom Event trigger.
  • dataLayer.push({event: …}).
  • Mark as key event; custom dimensions.
  • Preview, Realtime, DebugView; one method only.

Continue Learning

Scroll to Top