Where to Embed Google Analytics Code?

Cody Schneider8 min read

Adding the Google Analytics tracking code to your website is the essential first step toward understanding who your visitors are and how they interact with your site. Without it, you’re flying blind. This guide will walk you through exactly what the code is, where it needs to go, and the best methods for getting it set up correctly, whether you're a seasoned developer or using a website builder for the first time.

What is the Google Analytics Tracking Code?

The Google Analytics tracking code, also known as the Global Site Tag (gtag.js), is a small piece of JavaScript that you install on your website. Its job is simple but powerful: whenever someone visits a page on your site, this script activates. It collects non-personally identifiable information about the visitor and their session - like which page they landed on, how they got there, what device they're using, and which buttons they click - and sends all of that data securely to your Google Analytics account.

In Google Analytics 4, this snippet is tied to your unique "Measurement ID," which always starts with "G-". This ID tells the script exactly which Google Analytics property to send the data to. Every view, click, and interaction you see in your GA reports is there because this little snippet of code did its job.

Where to Find Your Google Analytics Code

Before you can add the code, you need to find it. If you've already set up your Google Analytics 4 property, retrieving the code snippet is straightforward.

  1. Log in to your Google Analytics account.
  2. Click on the Admin gear icon in the bottom-left corner.
  3. In the Property column, make sure your desired property is selected.
  4. Click on Data Streams, then select the web data stream for your website. If you don't have one, you'll need to create one first.
  5. Under the "Tagging instructions" section at the bottom, click on View tag instructions.
  6. A new panel will open. Click the Install manually tab. Here you'll find the complete JavaScript snippet you need to copy.

It will look something like this, but with your unique "G-" Measurement ID:

<!-- 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 Golden Rule: Place the Code in the <head> Section

Now for the most important part: The Google Analytics tracking code must be placed in the <head> section of your website’s HTML.

Specifically, it should be placed immediately after the opening <head> tag and before the closing </head> tag on every single page of your website you wish to track. The simple reason for this is performance and accuracy. The browser reads your website's code from top to bottom. By placing the GA snippet in the head, you ensure it loads as early as possible.

This is crucial because if a user visits your page and then quickly navigates away before your page fully loads, a script in the footer might not have had time to run. Placing it in the head guarantees that the tracking script is one of the very first things to execute, capturing that pageview data accurately before the user has a chance to leave.

Method 1: Manually Editing Your Website's HTML

This method involves directly editing your website's files. It's the most direct route but also carries the most risk. You should only use this method if you're comfortable with editing code and have a way to back up your site first.

The goal is to find the central template file that controls your website's header. In many systems, this is a file named header.php, theme.liquid, or a similar layout/master template file.

  1. Access your website's files via FTP or your hosting provider's file manager.
  2. Locate the primary header template file for your theme.
  3. Open the file for editing.
  4. Copy your entire Google Analytics (gtag.js) snippet.
  5. Paste the code snippet immediately after the opening <head> tag or just before the closing </head> tag. Either location works fine, as long as it's within the head section.
  6. Save the file and upload the new version to your server.

Warning: The main drawback of this method is that if you ever update your website's theme, your custom code changes (including your GA snippet) could be overwritten and deleted. This is why a CMS-specific or Google Tag Manager approach is usually recommended.

Method 2: Using Your CMS or Website Builder's Built-in Features

Most modern website platforms know that their users need to install tracking scripts. To make this safe and easy, they provide dedicated fields or plugins to add these codes without ever touching a line of core theme code.

WordPress

For WordPress users, you have a few excellent options that avoid manual theme edits:

  • Via a Plugin: The easiest and safest way is using a dedicated integration plugin. Google's own Site Kit directly connects your Google accounts (Analytics, Search Console, etc.) to your site with a few clicks. Other popular options include MonsterInsights and GA Google Analytics. These plugins not only place the code for you but often add additional reporting features to your WordPress dashboard.
  • Via Theme Options: Many modern themes include a dedicated section in the Theme Customizer (under Appearance > Customize) for adding header and footer scripts. Look for a section called "Header Scripts," "Tracking Codes," "Integrations," or something similar and paste your GA code there.

Shopify

Shopify makes this incredibly easy. There's no need to edit your theme's .liquid file.

  1. From your Shopify admin, go to Online Store > Preferences.
  2. Scroll down to the Google Analytics section.
  3. Paste your GA4 Measurement ID ("G-...") into the field. Shopify's native integration will handle the rest.

Squarespace and Wix

Like Shopify, these popular website builders have built-in areas designed for tracking codes.

  • In Squarespace: Go to Settings > Advanced > Code Injection. Paste your full Google Analytics snippet into the Header box.
  • In Wix: Go to Marketing & SEO > Marketing Integrations. Find the Google Analytics option and connect your account, pasting your Measurement ID when prompted. This will automatically add the tag to all pages.

Method 3: Using Google Tag Manager (The Professional's Choice)

Google Tag Manager (GTM) is a free tool that acts as a "container" for all of your marketing and analytics scripts (called "tags"). Instead of adding multiple scripts to your site's code (one for GA, one for the Facebook Pixel, one for a heatmap tool, etc.), you add just one GTM snippet. Then, you manage all your other tags from within the user-friendly GTM interface.

This is the best-practice method for a few key reasons:

  • Simplicity: You only inject one script into your site's code. This keeps your site loading faster and makes it easier to manage.
  • Control: Marketers can add, edit, and remove tracking tags without needing a developer to edit site code.
  • Power: GTM unlocks highly advanced tracking capabilities with triggers, variables, and event tracking that go far beyond standard pageviews.

The High-Level GTM Setup for Google Analytics:

  1. Create your Google Tag Manager account and container. GTM will give you its own code snippet.
  2. Install this GTM snippet in the <head> of your website (using any of the manual or CMS-specific methods described above).
  3. Inside your GTM dashboard, create a new Tag and select the type: Google Analytics: GA4 Configuration.
  4. Paste your GA4 Measurement ID ("G-...") into the required field.
  5. Under the "Triggering" section, choose the "All Pages" trigger to make the tag fire on every pageview.
  6. Save your tag, test it using GTM's "Preview" mode, and when you're ready, click "Submit" to publish your container.

Your Google Analytics tag is now live on your site, managed cleanly through GTM.

How to Verify Your Installation

Don't just set it and forget it. After installing your code, you need to confirm it's working properly.

  • Check Realtime Reports: The easiest way is to open your live website in one browser tab and your Google Analytics Realtime report in another. You should see yourself appear as at least one active user within a minute or two.
  • Use a Browser Extension: Install the GA Debugger or related third-party Chrome extensions that allow you to inspect tracking events. They'll tell you instantly if a Google Analytics tag is found and if it fired successfully.
  • Inspect the Page Source: Right-click on your webpage and select "View Page Source." Use your browser's find function (Ctrl+F or Cmd+F) to search for your Measurement ID ("G-...") to see if the script exists in the HTML.

Final Thoughts

Placing your Google Analytics snippet in the <head> section of every page on your site is the foundational step for accurate web analytics. Whether you do it manually, use a CMS integration, or adopt the professional standard with Google Tag Manager, getting this part right ensures you’re capturing the visitor data you need to make informed decisions.

Once your data starts flowing into Google Analytics, the next step is turning it into insights. Instead of spending hours digging through GA's complex reports, we created an easier way. With Graphed, you can connect your GA account in one click and use plain English to build dashboards and get answers, like "Show me a chart of my top traffic sources last month" or "Which blog posts are most popular?" We automate the reporting so you can focus on growing your business.

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.