GA4 Setup Guide: Step-by-Step Instructions with Code Examples

GA4 Setup Guide: Step-by-Step Instructions with Code Examples

Ready to unlock deeper insights from your website traffic? Setting up Google Analytics 4 (GA4) is essential for modern analytics, but the process can seem daunting. This step-by-step guide walks you through every stage of GA4 setup, from account creation to advanced event tracking, complete with clear code examples and troubleshooting tips.

Estimated reading time: 7 minutes 52 Views
On this page
Last Updated:

Google Analytics 4 (GA4) is the new standard for website and app analytics. Whether you’re migrating from Universal Analytics or starting fresh, a proper GA4 setup is critical for reliable data and actionable insights.

But here’s the challenge:

GA4’s event-based model is powerful, but it’s also different from what many marketers and developers are used to. The setup process, data streams, and event tracking require a new approach and often, a bit of code.

This comprehensive guide will walk you through the entire GA4 setup process, step by step. You’ll learn how to:

  • Create your GA4 property and data stream
  • Install GA4 manually with code examples
  • Configure event tracking for key user actions
  • Verify and troubleshoot your implementation
  • Leverage advanced GA4 features for better analytics

Let’s get started with the basics and move all the way to advanced event tracking.

What Is GA4 and Why Does It Matter?

google analytics visitors tracking screenshot

GA4 is Google’s latest analytics platform, replacing Universal Analytics (UA) as the default for all new properties. Unlike UA, which relied on sessions and pageviews, GA4 uses an event-based data model. Every user interaction pageview, click, scroll, or purchase is tracked as an event.

This shift enables more flexible, privacy-centric tracking across websites and mobile apps. GA4 also offers:

  • Improved cross-device and cross-platform measurement
  • Automatic event tracking for common actions
  • Enhanced privacy controls and data retention options
  • Deeper integration with Google Ads and BigQuery
  • Predictive analytics and machine learning insights

In short, GA4 is built for the future of analytics and setting it up correctly is essential for accurate reporting and marketing optimization.

GA4 vs. Universal Analytics: Key Differences

Before you dive into setup, it’s helpful to understand how GA4 differs from Universal Analytics:

  • Data Model: GA4 is event-based, while UA is session-based.
  • Cross-Platform: GA4 natively supports web and app tracking in a single property.
  • Privacy: GA4 includes more granular data controls, consent mode, and cookieless tracking options.
  • Reporting: GA4 uses Explorations and customizable reports instead of UA’s fixed dashboards.
  • Event Tracking: GA4 tracks more events automatically and makes custom event setup easier via code or Google Tag Manager.

For a detailed breakdown, see Google’s official GA4 documentation.

Step 1: Create Your Google Analytics Account

google analytics create account screen

If you already have a Google Analytics account, you can skip to property creation. Otherwise:

  1. Go to analytics.google.com and sign in with your Google account.
  2. Click Admin in the lower left.
  3. Under the Account column, click Create Account.
  4. Enter your account name and data-sharing preferences. Click Next.

Your account is now ready for a new GA4 property.

Step 2: Set Up a GA4 Property

google analytics create property screen

  1. In the Admin panel, under the Property column, click Create Property.
  2. Enter a descriptive property name (e.g., “My Website GA4”).
  3. Select your reporting time zone and currency.
  4. Click Next and fill in your business details.
  5. Click Create.

GA4 will prompt you to set up a data stream for your website or app.

Step 3: Add a Data Stream

google analytics add a data stream

google analytics web create data stream

  1. Click Data Streams in the property setup wizard.
  2. Choose Web for website tracking.
  3. Enter your website URL and a stream name (e.g., “Main Site”).
  4. Click Create stream.

GA4 will generate a unique Measurement ID (e.g., G-XXXXXXXXXX). You’ll need this for the next step.

Step 4: Install the GA4 Tag Manually (with Code Examples)

There are two main ways to install GA4:

  • Using Google Tag Manager (recommended for flexibility)
  • Manual installation by adding the GA4 tag directly to your site’s code

This guide focuses on manual installation with code examples.

How to Install GA4 with gtag.js

  1. In your GA4 data stream setup, find the Tagging Instructions section.
  2. Copy the Global Site Tag (gtag.js) code snippet provided. It looks like this:

Replace G-XXXXXXXXXX with your actual Measurement ID.

  1. Paste this code into the section of every page you want to track.
  2. Save and publish your changes.

Tip: If you use a CMS (like WordPress), many themes have a “Header Scripts” field. Or, use a plugin to insert header code.

How to Install GA4 with Google Tag Manager (Optional)

If you prefer using Google Tag Manager (GTM):

  1. Set up a GTM container for your website.
  2. Add the GTM container code to your site’s and sections.
  3. In GTM, create a new tag: Tag Type: Google Analytics: GA4 Configuration.
  4. Enter your Measurement ID.
  5. Set the trigger to “All Pages.”
  6. Publish your GTM container.

This approach makes it easier to manage and update analytics tags in the future.

Step 5: Verify Your GA4 Installation

