How to Add Google Analytics 4 Tracking Code to Website

Cody Schneider8 min read

Getting Google Analytics 4 set up is the first real step toward understanding who your website visitors are, where they come from, and what they do. This guide will walk you through exactly where to find your GA4 tracking code and the different ways to add it to your website so you can start collecting that valuable data.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

What Exactly Is the GA4 Tracking Code?

The Google Analytics 4 tracking code is a unique snippet of JavaScript that connects your website to your Google Analytics property. When someone visits your site, this code activates, sending information about the visit - like which page they landed on, what device they're using, and their geographic location - back to your GA4 account. Think of it as the handshake that allows your website to talk to Google Analytics.

If you're familiar with the older Universal Analytics, you'll remember the "UA-" tracking ID. The modern equivalent in GA4 is the Measurement ID, which always starts with "G-" followed by a string of characters (e.g., G-XYZ123ABC).

The "tracking code" itself generally refers to one of two things, and it's helpful to know the difference:

  • Measurement ID (e.g., G-XYZ123ABC): This is your unique identifier. Many platforms and plugins (like those for WordPress or Shopify) only require you to paste this ID into a settings field. They handle placing the full code in the background.
  • Global Site Tag (gtag.js): This is the full JavaScript snippet. You’ll need this entire block of code if you plan to install it manually into your website's HTML. The Measurement ID is included within this script.

Finding Your Measurement ID and Tracking Code

Before you can add the code, you need to find it. This only takes a minute inside your Google Analytics account.

Here’s the step-by-step process:

  1. Log in to Google Analytics: Head to https://analytics.google.com/ and sign in.
  2. Navigate to Admin: In the bottom-left corner of the screen, click on the gear icon labeled "Admin." This takes you to the settings area.
  3. Select Your Account and Property: Make sure you have the correct Account and GA4 Property selected in the first two columns. If you just created it, it should be selected by default.
  4. Open Data Streams: In the "Property" column, find and click on "Data Streams." A data stream is simply a source of data - in this case, your website.
  5. Select Your Web Stream: You will see a list of your data streams. Click on the one for your website. It will likely be named after your site URL.
  6. Find Your ID and Tag: This screen holds all the information you need.

Now that you have your code, it's time to add it to your website.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

How to Add the GA4 Tracking Code to Your Website

There are three main methods for installing the GA4 tag. The right one for you depends on how your website is built and your comfort level with tech. For most people, using a plugin or a built-in CMS integration is the easiest and safest choice.

Method 1: Manual Installation into Your Website’s HTML

This method involves editing your website’s source code directly. It's best suited for developers or those with custom-coded websites who are comfortable with HTML.

Warning: If you're not careful, you could break your website's layout or functionality. Always back up your site before making code changes.

Here’s what you need to do:

  1. Copy the entire Global Site Tag (gtag.js) snippet from your GA4 data stream settings. It will look like this (your Measurement ID will be different):
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YOUR_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [],
  function gtag(){dataLayer.push(arguments),}
  gtag('js', new Date()),

  gtag('config', 'G-YOUR_MEASUREMENT_ID'),
</script>
  1. Access your website’s theme files. For many content management systems, this means finding a file that controls the global header of your site, like header.php or head.html.
  2. Paste the entire code snippet immediately after the opening <head> tag.
  3. Save the file. This change needs to be made in a way that it appears on every single page of your website to ensure consistent tracking.
GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Method 2: Using a CMS Platform Integration or Plugin

This is by far the simplest and most recommended route for the majority of website owners using platforms like WordPress, Shopify, Wix, or Squarespace. These systems provide a dedicated field where you just paste your Measurement ID - no code editing required.

For WordPress Users:

The easiest way to add GA4 to a WordPress site is with a plugin.

  • Site Kit by Google: This is Google’s official plugin. It not only installs your Analytics tag but also integrates data from Search Console, AdSense, and other Google products directly into your WordPress dashboard.
  • Dedicated GA Plugins: Plugins like GA Google Analytics or MonsterInsights are popular choices that make setup a breeze.

