How to Generate Tracking Code in Google Analytics
To start collecting data about your website's visitors, you first need to generate a Google Analytics tracking code and add it to your site. This code is a small snippet of JavaScript that observes visitor activity and sends that information back to your Google Analytics reports. We’ll walk you through exactly where to find this code for both the new Google Analytics 4 and the older Universal Analytics (UA).
What is the Google Analytics Tracking Code?
The Google Analytics tracking code is a unique piece of JavaScript that connects your website to your Google Analytics property. When someone visits your website, this script runs in their browser, collecting anonymous data about their actions, such as which pages they view, how long they stay, and where they came from. It then bundles this information and sends it to Google's servers to populate your reports.
Every website you want to track needs this code installed. In Google Analytics 4, the modern version of the platform, this tracking capability is powered by the "Global Site Tag" (gtag.js). You will primarily interact with a Measurement ID, which looks like G-XXXXXXXXXX, that tells the global script which data stream to send information to.
For the older, now-discontinued Universal Analytics, you would look for a Tracking ID formatted as UA-XXXXXXXX-X. While both use the Global Site Tag, their configuration and the data they collect are fundamentally different.
How to Generate Tracking Code in Google Analytics 4
Finding your tracking code in GA4 involves looking for your Data Stream details. A data stream is simply a source of data flowing into your Analytics property - in this case, your website.
If you haven't created a Google Analytics account or a GA4 property yet, you'll need to do that first. Google will guide you through creating an account and your first property, at which point you'll be prompted to set up a data stream and get the code.
If you already have a property, follow these steps to find your code.
Step-by-Step Instructions
1. Navigate to the Admin Panel Log into your Google Analytics account. In the bottom-left corner, click on the gear icon labeled "Admin".
2. Go to Data Streams Make sure you have the correct Account and Property selected in the first two columns. In the "Property" column (the middle one), click on "Data Streams".
3. Select Your Web Data Stream You will see a list of your data streams. Click on the one associated with the website you want to track. It will typically be named after your website's URL.
4. View Tag Instructions In the "Web stream details" panel, look for the "View tag instructions" button at the bottom and click it. This takes you to the installation options.
Retrieving Your GA4 Tracking Code
Once you click "View tag instructions," you'll see a panel with a few choices. Here’s where you grab the specific identifier or the full script you need.
Option 1: Using Your Measurement ID (Easiest Method) Many website builders and CMS plugins (like those for WordPress, Shopify, or Wix) don't require the full code snippet. Instead, they just need your unique Measurement ID.
- Your Measurement ID is displayed prominently at the top right of the "Web stream details" page.
- It is formatted as G-XXXXXXXXXX.
- Simply copy this ID and paste it into the designated Google Analytics field in your website platform’s settings.
Option 2: Installing The Global Site Tag Manually If your website builder doesn't have a simple integration, or if you're working with a custom-coded site, you'll need the full JavaScript snippet.
- Inside the "Installation instructions" tab, click on "Install manually".
- Here you will find the Global Site Tag (gtag.js) script. It will look like the code block below.
- Click the copy icon to copy the entire script to your clipboard.
<!-- 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>This entire block of code needs to be pasted into the <head> section of every page of your website. If you use a templating system or "header" file, you can often place it there once to have it appear on every page.
Finding Your Tracking ID in Universal Analytics (for Legacy Users)
Google has officially discontinued Universal Analytics, and all users are encouraged to use GA4. However, you might still need to find your old UA Tracking ID for reference or a third-party integration that hasn't updated to GA4 yet.
Follow these steps to find your UA Tracking ID and code snippet.
Step-by-Step Instructions
1. Go to the Admin Section Just like with GA4, log in to your account and click the "Admin" gear icon at the bottom left.
2. Select Your Universal Analytics Property In the "Property" column, find and select your old Universal Analytics property. These are easily identifiable by their Tracking ID, which starts with "UA-".
3. Click on Tracking Info Still in the "Property" column, click on the "Tracking Info" menu option. A dropdown will appear. Select "Tracking Code" from the list.
4. Find Your Tracking ID and Snippet This page shows you everything you need:
- Tracking ID: Displayed clearly at the top of the page, formatted as UA-XXXXXXXX-X.
- Global Site Tag (gtag.js): The full code snippet is shown in a box right below the ID. You can copy this for manual installation if needed.
Verifying Your Tracking Code is Working
After you’ve added the tracking code to your website, you'll want to make sure it’s installed correctly and sending data. Here are two simple ways to check:
1. Use the Realtime Report
This is the quickest confirmation method.
- Go to your Google Analytics property (either GA4 or UA).
- In the left-hand navigation, go to Reports > Realtime.
- Open your website in a separate browser tab or on your phone. If you can, go to a few different pages.
- Within a minute or two, you should see yourself appear as at least one active user in the Realtime report. If you see activity, the tracking code is working!
2. Use Google Tag Assistant Legacy
Tag Assistant is a free Chrome extension by Google that checks for a variety of Google tags (Analytics, Ads, Tag Manager) on any given page.
- Install the Tag Assistant Legacy (by Google) extension from the Chrome Web Store.
- Navigate to your website homepage and click the Tag Assistant icon in your browser toolbar.
- Enable it for your site and refresh the page.
- The pop-up will show you what Google tags it found. You should see your Google Analytics tag listed. A green or blue icon indicates it's working properly, while red or yellow suggests there might be an issue.
Common Mistakes to Avoid
Installing your tracking code is usually simple, but a few common mistakes can prevent data from being collected correctly.
- Placing the code in the wrong spot: The tracking script must be in the
<head>section of your HTML. Putting it in the<body>can lead to incomplete data tracking, as the code may not load before a visitor leaves the page. - Installing the code on only one page: The script must be present on every single page you wish to track. The best practice is to put it in a global header template file that loads on all pages.
- Duplicate tracking codes: Having two versions of the same tracking code (or both a UA and a GA4 code pointing to different properties) can significantly skew your data, causing issues like artificially low bounce rates and doubled pageview counts. Use Tag Assistant to check for duplicates.
- Syntax errors: When copying and pasting the code manually, be careful not to accidentally alter it. Even removing a single comma or quote can break the script entirely.
Final Thoughts
Finding and generating your Google Analytics tracking code is a foundational step in understanding your website's performance. Whether you're pasting a simple GA4 Measurement ID into a plugin or manually adding the full gtag.js snippet to your site's header, the process is straightforward once you know where to look in the Admin panel.
Once your data starts flowing in, you unlock a wealth of insights. However, analyzing data across Google Analytics, your ad platforms, your CRM, and other tools often involves manual work and clunky spreadsheets. This is why we built Graphed to simplify things. By connecting your Google Analytics and other data sources, we let you create real-time dashboards and reports just by asking questions in plain English. For example, you can ask, "Show me my top traffic sources from Google Analytics that led to sales in Shopify this month," and instantly get a report, saving you hours of manual data wrangling.
Related Articles
What SEO Tools Work with Google Analytics?
Discover which SEO tools integrate seamlessly with Google Analytics to provide a comprehensive view of your site's performance. Optimize your SEO strategy now!
Looker Studio vs Metabase: Which BI Tool Actually Fits Your Team?
Looker Studio and Metabase both help you turn raw data into dashboards, but they take completely different approaches. This guide breaks down where each tool fits, what they are good at, and which one matches your actual workflow.
How to Create a Photo Album in Meta Business Suite
How to create a photo album in Meta Business Suite — step-by-step guide to organizing Facebook and Instagram photos into albums for your business page.