Where to Put Google Analytics Code?

Cody Schneider8 min read

Adding Google Analytics to your website is the very first step toward understanding how visitors find and interact with your content. To start collecting that data, you need to install a special piece of tracking code. This article will show you exactly what that code is, where to find it, and the four best ways to place it on your site so you can start tracking with confidence.

What Exactly Is the Google Analytics Tracking Code?

The Google Analytics tracking code is a small snippet of JavaScript that you add to your website. When a user visits a page, this script runs in their browser, collects anonymous information about their session (like the page they're viewing, their device type, and where they came from), and sends it to your Google Analytics account.

For the latest version, Google Analytics 4, the code is called the Google tag (or gtag.js). It's designed to be the single tagging solution for all of Google's marketing and analytics products. You might occasionally see references to the older analytics.js (Universal Analytics) or ga.js (Classic Google Analytics), but for any new setup, you will be using a GA4 Google tag.

A typical GA4 tracking code snippet looks 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>

The most important part of this code is the "G-XXXXXXXXXX" string. This is your unique Measurement ID, which tells Google which specific GA4 property to send the data to.

How to Find Your Google Analytics Code

Before you can add the code, you need to find it in your Google Analytics account. If you just created a new GA4 property, Google usually shows you the code right away. If not, here’s how to track it down.

  • Step 1: Log into your Google Analytics account.
  • Step 2: Click on the 'Admin' gear icon in the bottom-left corner of the screen.
  • Step 3: Make sure you have the correct account and property selected in the top columns. In the 'Property' column, click on Data Streams.
  • Step 4: Click on the specific Web data stream for the website you want to track. The stream name usually includes your website URL.
  • Step 5: Look for the "Google tag" section at the bottom and click on View tag instructions.
  • Step 6: A new screen will appear. Under the tab 'Install with a website builder or CMS', it may show platform-specific instructions, but for the raw code, click the Install manually tab.

Here you will find the complete JavaScript snippet. Click the copy icon in the top right to copy the entire code to your clipboard.

The Golden Rule: Place the Code in the <head> Section

Google’s official recommendation is to place the tracking code in the <head> section of your website’s HTML. Specifically, you should paste it immediately after the opening <head> tag or just before the closing </head> tag on every single page you want to track.

Why is this so important? The <head> section of a webpage is one of the first things a browser loads. Placing the Analytics script here ensures it has a chance to run and send data for every visitor, even those who click away from the page quickly. If you were to place it at the bottom of the page (in the footer, for example), a user might leave before the browser ever gets to the script, meaning their visit would never be recorded.

Now, let's look at the practical ways to get this done.

Four Ways to Add the Google Analytics Code to Your Website

There isn't a single "best" way for everyone. The right method depends on your technical comfort level and the platform your website is built on. Here are the four most common and effective approaches.

Method 1: Manually Adding the Code to Your Site's HTML Files

This is the most direct method and works for any website, whether it's a simple static site or a complex content management system (CMS). It involves editing your website’s core template files. This is best suited for those comfortable looking at code.

  • For Static HTML Websites: You will need to open every single .html file and paste the GA code just before the closing </head> tag.
  • For WordPress: You'll want to find the header.php file in your theme’s folder. You can access this via an FTP client or through your hosting provider's file manager. IMPORTANT: If you edit the main theme file directly, your changes will be erased the next time the theme updates. It's much safer to use a child theme and copy the header.php file there to edit it.
  • For Shopify: Look for the theme.liquid file in your theme code. Go to Online Store > Themes > … (Actions) > Edit Code. This file acts as the primary template for every page. Paste the code before the </head> tag. However, Shopify has a better, built-in method we'll cover later.

Pros: No plugins needed, you have full control. Cons: Can be easily overwritten by theme updates, risky if you aren't comfortable with code.

Method 2: Using a WordPress Plugin (The Easiest Way)

For the millions of websites running on WordPress, using a plugin is by far the simplest and safest way to add your Analytics code. These plugins correctly place the code for you and ensure it doesn't get removed when you update your theme.

Your two main options are:

  • Dedicated Google Analytics Plugins: Tools like Google’s official "Site Kit", "GA Google Analytics", or "MonsterInsights" simplify the process. Instead of asking you for the full code snippet, they typically just ask for your Measurement ID (G-XXXXXXXXXX). You just install the plugin, connect your Google account, and it handles the rest. Many also provide simple dashboards within WordPress.
  • Header and Footer Scripts Plugins: General-purpose plugins like "WPCode - Insert Headers and Footers". These plugins give you a simple box where you can paste any script (including the GA code snippet) and tell it to load in the header globally.

This method comes highly recommended for most WordPress users due to its simplicity and safety.

Method 3: Using Google Tag Manager (The Most Powerful Way)

If you plan on adding more than just Google Analytics to your site (like Facebook Pixel, Google Ads tags, or heat-mapping scripts), then Google Tag Manager (GTM) is the professional choice.

Think of GTM as a "container" for all your tracking scripts. Instead of adding a dozen different code snippets directly to your website, you add just one: the GTM container code. Then, you manage all your individual scripts from inside the GTM interface.

The high-level steps are:

  1. Create a Google Tag Manager account and a container for your site.
  2. Install the GTM container code on your website (just like in Method 1). This is a one-time setup.
  3. Inside your GTM account, create a new tag. Choose the tag type Google Analytics: GA4 Configuration.
  4. Paste your Measurement ID into the appropriate field.
  5. Set the "Trigger" for this tag to fire on "All Page" views.
  6. Preview and publish your GTM container, and your GA tracking is live.

Pros: Keeps your site code clean. Easy to manage and add multiple tracking tags. Gives you extreme flexibility. Cons: A slight learning curve compared to simple plugins.

Method 4: Using Your Website Builder's Built-In Features

Many popular website platforms have the option for analytics built-in or provide an easy way to integrate with Google Analytics, usually just by entering your Measurement ID. You do not have to touch the code snippet at all in these scenarios. Just look for the designated spot in the platform's settings:

  • For Shopify: Go to Online Store > Preferences, under the Google Analytics section, paste your Measurement ID there.
  • Squarespace: Navigate to Settings > Advanced > External API Keys, and enter your ID there.
  • Wix: Go to Marketing Integrations > Google Analytics and follow the prompts.

This is almost always the best approach for websites built with these platforms.

Final Thoughts

Installing Google Analytics tracking code is a vital but easy task that every website owner must undertake. By implementing the methods above, whether manually, via plugin, using Google Tag Manager, or through a website builder, you're on your way to accurately tracking visitor data and making informed decisions to optimize your online presence.

Remember that measuring site performance is just the start of improving what you offer to users. Use Google Analytics to regularly assess trends and adapt your website as needed. Finally, for those looking to streamline their content management, consider using a tool like Graphed to keep organized and save time managing your site's data effectively.

Related Articles

How to Connect Facebook to Google Data Studio: The Complete Guide for 2026

Connecting Facebook Ads to Google Data Studio (now called Looker Studio) has become essential for digital marketers who want to create comprehensive, visually appealing reports that go beyond the basic analytics provided by Facebook's native Ads Manager. If you're struggling with fragmented reporting across multiple platforms or spending too much time manually exporting data, this guide will show you exactly how to streamline your Facebook advertising analytics.

Appsflyer vs Mixpanel​: Complete 2026 Comparison Guide

The difference between AppsFlyer and Mixpanel isn't just about features—it's about understanding two fundamentally different approaches to data that can make or break your growth strategy. One tracks how users find you, the other reveals what they do once they arrive. Most companies need insights from both worlds, but knowing where to start can save you months of implementation headaches and thousands in wasted budget.