How to Get Google Analytics JavaScript Code
Putting the Google Analytics JavaScript code on your website is the very first step toward understanding how visitors find and use your site. This small snippet is the key that unlocks all your website data. This article will show you exactly where to find your tracking code for both Google Analytics 4 and the older Universal Analytics, and how to install it correctly.
What Exactly Is the Google Analytics Tracking Code?
The Google Analytics tracking code is a block of JavaScript that you place on your website. When a visitor loads a page, this script runs in their browser. It collects anonymous information about the visitor and their session, such as which page they are on, what device they are using, and where they came from. It then sends this information to your Google Analytics account for processing.
You might hear this code referred to by a few different names, which can be confusing:
- Global Site Tag (gtag.js): This is the current framework for the tracking code used by Google Analytics, Google Ads, and other Google products. It's what you'll be looking for in most cases.
- The GA Tag or GA Snippet: A common shorthand for the JavaScript code block.
- Tracking ID or Measurement ID: These are unique identifiers within the code snippet itself (like
UA-12345678-1for Universal Analytics orG-ABCDE12345for GA4) that tell Google which account and property to send the data to.
Simply put, without this code on your site, Google Analytics has no way of knowing who's visiting, and your reports will remain empty. Finding and installing it is a non-negotiable step to gather any website analytics.
How to Find Your Tracking Code in Google Analytics 4
Google Analytics 4 is the current standard, so this is where you'll be working if you've set up analytics recently. The process is straightforward and only takes a few clicks.
Here are the step-by-step instructions:
- Log in to Google Analytics: Head over to the Google Analytics homepage and sign in to your account.
- Navigate to the Admin Panel: Find and click the gear icon labeled 'Admin' in the bottom-left corner of your screen.
- Go to 'Data Streams': You will see two columns: 'Account' and 'Property'. In the 'Property' column, look for the option called 'Data Streams' and click on it. A data stream is essentially a source of data, which in this case is your website.
- Select Your Web Stream: You will see a list of your data streams. Click on the one associated with your website.
- Find the Tag Instructions: On the 'Web stream details' page, look for a section at the bottom titled 'View tag instructions'. Click on it.
- Access Your JavaScript Code: A new 'Installation instructions' panel will appear. Make sure you are on the 'Install manually' tab. You'll see the Global Site Tag (gtag.js) JavaScript code snippet here. Click the copy icon in the top right to copy the entire code block to your clipboard.
The code will look something like this:
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABCDE12345"></script>
<script>
window.dataLayer = window.dataLayer || [],
function gtag(){dataLayer.push(arguments),}
gtag('js', new Date()),
gtag('config', 'G-ABCDE12345'),
</script>Note the unique Measurement ID (starting with "G-") within the script. This is what connects the code to your specific GA4 property.
Finding the Code for Universal Analytics (For Legacy Properties)
Universal Analytics (UA) properties stopped processing new data on July 1, 2023. However, you may still need to find your old tracking code for archival purposes or to manage a very old site. The process is slightly different from GA4.
- Go to the Admin Panel: Log into Google Analytics and click the 'Admin' gear icon.
- Select your UA Property: Make sure you have the correct Universal Analytics property selected in the 'Property' column. UA properties have IDs that start with 'UA-'.
- Open Tracking Info: In that 'Property' column, click on 'Tracking Info' which will expand a new menu.
- Click 'Tracking Code': From the expanded menu, click 'Tracking Code'. This will take you to a page displaying your Global Site Tag (gtag.js) and your unique Tracking ID, which starts with 'UA'.
The Universal Analytics code snippet looks very similar to the GA4 version, but it references a 'UA-' ID instead of a 'G-' ID. Again, keep in mind this code no longer collects new data.
Where to Paste Your Google Analytics Code
Once you've copied your JavaScript code, the next step is getting it onto your website. It needs to be placed on every single page you want to track. The best way to do this is to add it to a sitewide header file, so it loads automatically on every page.
Option 1: Adding the Code Manually (The Direct Method)
This method involves editing your website’s HTML files. You need to paste the Google Analytics code snippet immediately after the opening <head> tag of your website. If you are using WordPress, this could be in a file like header.php. This approach works, but it can be risky for beginners. If you make a mistake, you could break your site. Also, if you update your website theme, your changes might be overwritten and you’ll have to add the code back in.
Option 2: Using a Plugin or Built-in Integration (The Easiest Method)
Most modern website platforms have a simple, built-in way to add Google Analytics without ever touching code. This is the recommended route for most users.
- WordPress: While you can edit your theme files directly, it's safer to use a plugin like "WPCode – Insert Headers and Footers" or "Site Kit by Google". These give you safe fields where you can paste your JavaScript snippet without the risk of breaking your theme. Some themes also come with their own dedicated "Scripts" or "Integrations" section in the theme customizer.
- Shopify: In your Shopify Admin, go to Online Store > Preferences. You’ll find a dedicated "Google Analytics" section where you can simply paste your code or Measurement ID.
- Squarespace: Go to Settings > Advanced > External API Keys. Here you will find a field specifically for your Google Analytics "Measurement ID" (the
G-ABCDE12345part). - Wix: Navigate to Marketing & SEO > Marketing Integrations. You can connect Google Analytics here, typically by providing just your Measurement ID.
Option 3: Using Google Tag Manager (The Best Practice)
Google Tag Manager (GTM) is a free tool that acts as a container for all your website tracking tags, not just Google Analytics. Instead of placing ten different JavaScript snippets into your website's code (for GA, Facebook Pixel, Google Ads, etc.), you place one GTM snippet. Then, you manage all your other tags from inside the GTM interface.
Using GTM is the most scalable and professional way to manage tracking. It keeps your website code clean and allows you to add or modify tracking without needing a developer. To install GA4 via GTM, you’ll add one 'GA4 Configuration' tag within the GTM interface and publish your changes. This is the method most marketers and agencies prefer.
How to Verify Your Installation Is Working
After adding the code, you must confirm it's actually working. There's nothing worse than thinking you're collecting data for weeks only to find out the code was never installed correctly. Here are a few simple ways to check:
1. Use the Realtime Report
This is the quickest and easiest way. Inside Google Analytics, go to Reports > Realtime. Then, open your own website in a different browser tab or on your phone. In a minute or so, you should see yourself appear as a visitor in the Realtime report. If you see activity, it's working!
2. View Page Source
Go to your website, right-click anywhere on the page, and select "View Page Source". This will open a new tab with your site's HTML. Press Ctrl+F (or Cmd+F on a Mac) and search for "gtag.js" or your Measurement ID (e.g., "G-ABCDE12345"). If it appears in the code, it has been installed correctly.
3. Use the Tag Assistant Legacy Chrome Extension
Install the free Tag Assistant Legacy extension for Google Chrome. When you visit your website, click the extension's icon. It will scan the page for any Google tags and show you a report. If it finds your Google Analytics tag and it’s green, you are good to go.
Final Thoughts
Getting your Google Analytics JavaScript snippet is a simple process directly inside the platform's Admin panel. From there, you have several options for installation, with Google Tag Manager being the most robust long-term solution. Taking a final minute to verify that data is being collected is a critical last step to ensure your analytics are working from day one.
Once you install Google Analytics, you’ll have a great stream of data about your website. But that’s just one piece of the puzzle. The big challenge is connecting that data with information from your ad platforms, CRM, and email tools to understand what’s driving real growth. At Graphed, we built a way to eliminate that manual-reporting headache. We make it easy to link all your data sources so you can use simple, plain-English questions to create dashboards and reports - like asking an analyst for help, but getting the answer in seconds.
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.