google analytics verify installation

After installing the GA4 tag, you should verify that data is being sent to your property.

Check in GA4 DebugView

  1. In your GA4 property, go to Admin > DebugView.
  2. Open your website in a new browser tab (ideally in Incognito mode).
  3. Perform some actions (pageviews, clicks). You should see events appear in DebugView in real time.

Use Google Tag Assistant

Install the Google Tag Assistant Chrome extension. Visit your site and check for the GA4 tag firing correctly.

If you see your Measurement ID and no errors, your setup is working.

Step 6: Set Up Event Tracking (with Code Examples)

GA4 tracks several events automatically (page_view, scroll, outbound_click, etc.), but you’ll often want to track custom events like sign-ups, downloads, or purchases.

How to Track a Custom Event with gtag.js

To track a custom event (e.g., a newsletter sign-up), add this code to your site’s JavaScript when the event occurs:

gtag('event', 'sign_up', { method: 'Newsletter Form' }); 

This sends a sign_up event to GA4 with an additional parameter (method).

Example: Tracking a Button Click

Suppose you want to track clicks on a “Download PDF” button:

Now, every time the button is clicked, a download event is sent to GA4.

Recommended Events

Google provides a list of recommended events (like login, purchase, search). Use these names for consistency and enhanced reporting.

Step 7: Test and Troubleshoot Your GA4 Setup

Proper testing ensures your analytics are accurate. Here’s how to troubleshoot common issues:

  • Events not appearing in DebugView: Check your Measurement ID, code placement, and browser extensions that may block scripts.
  • Duplicate events: Ensure you’re not firing the same event multiple times on a single action.
  • Data delays: GA4 real-time data is fast, but standard reports may take up to 24 hours to update.
  • Tag conflicts: Remove old Universal Analytics tags to avoid double-counting.

For advanced troubleshooting, use browser developer tools (Network tab) to inspect outgoing requests to www.google-analytics.com/g/collect.

Advanced GA4 Setup Tips and Best Practices

  • Enable Enhanced Measurement: In your data stream settings, toggle on Enhanced Measurement to automatically track scrolls, outbound clicks, site search, video engagement, and file downloads.
  • Set Up Conversions: Mark key events (like sign_up or purchase) as conversions in GA4 for easy goal tracking.
  • Link Google Ads: Integrate GA4 with Google Ads for better campaign attribution and remarketing.
  • Use BigQuery Export: For advanced analysis, connect GA4 to BigQuery to access raw event data.
  • Configure User Properties: Use gtag('set', ...) to define custom user properties for segmentation.

GA4 Code Examples for Common Use Cases

contact form sumbit send to ga4 event

Track a Form Submission

document.getElementById('contact-form').addEventListener('submit', function() { gtag('event', 'form_submit', { form_id: 'contact-form' }); }); 

Track E-commerce Purchases

gtag('event', 'purchase', { transaction_id: 'T12345', value: 99.99, currency: 'USD', items: [ { item_id: 'SKU_123', item_name: 'Product Name', quantity: 1, price: 99.99 } ] }); 

Set a Custom User Property

gtag('set', 'user_properties', { membership_level: 'gold' }); 

These examples can be adapted to your site’s needs. For more, see the GA4 developer documentation.

Migrating from Universal Analytics to GA4

migrating from universal analytics to ga4

If you’re moving from UA to GA4, follow these steps:

  1. Set up your GA4 property alongside your existing UA property.
  2. Install the GA4 tag as described above.
  3. Map your UA goals to GA4 events and mark them as conversions.
  4. Update your reporting and dashboards to use GA4 data.
  5. Plan for UA data sunset (Google stopped processing UA data in July 2023).

For a detailed migration checklist, see Google’s official migration guide.

GA4 Setup Troubleshooting FAQ

Why is my GA4 data not showing up?

Check that your Measurement ID is correct, the tag is in the of every page, and no browser extensions are blocking analytics scripts. Use DebugView and Tag Assistant for diagnosis.

Can I use both GA4 and Universal Analytics?

Yes, you can run both in parallel, but Google recommends focusing on GA4 as UA is deprecated.

How do I track cross-domain traffic?

In your GA4 data stream settings, configure cross-domain tracking by listing all relevant domains. Update your gtag.js code if needed.

How do I exclude internal traffic?

In GA4, go to Admin > Data Streams > More Tagging Settings > Define Internal Traffic. Add your office IP addresses to filter out internal visits.

Resources and Next Steps

With your GA4 setup complete, you’re ready to collect actionable data, optimize your marketing, and future-proof your analytics. For more advanced tracking, explore GA4’s custom dimensions, audiences, and integrations with Google Ads and BigQuery.

Remember: analytics is only as good as your implementation. Test regularly, update your events as your site evolves, and stay current with Google’s latest GA4 features.

Happy tracking!

Was this topic helpful?
Instant Interactive Guide
Quick Insights About:   GA4 setup guide step by step with code examples
Verified insights by NextAlgoo Editorial Team.

Leave a Comment