Where to Put Google Analytics Script?

Cody Schneider9 min read

Adding Google Analytics to your website is one of the first steps to understanding who your visitors are and what they do. But one question stumps a lot of people right at the start: where exactly does that little snippet of code go? This article will show you the right place to put your Google Analytics script and walk you through the most common ways to get it done.

GraphedGraphed

Still Building Reports Manually?

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

Watch Graphed demo video

Understanding the Google Analytics Script

Before we place the script, let’s quickly break down what it is. The Google Analytics script - often called the "Google tag" or gtag.js - is a small piece of JavaScript code. Its job is pretty simple but powerful: it runs in the background on your website, observes how users interact with your pages (like which pages they view, how long they stay, and what buttons they click), and sends all that data to your Google Analytics account.

Placing this script correctly ensures that it can "see" and record every user on every page, giving you a complete picture of your website traffic.

The Golden Rule: Place Your Script in the <head> Tag

If you're looking for the short answer, here it is: The Google Analytics tracking script should be placed inside the <head> section of your website’s HTML code, right before the closing </head> tag.

This is the universally recommended best practice for a few important reasons:

  • It Ensures the Script Loads First: Website code is loaded from top to bottom. Placing the script high up in the <head> ensures it loads and starts running as one of the first things on your page. If you placed it at the bottom of the page (e.g., in the footer), a visitor could click a link and leave your site before the script has a chance to load. If that happens, you miss tracking their visit entirely.
  • It Provides Consistent Site-Wide Tracking: Most website builders and themes use a single header template file that gets loaded on every single page of the site. By placing the code in that one location, you guarantee it’s present across your entire website without having to manually add it to hundreds of pages.

Think of the <head> section as your page's "startup sequence." You want analytics tracking to be part of that initial sequence so it doesn't miss anything that happens afterward.

GraphedGraphed

Still Building Reports Manually?

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

Watch Graphed demo video

Finding Your Google Analytics Tracking Code (Google Tag)

To install the script, you first need to grab it from your Google Analytics account. Here’s how to find the exact snippet for your Google Analytics 4 property.

  1. Log in to your Google Analytics account.
  2. Click on the Admin gear icon in the bottom-left corner.
  3. Make sure the correct Account and Property are selected. In the "Property" column, click on Data Streams.
  4. Select the web data stream for your website (it will be the URL).
  5. Under the section titled "Google tag," click on View tag instructions.
  6. A new page will open. Click the Install manually tab.

You’ll see a box containing a chunk of JavaScript. This is it! 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>

Just click the copy icon in the top right of this box to get it onto your clipboard. Your unique "G-" Measurement ID will already be included in the code.

4 Ways to Add the Google Analytics Script to Your Website

Now that you have your script, let’s look at how to add it. The best method depends entirely on the platform your website is built on.

Method 1: Manually Paste the Code into Your HTML

This method is for people with static HTML websites or those who are comfortable editing their own theme files directly.

  1. Open the HTML file for your page (e.g., index.html) in a code editor.
  2. Find the closing </head> tag. It's usually located near the top of the file.
  3. Paste the entire Google Tag script you copied onto the line just before the </head> tag.
  4. Save your file and upload it to your web server.

Important: If your site uses multiple templates or manually created pages, you will need to repeat this process for every unique page or template file where the header is defined.

GraphedGraphed

Still Building Reports Manually?

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

Watch Graphed demo video

Method 2: Use an Official or Dedicated Plugin (E.g., WordPress)

If you're using a Content Management System (CMS) like WordPress, messing with theme files can be risky. One wrong move could break your site. The easier and safer way is to use a plugin.

For WordPress users, the best option is Site Kit by Google. It's Google's official plugin that not only adds your Analytics script but also connects Search Console, AdSense, and other Google services directly into your WordPress dashboard.

Here’s how to set it up:

  1. In your WordPress admin panel, go to Plugins > Add New.
  2. Search for "Site Kit by Google".
  3. Click Install Now and then Activate.
  4. A setup wizard will launch. Follow the steps to connect your Google account.
  5. When you get to the Google Analytics connection step, Site Kit will find your GA4 property and, with your permission, automatically place the tracking script in the right spot. You don't have to touch a single line of code.

Many other plugins, like "GA Google Analytics," specialize in simply adding the tracking code if you don't need the other Site Kit features.

