How to Paste Google Analytics Code in Website

Cody Schneider8 min read

Getting Google Analytics on your website is one of the first - and most important - steps to understanding your audience. It unlocks a treasure trove of data about who your visitors are, how they found you, and what they do on your site. This guide will walk you through exactly where to find your Google Analytics tracking code and the best ways to install it, no matter which website platform you use.

GraphedGraphed

Still Building Reports Manually?

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

Watch Graphed demo video

What Exactly Is the Google Analytics Tracking Code?

In short, the Google Analytics tracking code (also called the GA tag or gtag.js) is a small snippet of JavaScript that you add to your website. When a visitor lands on a page, this code runs in their browser. It gathers anonymous information about the visit - like which page they're on, what kind of device they're using, and which channel they came from (e.g., Google search, a Facebook ad, or a link from another site).

All this information is then sent directly to your Google Analytics account, where it’s processed and organized into neat reports. Without this code, Google Analytics has no way of knowing what’s happening on your site. It’s the essential connection between your website and your analytics reports.

If you're setting up Analytics today, you'll be using the latest version, Google Analytics 4. It uses the "global site tag" or gtag.js. You might see references to older tags like analytics.js (for Universal Analytics), but those are now outdated. All you need to worry about is the gtag.js script and its unique Measurement ID, which starts with "G-".

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

Before you can install the code, you need to find it. This only takes a minute inside your Google Analytics account. If you haven't created a GA4 property yet, you'll be prompted to do so first.

Here’s how to locate your unique code:

  1. Log in to your Google Analytics account.
  2. Click on the Admin gear icon in the bottom-left corner of the screen.
  3. In the Property column, make sure your desired GA4 property is selected from the dropdown menu.
  4. Click on Data Streams in the Property column.
  5. You should see your website listed as a data stream. Click on it to open the details.

Here, you'll find two key pieces of information:

  • Measurement ID: This is a unique identifier that looks like G-XXXXXXXXXX. Some platforms and plugins will only ask for this ID.
  • Global Site Tag (gtag.js): For most installations, you'll need the full JavaScript snippet. Scroll down and click on View tag instructions. Under the "Install manually" tab, you'll see the full code. It looks like this:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YOUR_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [],
  function gtag(){dataLayer.push(arguments),}
  gtag('js', new Date()),

  gtag('config', 'G-YOUR_MEASUREMENT_ID'),
</script>

Keep this page open or copy the full script to a text editor. We're about to put it to work.

How to Install the Google Analytics Code on Your Website

The "right" way to add the code depends entirely on the platform your website is built on. Below are the most common methods, from manual HTML editing to easy platform-specific integrations.

Method 1: Manually Paste the Code into Your Website's <head>

This method is for basic HTML websites or for tech-savvy users who are comfortable editing code files directly.

The rule is simple: the gtag.js tracking script should be placed immediately after the opening <head> tag on every single page of your site.

  1. Copy the entire Global Site Tag snippet from your Google Analytics account.
  2. Access your website's HTML files. If your site has a file like header.php or a header template that's included on every page, you only need to edit it in one place.
  3. Paste the tracking code right after the opening <head> tag.
<!DOCTYPE html>
<html>
  <head>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-YOUR_MEASUREMENT_ID"></script>
    <script>
      window.dataLayer = window.dataLayer || [],
      function gtag(){dataLayer.push(arguments),}
      gtag('js', new Date()),

      gtag('config', 'G-YOUR_MEASUREMENT_ID'),
    </script>
    <meta charset="utf-8">
    <title>My Awesome Website</title>
    <!-- ... other head elements ... -->
  </head>
  <body>
    <!-- ... page content ... -->
  </body>
</html>
  1. Save and upload the updated file(s) to your server.

Warning: If you are using a CMS like WordPress, it's not recommended to directly edit theme files like header.php, as your changes may be wiped out the next time the theme updates.

GraphedGraphed

Still Building Reports Manually?

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

Watch Graphed demo video

Method 2: Using WordPress Plugins or Theme Settings

WordPress powers over 40% of the web, and thankfully, it offers several user-friendly ways to add your Analytics code without touching a line of PHP.

With a Dedicated Plugin (Recommended)

