Course contentsShow
← Web Development
Lesson 2781 of 2,98250. Third-Party IntegrationsFree lesson

Google Analytics 4 Setup

Learn how to add GA4 to your site, create measurement IDs, and configure basic tracking.

Google Analytics 4 Setup

You'll learn how to integrate GA4 into your website by creating measurement IDs and configuring tracking to monitor user interactions.

Google Analytics 4 (GA4) is Google's latest analytics platform that tracks how users interact with your website. Think of it as installing security cameras in a store—instead of recording video, you're capturing page views, clicks, and user journeys through your site.

Creating Your GA4 Property

First, you need a Google Analytics account and property. Navigate to the GA4 admin panel and create a new property (which represents your website). During setup, you'll receive a Measurement ID that looks like G-XXXXXXXXXX. This unique identifier connects your website to your analytics dashboard.

Adding GA4 to Your Site

The simplest integration uses Google's gtag.js snippet. Add this code to the <head> section of every page you want to track:

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XXXXXXXXXX');
</script>

Replace G-XXXXXXXXXX with your actual Measurement ID. The async attribute ensures analytics doesn't block page rendering—performance matters!

Basic Configuration

The gtag('config') call initializes tracking with default settings. GA4 automatically captures page views, scrolls, outbound clicks, and site searches. The dataLayer array queues events before the script fully loads, ensuring nothing gets lost.

Key Takeaway: GA4 setup requires adding a measurement-ID-specific tracking snippet to your site's head section, which automatically begins collecting basic user interaction data.