How to Generate Google Analytics Code for Website

Cody Schneider••9 min read

Getting your website analytics set up is the first step toward understanding who your visitors are and what they care about. This article will guide you through the entire process of generating your Google Analytics tracking code and getting it installed on your site, even if you're not a technical person.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

What is the Google Analytics Code and Why Do You Need It?

The Google Analytics code, also known as the tracking code or the Global Site Tag (gtag.js), is a small snippet of JavaScript that you add to your website. When a person visits one of your pages, this script runs in their browser, collecting anonymous data about their visit - like their geographic location, how they found your site, and which pages they viewed - and sends it to your Google Analytics account.

Without this code, your Google Analytics account is a blank slate, it has no way of seeing the traffic coming to your website. Installing it unlocks crucial insights you can use to:

  • Understand Your Audience: Find out where your visitors are from, what language they speak, and what devices they use (desktop vs. mobile).
  • See What Content Works: Discover which blog posts, landing pages, or products are the most popular with your audience.
  • Track Marketing Performance: See which channels, from social media to paid ads to email newsletters, are actually driving traffic and conversions.
  • Identify Website Issues: Find out which pages have high exit rates, signaling that you may need to improve the content or user experience.

In short, it's the connection that allows your website to "talk" to Google Analytics. Getting it set up correctly is the most important part of the process.

A Quick Note on GA4 vs. Universal Analytics

As you start this process, you may see older tutorials mention "Universal Analytics" or tracking IDs that start with "UA-". That was the previous generation of Google Analytics. As of July 2023, Google has officially moved on to Google Analytics 4, and all new accounts are created as GA4 properties.

This is great news for you, as you're starting fresh with the latest and most powerful version. GA4 is designed to be more flexible, user-centric, and privacy-focused. The code we generate in this guide will be for a GA4 property, which uses a "Measurement ID" that starts with "G-" instead of a "UA-" tracking ID.

Step-by-Step: How to Generate Your Google Analytics 4 Code

Generating the code only takes a few minutes. All you need is a Google account. Follow these steps to create your account and data stream.

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: Sign In and Create a New Account

First, navigate to the Google Analytics homepage. You can sign in with your existing Google account (the one you use for Gmail, Google Drive, etc.). If you don't have one, you'll need to create one first.

Once you're logged in, click the blue "Start measuring" button. This will begin the setup process for a new account.

Step 2: Set Up Your Account Details

This first screen is for the "Account" level. An account is the highest level of organization in Google Analytics. If you manage multiple businesses, you might have a separate account for each. For most users, you'll just have one account for your primary business.

Enter a name for your account - typically your business name. Below that, you'll see "Account Data Sharing Settings." These control what data you share with Google. The default settings are fine for most users. Click "Next" when you're ready.

Step 3: Create a Property

The next level down is the "Property". A property represents a specific website or app. If your business has a website AND a mobile app, you would create a separate property for each under the same account. For now, we're just focused on your website.

Here's what you'll need to do:

  • Property name: Enter the name of your website (e.g., "My Business Website").
  • Reporting time zone: Select the time zone you operate in. This ensures your daily reports align with your actual day.
  • Currency: Choose the currency your business uses. This is especially important if you plan on tracking e-commerce revenue.

Click "Next" to continue.

Step 4: Provide Your Business Details

Google asks for some basic information about your business. This helps them tailor your experience and provide relevant benchmarking data.

  • Industry Category: Choose the category that best fits your business.
  • Business size: Select the number of employees.

Click "Next" once you've made your selections.

Step 5: Choose Your Business Objectives

In this step, tell Google why you're using Analytics. Are you trying to generate leads? Drive online sales? Build brand awareness? You can check all that apply.

Choosing your objectives helps GA4 customize the reports it shows you upfront, making it easier to find the information you care about most. Once you've selected your objectives, click "Create" and accept the Google Analytics Terms of Service Agreement.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Step 6: Set Up a Data Stream

Your account and property are now created! The final mandatory step is to create a "Data Stream." This is the source of data that flows into your property. Basically, it's you telling GA4, "I want to track a website."

You'll be prompted to choose a platform. Select "Web".

On the next screen, you'll enter:

  • Website URL: The full address of your website (e.g., www.yourwebsite.com). Make sure to select https or http correctly.
  • Stream name: Give it a descriptive name, like "Main Website" or your site's name.

