How to Create Google Analytics Code
Getting your Google Analytics code is the vital first step to understanding what’s happening on your website. This small snippet of JavaScript is the bridge that connects visitor activity on your site to the powerful reports waiting for you in Google Analytics. This guide will walk you through exactly how to create a new Google Analytics 4 property and find the code you need to start tracking your traffic.
What Exactly Is the Google Analytics Code?
When people talk about the "Google Analytics code," they're usually referring to one of two things, and it's helpful to know the difference. Both are found in the same place, but they're used in slightly different ways.
- The Measurement ID: This is a unique identifier for your website's data stream in Google Analytics 4. It looks something like G-XXXXXXXXXX. You'll typically use this ID when you're using a simple integration or plugin on your website platform (like Shopify or WordPress), as you can just copy and paste this ID into a designated field.
- The Global Site Tag (gtag.js): This is the full JavaScript code snippet. It includes your Measurement ID and contains all the instructions for a browser to send tracking data back to Google's servers. You'll need this entire snippet if you plan to install Google Analytics manually by adding it directly to your website's HTML code.
Since Google fully transitioned from Universal Analytics (with its "UA-" IDs) to Google Analytics 4, all new properties will use the "G-" Measurement ID and the gtag.js format. Everything in this tutorial is based on the modern GA4 setup process.
Creating a New Google Analytics Account and Property (Step-by-Step)
If you have never used Google Analytics before, you will need to create a new account, property, and data stream. If you already have a GA4 account, you can skip to the next section about creating a new data stream. For everyone starting from scratch, here’s how to do it.
Step 1: Sign In to Google Analytics
First, head over to the Google Analytics website. You’ll need to sign in with a Google account. This can be the same one you use for Gmail, Google Drive, or any other Google service. If you don't have one, you'll need to create one first.
Once you're signed in, you’ll be greeted with a welcome screen prompting you to "Start measuring." Click this big blue button to begin.
Step 2: Start the Account Setup
You’ll now be guided through a simple setup wizard. The first step focuses on the "Account" level, which is the highest level of organization in Google Analytics. An account can hold multiple properties (websites or apps).
Give your account a descriptive name. This is usually your business or organization's name. For example, "Sarah's Flower Shop."
Below the name, you'll see "Account Data Sharing Settings." These settings control how the data from your account is shared with Google for things like technical support and benchmarking. For most users, it's perfectly safe and even beneficial to leave all of these boxes checked. Click "Next."
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 3: Create a Property
Now it’s time to create your first "Property." A property represents your website or app. If you have several websites, you might set up multiple properties under one account.
- Property name: Enter the name of your website. This could be something like "Sarah's Flowers Website" or simply your domain name.
- Reporting time zone: Select the time zone you operate in. This is very important, as it ensures that your daily reports align with your actual business day.
- Currency: Choose the currency you use for business transactions. This is crucial for accurate e-commerce reporting later on.
Click "Next" to move on.
Step 4: Provide Your Business Details
Google asks for a bit more information about your business to help tailor your experience and provide relevant benchmarking data (allowing you to compare your performance with anonymized data from others in your industry).
- Industry category: Choose the option that best describes your business.
- Business size: Select the number of employees in your company.
Click "Next" again.
Step 5: Choose Your Business Objectives
This is an important step in the GA4 setup. Based on your selections here, Google will customize the standard reports you see in your dashboard to make them more relevant to your goals.
For example, if you choose "Generate leads," you’ll see reports related to lead forms and conversions more prominently. Don't worry about trying to pick the perfect option, you can always change and customize reports later. If you aren't sure, you can simply select "Get baseline reports" at the bottom. Once you’ve made your selections, click "Create" and accept the Google Analytics Terms of Service Agreement.
Setting Up Your First Data Stream to Get the Code
After creating your account and property, you'll be prompted to set up a "data stream." Think of a data stream as the source of traffic for your property. A single property can have multiple data streams (e.g., one for your website, one for your iOS app, and another for your Android app). Here, we’re focused on your website.
Step 1: Choose a Platform
You will see three choices: "Web," "Android app," and "iOS app." For tracking a standard website, click Web.
Step 2: Enter Your Website Details
Next, you’ll configure the details for your web data stream.
- Website URL: Enter your website’s domain name. Make sure you select either
https://orhttp://from the dropdown. For most modern websites, this will behttps://. - Stream name: Give your stream a name for your own reference. Something like "[Your Website Name] - Web Traffic" is perfectly fine.
You’ll also see a section called "Enhanced measurement." Leave this enabled! This great feature automatically tracks common user interactions without any extra setup, including page views, scrolls, outbound link clicks, site search, and video engagement.
Click "Create stream."
Step 3: Find Your Google Analytics Code
Success! You'll now be taken to the "Web stream details" page. This is the screen you’ve been looking for. It contains both your Measurement ID and your full Global Site Tag code.
Your Measurement ID (The "G-" ID)
In the top right corner of the page, you'll see your Measurement ID, starting with "G-". This is the unique key you’ll need if you are using a simple website integration or plugin. You can click the copy icon next to it to add it to your clipboard.
Your Global Site Tag (The gtag.js Script)
If you scroll down, you will find a section called "View tag instructions." Under the "Install manually" tab, you'll see your Global Site Tag (gtag.js). This is the full code snippet you need for manual installation.
<!-- 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>How to Install the Google Analytics Code on Your Website
Now that you have your code, it’s time to put it on your website so it can start collecting data. There are three common ways to do this.
Method 1: Using a Plugin or Integration (The Easiest Way)
Most modern website builders and CMS platforms have built-in integrations for Google Analytics that make installation incredibly easy. For this method, you only need your Measurement ID (G-XXXXXXXXXX).
- WordPress: You can use Google’s official Site Kit plugin or other popular plugins like MonsterInsights. Both will provide a simple field where you can paste your Measurement ID.
- Shopify: In your Shopify admin, go to Online Store > Preferences. You’ll find a Google Analytics section where you can paste your code. Note that some themes require pasting the full tag.
- Squarespace: Go to Settings > External API Keys and you will find a field to enter your Google Analytics Measurement ID.
This is the recommended method for anyone who isn’t comfortable editing their website’s code directly.
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.
Method 2: Using Google Tag Manager
Google Tag Manager (GTM) is a more advanced but highly flexible tool that acts as a container for all of your website’s tracking codes (or "tags"). By installing the GTM code on your site once, you can add, edit, and remove tracking tags like Google Analytics directly from the GTM interface without ever touching your website's code again. While setting it up takes a few more steps, it's the professional standard for managing tracking scripts.
Method 3: Adding the Code Manually (Directly to Your Website’s HTML)
If your platform doesn’t have a simple integration and you don't want to use Google Tag Manager, you can add the code manually. For this, you need the full Global Site Tag (gtag.js) snippet.
Copy the entire script. You then need to paste it into the <head> section of every page on your website. Often, this is done by editing a master template file, like header.php in a WordPress theme. A quick word of caution: when editing theme files, it's always best practice to use a child theme so your changes aren't overwritten when the theme is updated.
Verify Your Google Analytics Installation
Once you’ve installed the code, you want to make sure it's working. The easiest way is using the Real-Time report in Google Analytics.
In your GA4 property, navigate to Reports > Real-Time. Now, open your website in a different browser tab or on your phone. Within about a minute, you should see yourself appear as a visitor in the Real-Time report. If you see activity, congratulations – you’ve successfully installed Google Analytics!
Final Thoughts
The process of generating your Google Analytics code boils down to creating an account and property, setting up a web data stream, and finding your Measurement ID or gtag.js snippet. Installing it is usually a simple case of pasting the ID into a plugin or, for more advanced users, adding the script to your site's code directly.
Getting insights flowing from your website is an exciting first step. But soon, you might find yourself juggling tabs between Google Analytics, your email platform, your CRM, and your ad accounts just to get the full picture. Instead of getting buried in manual reporting, we created Graphed to do the heavy lifting for you. We make it easy to connect all your data sources - including Google Analytics - and use simple, natural language to build real-time "cockpit" dashboards for your entire business.
Related Articles
Facebook Ads for Electricians: The Complete 2026 Strategy Guide
Learn how to run high-converting Facebook ads for your electrical business in 2026. Covers campaign types, targeting strategies, and creative best practices.
Facebook Ads for Restaurants: The Complete 2026 Strategy Guide
Learn how to run profitable Facebook ads for restaurants in 2026. This comprehensive guide covers the 7 killer strategies, ad formats, targeting, and budgeting that top restaurants use to drive reservations and orders.
Facebook Ads for Dog Trainers: The Complete 2026 Strategy Guide
Learn how to use Facebook ads to generate high-quality leads for your dog training business in 2026. Complete strategy guide with targeting, lead magnets, and budget optimization.