How to Install Google Analytics Code
Ready to go from 'guessing' what works on your website to 'knowing'? Installing the Google Analytics code is your very first step toward understanding your audience and making data-backed decisions. This guide will walk you through setting up your Google Analytics 4 property and adding the tracking code to your site using several different methods, from the most basic to the most advanced.
First, a Quick Primer: What Is This Code Exactly?
The "Google Analytics code" is a small piece of JavaScript that you place on every page of your website. This script, officially called the Google tag (gtag.js), communicates with Google's servers, sending valuable information about user activity back to your Analytics account.
Each time someone visits a page, the script runs and collects anonymous data, including things like:
- How they arrived on your site (e.g., from a Google search, Facebook link, or another referring site).
- How long they stayed on a page.
- Which pages they visited.
- What device and browser they used.
- Their general geographic location (city, country).
In Google Analytics 4, this script is associated with a unique identifier called a Measurement ID, which looks something like G-XXXXXXXXXX. This ID ensures that the data from your website gets sent to your correct Google Analytics property.
Free PDF · the crash course
AI Agents for Marketing Crash Course
Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.
Step 1: Create Your Google Analytics 4 Property
Before you can install any code, you need a place for the data to go. This means creating a GA4 account and a "property" for your website. If you already have an account, you can skip to creating a property.
- Go to the Google Analytics website and sign in with your Google account.
- Click the "Admin" gear icon in the bottom-left corner.
- In the Account column, click "Create Account." If you already have an account, you can select it and click "Create Property" in the adjacent column.
- Account Setup: Give your account a name. This is usually your business or organization's name. Manage your data sharing settings and click "Next."
- Property Setup: Name your property (usually your website's name), select your reporting time zone, and choose your currency. Click "Next."
- Business Details: Provide some optional information about your industry and business size. This helps Google provide more relevant benchmark data. Click "Next."
- Business Objectives: Choose what you want to achieve with Analytics, like "Generate leads" or "Drive online sales." This helps tailor the reports in your GA4 account. Click "Create."
- Choose a Platform: You'll be asked to set up a "data stream." Since you're tracking a website, click on "Web."
- Set up Web Stream: Enter your website's URL (e.g.,
https://www.yourwebsite.com) and give your stream a name (again, usually just your website's name is fine). Ensure the "Enhanced measurement" toggle is on, as this automatically tracks common events like outbound clicks and scrolls. Click "Create stream."
Once you click "Create stream," you'll be taken to the Web stream details page. This is where you'll find your tracking code. A window titled "Install your Google tag" will pop up automatically. Here, you'll see your Measurement ID at the top right and different installation instructions.
Step 2: Choose Your Installation Method
This is where things can differ based on how your website is built. We'll cover the three most common methods, from easiest to most flexible.
Method 1: Using a Website Builder or CMS Integration (The Easiest Way)
Most modern website platforms like Shopify, WordPress, Squarespace, and Wix have built-in integrations for Google Analytics. This is by far the simplest path, as it doesn't require touching any code. You typically just have to copy your Measurement ID (G-XXXXXXXXXX) and paste it into a specific field in your website's settings.
For Shopify:
- From your Shopify Admin dashboard, go to Online Store > Preferences.
- In the Google Analytics section, you'll see a box. Before pasting anything, you may need to click "Manage pixel here" and add the "Google & YouTube" app to your sales channels if you haven't already.
- Follow the steps inside the Google & YouTube app to connect your Google account. Shopify has a fairly guided process that makes it simple to ensure your account is connected correctly.
- Once connected, Shopify will handle the placement of the tag completely.
For WordPress:
WordPress offers two primary ways to do this: through a theme setting or, more commonly, with a plugin.
- Google Site Kit: This is Google's official plugin for WordPress users. It not only installs the Analytics code but also connects other Google services (like Search Console and AdSense) and displays their data directly in your WordPress dashboard. Simply install the plugin, activate it, and follow the on-screen setup wizard to connect your Google account.
- Other Plugins: Many other plugins, like MonsterInsights or GA Google Analytics, can also handle the installation. You'll install the plugin, go to its settings page, and paste your Measurement ID into the designated field.
For Squarespace or Wix:
These platforms also have straightforward, built-in solutions.
- In Squarespace: Go to Settings > Marketing > Marketing Tools (or similar), and look for an option that says "Google Analytics." You will find a field to paste your Measurement ID.
- In Wix: Go to Marketing & SEO > Marketing Integrations. Under "Google Analytics," click "Connect" and follow the prompts to paste your Measurement ID.
Method 2: Directly Adding the Tag to Your Website's Code (The Manual Way)
If you have a custom-built website or are comfortable editing HTML files, this is the most direct approach. In your GA4 Web stream details, click "Install manually."
Google will give you a block of JavaScript code (the gtag.js code snippet). It will look something like this:
<!-- 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>Here's what to do:
- Copy the entire code snippet provided by Google Analytics.
- Open the HTML files for every page on your website you want to track. If your site uses a template or header file that's included on every page, you'll only need to edit that one file.
- Paste the code snippet immediately after the opening
<head>tag on each page. Placing it high up in the<head>ensures it loads and runs as early as possible. - Save and upload the updated file(s) to your web server.
Warning: Be very careful when directly editing site files. A small syntax error can break your website's layout or functionality. Always keep a backup of the original file before making changes.
Method 3: Using Google Tag Manager (The Best Practice Way)
Google Tag Manager (GTM) is a free tool that acts as a container for your various tracking codes (called "tags"), including Google Analytics. Instead of adding many different scripts to your site, you add the GTM container code once, and then manage all your tags from the GTM interface.
This is the recommended method for anyone serious about marketing analytics because it keeps your website code clean and allows you to add or modify tracking tags without needing a developer.
Here's the process at a high level:
- Set up a GTM Account: If you don't have one, go to tagmanager.google.com and create an account and a container for your website.
- Install the GTM Container Script: Just like the manual method for GA, GTM will provide you with two code snippets. One goes high in the
<head>of your site, and the other goes just after the opening<body>tag. You only have to do this once. - Create a New Tag in GTM:
- Save, Preview, and Publish: Save your tag. It is highly recommended to use GTM's "Preview" mode to test that the tag is firing correctly on your live site. Once confirmed, click "Submit" to publish your container and make the changes live.
Free PDF · the crash course
AI Agents for Marketing Crash Course
Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.
Step 3: Verify That It's Working
After installing the code, you want to be sure it's actually collecting data. Wait a few minutes (sometimes it can take longer), then check your setup using these methods:
- GA4 Realtime Report: The easiest way. Go to your Google Analytics property and navigate to Reports > Realtime. Open your website in a new tab or on your phone. If everything is set up correctly, you should see yourself pop up as a user on the map within a minute.
- Google Tag Assistant: Tag Assistant is a Chrome extension that shows you which Google tags are firing on a page. Install it, visit your website, click the extension icon, and hit "Enable." Refresh the page, and it should show you if your Google Analytics tag was found and if it fired successfully.
Final Thoughts
Setting up Google Analytics is the foundation of digital marketing and website management. By creating a GA4 property, choosing the right installation method for your website—whether it's using a simple CMS integration, manually adding the code, or using the powerful Google Tag Manager—and verifying the setup, you've unlocked the ability to understand how users interact with your site.
Of course, getting the data flowing is only the first step. The real challenge is turning all of those clicks, sessions, and events into clear, actionable insights. Traditionally, that has meant grappling with complex reports and manual analysis. Using AI, we designed Graphed to solve this by letting you have a conversation with your data. By connecting your Google Analytics and other sources, we enable you to instantly build dashboards and get answers using plain English, giving you actionable insights in seconds, not hours.
Related Articles
Facebook Ads for Florists: The Complete 2026 Strategy Guide
Learn proven Facebook advertising strategies for florists in 2026. Target the right audience, create compelling visuals, and optimize your ad budget for maximum ROI.
Facebook Ads For Dental Practices: The Complete 2026 Strategy Guide
Learn how to effectively use facebook ads for dental practices to attract new patients to your dental practice. This comprehensive 2026 guide covers targeting, budgeting, creative strategies, and ROI expectations.
Test: Facebook Ads For Dentists 2026
Test excerpt