The general steps are the same for most plugins:

  1. Install and activate your chosen plugin from the WordPress plugin directory.
  2. Navigate to the plugin’s settings page.
  3. Follow its setup wizard, which will either prompt you to connect your Google account or provide a field to paste your G- Measurement ID.
  4. Save your settings, and you're done! The plugin handles inserting the code in the right place on every page.

For Shopify Merchants:

Shopify has a clean, native integration for Google Analytics 4, making setup incredibly straightforward.

  1. From your Shopify Admin dashboard, go to Online Store > Preferences.
  2. Scroll down to the "Google Analytics" section.
  3. You will see a field labeled "Google Analytics account." Paste your G- Measurement ID into this box.
  4. Click Save. That’s it! Shopify will now manage your GA4 tracking tag.

For Squarespace, Wix, and Others:

Most modern website builders have similar built-in tools. You’ll need to look in their specific settings panels:

  • On Squarespace: Go to Settings > Marketing > Marketing Tools and use the "Google Analytics" field.
  • On Wix: Navigate to Marketing & SEO > Marketing Integrations and connect your Google Analytics account.

Just like the others, these platforms only need your "G-" Measurement ID. The system takes care of the rest.

Method 3: Using Google Tag Manager (GTM)

This method is the most powerful and flexible, making it the preferred choice for marketers and businesses that plan to manage multiple tracking scripts (like Facebook Pixel, LinkedIn Insight Tag, etc.). Google Tag Manager acts as a container for all your tags. You install the GTM script on your site once, and from then on, you manage what fires through the GTM interface - no more editing website code.

Here’s the basic setup process:

  1. Set up GTM: If you haven't already, go to https://tagmanager.google.com/ and create an account and a container for your website.
  2. Install the GTM Script: GTM will give you two code snippets. One goes high in the <head> of your site, and the other goes after the opening <body> tag. This is the only code you will need to add to your website directly.
  3. Create a GA4 Tag in GTM:
  4. Set a Trigger:
  5. Save and Publish: Save your new tag. Then click the blue "Submit" button in the top-right corner of GTM, give your changes a name (like "Added GA4 Tag"), and "Publish" your container.

How to Verify Your GA4 Installation Is Working

Once you've installed the code, don't just assume it’s working. You absolutely need to verify it. Luckily, this is easy to do.

Check the Realtime Report

This is the quickest and most common method.

  1. Open your Google Analytics 4 property.
  2. In the left-hand navigation, go to Reports > Realtime.
  3. In a separate browser window (preferably an incognito or private window), open your website and click on a few pages.
  4. Back in the GA4 Realtime report, you should see your visit appear within a minute or two. You’ll see your location on the map and the pages you viewed listed in the "Views by Page title" card. If you see activity, it's working!
GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Use Browser Developer Tools

If you feel a bit more technical, you can check that your browser is sending the tracking data correctly.

  1. On your website, right-click and select "Inspect" to open Developer Tools.
  2. Go to the "Network" tab.
  3. In the filter box, type collect.
  4. Refresh your webpage.
  5. You should see entries appear with names that start with collect?v=2.... If you click on one, you can inspect its details and see your GA4 Measurement ID is being sent in the request.

Use the GTM "Preview" Mode

If you used Google Tag Manager, its "Preview" mode is the best way to debug. Click "Preview" in your GTM workspace, enter your website URL, and a new tab connected to your GTM container will open. As you click around, the Tag Assistant debug window will show you exactly which tags have fired. You should see your GA4 tag under 'Tags Fired'.

Final Thoughts

Getting your Google Analytics 4 tracking code installed properly is a foundational task for any digital marketer or business owner. Whether you do it by adding the script manually, using a simple CMS plugin, or leveraging the power of Google Tag Manager, the end goal is the same: to start gathering the data you need to make smarter decisions.

Once your data is flowing into Google Analytics, the next challenge becomes making sense of it all. Manually building custom reports day after day to answer questions about marketing performance can feel like a chore. In our experience, we saw that most marketers and founders were drowning in data but struggled to find actionable insights quickly. That's why we created https://www.graphed.com/register. Our platform connects directly to your Google Analytics, marketing, and sales tools, letting you create dashboards and get insights in seconds just by asking questions in plain English - no technical skills needed.

Related Articles