Enhanced Measurement should be enabled by default. This is a powerful GA4 feature that automatically tracks common interactions like page views, clicks, and scrolls without any extra setup. It's best to leave this on.

Click "Create stream."

Finding Your Google Analytics Code (Global Site Tag)

As soon as you create the stream, a new window will pop up with your "Installation instructions." This is where you'll find your tracking code. Relax, you're almost there! You'll see two main options under a tab called "Install automatically" and "Install manually."

1. Install a tag manually

Click on the "Install manually" accordion. Here you'll see a box with a chunk of code. This is your Global Site Tag (gtag.js). It will look something like this:

<!-- Global site tag (gtag.js) - Google Analytics -->
<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>

This entire snippet of code needs to be placed in the <head> section of every page on your website. If you have a custom-coded site, you copy this and paste it. If you use a platform like WordPress, however, there's usually an easier way.

2. Use Your Measurement ID

Most modern website builders and CMS platforms don't require you to copy-paste the full code. Instead, they have a dedicated field where you just need to enter your Measurement ID.

You can find this ID at the top right of the "Web stream details" window. It's the one that starts with "G-" (e.g., G-XXXXXXXXXX).

How to Add the Google Analytics Code to Your Website

Now that you have your code, it's time to install it. The method varies depending on what platform your website is built on.

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.

WordPress

For WordPress users, the easiest and safest method is to use a plugin.

  • Google's Official Site Kit Plugin: This is a great option. Install the plugin, and it will guide you through connecting your Google account. It automatically detects your GA4 property and installs the code for you, without you ever having to see a line of code.
  • Other Plugins (like MonsterInsights or GA Google Analytics): These plugins offer a simple settings page where you can just paste your "G-" Measurement ID and save.
  • Theme Settings: Many modern themes have a built-in section in the theme customizer for "Scripts" or "Integrations," where you can paste the full Global Site Tag snippet or just the ID.

Advanced users: You can also add the code directly to your theme's header.php file, but this is not recommended for beginners. If you do this, always use a child theme so your changes aren't wiped out when you update your main theme.

Shopify

Shopify makes this incredibly easy. There's a dedicated spot for Google Analytics built right in.

  1. From your Shopify Admin, go to Online Store > Preferences.
  2. Scroll down to the "Google Analytics" section.
  3. You'll see a box that says "Paste your Google Analytics code here from Google." Paste your full Global Site Tag snippet here.
  4. Click Save.

Shopify will handle the rest, ensuring the code is on all your pages, including your checkout.

Squarespace

Squarespace has a built-in field specifically for your Measurement ID.

  1. In the main menu, go to Settings > Advanced > External API Keys.
  2. In the "Google Analytics" field, paste your Measurement ID "G-...".
  3. Click Save.

Do not paste the full code snippet here, just the ID is needed.

Wix

On Wix, you connect Google Analytics through Marketing Integrations.

  1. Go to your site's dashboard.
  2. Click Marketing & SEO > Marketing Integrations.
  3. Under "Google Analytics," click "Connect".
  4. Paste your Measurement ID "G-..." into the pop-up window and click Save.

How to Verify Your Google Analytics Installation

Once you've installed the code, you need to make sure it's working. Don't just assume it is! This is easy to check.

  1. Go back to your Google Analytics account.
  2. In the left-hand navigation, go to Reports > Realtime.
  3. Open a new browser tab or use your phone and visit your own website.
  4. Look at the Realtime report in Google Analytics. Within about a minute, you should see at least one "user" appear on the map and in the data cards.

If you see your own visit pop up, congratulations! You have successfully installed Google Analytics. Data will now start populating in your reports. Note that it can take 24-48 hours for standard reports to fully process new data.

Final Thoughts

Generating and installing your Google Analytics code is a foundational step for making data-driven decisions about your website. By taking these few minutes to get set up, you've unlocked a powerful tool for understanding your audience and measuring what matters for your business.

Of course, once data starts flowing into Google Analytics, the next challenge becomes making sense of it all. At Graphed, we make this part easier. Instead of getting lost inside GA4's interface, you connect your account once and then use simple, natural language to get answers and build dashboards. Just ask, "Show me my top 10 landing pages from organic search last month," and we instantly create the report for you, turning hours of analysis into a 30-second conversation.

Related Articles