Method 3: Native Platform Integrations (Shopify, Squarespace, Wix)

Most modern website builders and e-commerce platforms have built-in support for Google Analytics. You don't even need to touch the script itself - you just need your 'G-' Measurement ID.

For a Shopify Store:

  1. From your Shopify admin, go to Online Store > Preferences.
  2. Scroll down to the "Google Analytics" section.
  3. Paste only your Measurement ID (e.g., G-XXXXXXXXXX) into the field and click Save.

Shopify automatically adds the Google tag to your <head> section in the required format.

For a Squarespace Site:

  1. Go to your Squarespace dashboard and navigate to Settings > Advanced > External API Keys.
  2. Find the field for "GOOGLE ANALYTICS MEASUREMENT ID."
  3. Paste your 'G-' Measurement ID in the box and Save.

Just like Shopify, Squarespace handles the code placement for you.

Method 4: The Advanced Route with Google Tag Manager

For marketers and business owners who plan on using other tracking tools besides Google Analytics (like a Facebook Pixel, LinkedIn Insight Tag, etc.), Google Tag Manager (GTM) is the very best approach.

Simply put, GTM acts as a "container" for all your scripts. You install the Tag Manager script on your site once, and then you add, remove, and manage all your other tags from the GTM web interface - no more editing website code.

Setting up Google Analytics via GTM involves:

  1. Creating a Google Tag Manager account and a 'container' for your website.
  2. GTM will provide a script to add to your site's <head>, much like the standard GA script. You only have to do this once.
  3. Inside Tag Manager, you create a new tag, selecting "Google Analytics: GA4 Configuration."
  4. You paste your GA4 'G-' Measurement ID into that tag.
  5. You set a trigger for this tag to "Fire" on "All Page Views."
  6. Publish your container. Your GA tracking is now live!

While it has a slightly higher learning curve, it makes managing dozens of analytics and marketing scripts much more organized in the long run.

GraphedGraphed

Still Building Reports Manually?

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

Watch Graphed demo video

How to Check if It's Working

Once you've added your script, don't just assume it works! Verifying the installation is a critical final step.

The easiest way is to use Google Analytics' own Real-time report:

  1. In your Google Analytics account, navigate to Reports > Real-time report.
  2. In a separate browser window, open your own website.
  3. Within about 30-60 seconds, you should see your own visit pop up in the Real-time report (showing at least 1 user in 'Users in last 30 minutes'). You might also see yourself in the view by 'Country' or 'City' maps.

If you see your activity, congratulations! Your Google Analytics script is installed correctly and is actively collecting data.

Common Mistakes to Avoid

  • Duplicate Scripts: Never install the script more than once. If you add it manually and through a plugin, it will fire twice for every page view, which will corrupt your data by doubling your traffic and incorrectly halving your bounce rate.
  • Placing it in the Body or Footer: As mentioned, placing the script too low in your HTML could cause you to miss tracking visitors who leave your site before the entire page finishes loading. Always stick to the <head>.
  • Forgetting to Paste It: It might sound obvious, but it's easy to copy the script and forget to paste it, or paste it and forget to save the file or publish the changes. Always double-check and verify.
  • Using Old Code: Universal Analytics ('UA') was retired in 2023. If your code starts with 'UA-' instead of 'G-', you have the old, obsolete tracking script. Always make sure you're using the newest gtag.js code from a GA4 property.

Final Thoughts

While there are several ways to get the job done, the rule for where to place your Google Analytics script is simple: inside the <head> tag of your website's code. This ensures timely and complete data tracking on every page. Whether you do this manually, with a plugin, or through a dedicated integration, always remember to verify your installation with the Real-time report to confirm everything is working perfectly.

Getting your tracking set up is the first step, but the real work begins when you have to start analyzing all that data. The standard Google Analytics interface can feel overwhelming, making it hard to get clear answers to simple business questions. When it comes time to actually use your data, we built Graphed to turn hours of data exploration into a single question. We let you connect your Google Analytics account and ask questions in plain English - like "What were my top landing pages last month?" - and instantly get back clean visuals and automated reports.

Related Articles

How to Enable Data Analysis in Excel

Enable Excel's hidden data analysis tools with our step-by-step guide. Uncover trends, make forecasts, and turn raw numbers into actionable insights today!