Plugins are the safest and easiest way for most WordPress users.

  • Site Kit by Google: This is Google’s official plugin. You simply install it, connect it to your Google account, and it handles everything for you automatically - not just for Analytics, but for Search Console and AdSense as well.
  • MonsterInsights: A popular freemium plugin focused on making analytics easy. Installation involves a guided setup where you authenticate your Google Account, and it does the rest. It also adds a helpful reporting dashboard inside WordPress.
  • Insert Headers and Footers by WPCode: A simple, lightweight plugin that provides a box where you can paste scripts into your site's header. Just copy the full gtag.js snippet from GA and paste it into the "Header" section of the plugin's settings.

Using Your Theme's Built-in Options

Many modern WordPress themes come with a dedicated section for adding tracking scripts. Look in your theme's options panel, which is often found under Appearance > Customize or in its own separate settings menu in the WordPress sidebar. You'll often find a box where you can paste the header script.

Method 3: On Shopify

Shopify makes it incredibly straightforward to track sales and visitor activity.

  1. From your Shopify admin, go to Online Store > Preferences.
  2. Scroll down to the Google Analytics section.
  3. Paste your entire Global Site Tag (gtag.js) snippet into the box.
  4. Click Save.

Shopify will automatically add the code correctly across your store, including on the checkout pages, to ensure full eCommerce tracking.

Method 4: On Squarespace or Wix

These platforms also have built-in integrations that only require your Measurement ID.

For Squarespace:

  1. From the Home Menu, click Website, then click Website Tools.
  2. Click External API Keys.
  3. In the Google Analytics field, paste just your Measurement ID (G-XXXXXXXXXX).
  4. Click Save.

For Wix:

  1. Go to Marketing & SEO in your site's dashboard.
  2. Under Marketing Integrations, click Google Analytics.
  3. Click Connect in the top-right corner.
  4. Enter your Measurement ID and click Save.

Method 5: The Advanced Way with Google Tag Manager

Google Tag Manager (GTM) is a free tool that acts as a container for all your third-party scripts (like Analytics and Facebook tags). While there’s a learning curve initially, it saves time in the long run and provides more flexibility.

This is the most flexible, scalable, and recommended method for any business that plans on growing.

The process is a bit different:

  1. Instead of installing the GA code directly on your site, you first install the GTM container code (which you get from GTM).
  2. Inside your Google Tag Manager account, you create a new tag.
  3. For the tag type, choose Google Analytics: GA4 Configuration.
  4. In the tag settings, paste your GA4 Measurement ID.
  5. Set the tag to trigger on "All Pages".
  6. Save and publish your GTM container.

Now, GTM is managing your GA tag for you.

GraphedGraphed

Still Building Reports Manually?

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

Watch Graphed demo video

How to Verify Your Google Analytics Tag is Working

Once you’ve installed your code, don't just assume it’s working. You need to check. Here are three simple ways to confirm the tag is active on your site and data is being sent to Google Analytics.

1. Check the Realtime Report

This is the fastest and easiest test.

  • Open your Google Analytics dashboard.
  • In the left-hand navigation, go to Reports > Realtime.
  • In a separate browser window or on your phone, browse your site and visit several pages.
  • Back in the Realtime report, you should see at least "1" in the "Users in last 30 minutes" card.

If you see your own activity, the tag is working!

2. Use the Tag Assistant Companion Chrome Extension

Tag Assistant is a free browser extension from Google that checks for tracking tags on any webpage.

  • Install the Tag Assistant Companion to your Chrome browser.
  • Navigate to your website and click the Tag Assistant icon.
  • You should see your GA4 Measurement ID listed. If it appears in blue, it's working correctly. Any errors will display in red, and you can troubleshoot from there.

3. View Your Website's Source Code

This method is straightforward and effective.

  • Right-click on any page of your website and select View Page Source.
  • A new tab will open with your site's HTML. Use the Find feature (usually Ctrl + F or Command + F) to search for gtag.js.
  • If the snippet is present, it confirms the code is included on your site.

Final Thoughts

Setting up your Google Analytics tracking code is a foundational step for making data-driven marketing and business decisions. Once it's correctly installed, data will begin to flow into your reports, providing you with a starting point for understanding your website's performance and learning what truly resonates with your audience.

Of course, collecting the data is just the beginning, the real challenge is turning it into clear insights quickly. That's exactly why we built Graphed. Instead of getting lost trying to build custom reports in the Google Analytics interface, you can connect your GA account to our platform and create entire dashboards using simple, natural language. Just ask, "Show me traffic sources and conversion rates for the last 30 days," and we instantly generate the visualizations you need, allowing you to get answers in seconds, not hours.

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!