How to Add Google Analytics Code in Website
Adding the Google Analytics code to your website is the first essential step toward understanding who your visitors are and how they find you. Without it, you're flying blind. This guide will walk you through exactly how to find your tracking code and install it on your site using the most common methods, from a simple cut-and-paste to the gold standard used by professional marketers.
What Exactly is the Google Analytics Code?
Before you copy and paste anything, let's quickly clarify what you're working with. You'll encounter two main components: the Google tag (also known as gtag.js) and the Measurement ID.
- The Google Tag (
gtag.js): This is the full JavaScript snippet that actually does the work. When a user visits your site, this script runs in their browser and sends key information - like which page they're on, their device type, and where they came from - to your Google Analytics property. It's designed to be placed in the<head>section of your website’s HTML. - The Measurement ID: This is a unique identifier for your specific data stream (your website). It looks like
G-XXXXXXXXXX. Many plugins and website builders have simplified the integration process so you only have to paste this ID, and they'll handle inserting the full JavaScript snippet for you.
In short, the Google tag is the full engine, and the Measurement ID is the license plate that tells the engine where to send the data.
Finding Your Google Analytics Code
First, you need to grab the code snippet or your Measurement ID from your Google Analytics account. If you haven’t created an account yet, you’ll need to do that first by heading to the Google Analytics website and signing up. Assuming you have a Google Analytics 4 property set up, here's where to find what you need.
- Navigate to your Google Analytics account.
- Click on the Admin icon (the gear) in the bottom-left corner.
- In the Property column, make sure you have the correct property selected.
- Click on Data Streams and select the data stream for your website.
- You'll see your Measurement ID (e.g., G-TCZMJG1X3F) right at the top. If a platform asks only for this ID, this is what you need.
- For the full code snippet, look for the "View tag instructions" button at the bottom of the page.
- Click on the Install manually tab. Here you will find the full
gtag.jscode snippet ready for you to copy.
How to Add Google Analytics to Your Website: 3 Methods
There are several ways to get this code onto your website. The best method depends on your technical comfort level and the platform your website is built on.
Method 1: Manual Installation (The Developer's Way)
Manually adding the gtag.js script to your site’s HTML files is the most direct method. It’s perfect if you have a simple, custom-built website or are comfortable navigating your site’s code.
When to use this method:
- Your site is static HTML.
- You use a WordPress theme framework where you modify a child theme.
- You feel confident editing code and know how to find your website's header file.
Steps:
- Copy the entire Google tag snippet from your Data Stream settings as described above.
- Access your website's files. This might be through an FTP client or directly through your hosting provider's file manager. If you’re using a CMS like WordPress, you might access this via Appearance > Theme File Editor.
- Locate the file that controls the HTML head section for your entire site. On a WordPress site, this file is typically named header.php. For static HTML sites, you may have to edit the file for every single page.
- Paste the copied Google tag snippet immediately before the closing
</head>tag. - Save your changes and upload the file back to your server if needed.
Important: Be very careful when editing core theme files. An error can break your site. Furthermore, if you directly edit a primary theme file in WordPress, your changes could be overwritten the next time the theme updates.
Method 2: Using a CMS Integration or Plugin (The Easy Way)
Most modern website builders and content management systems (CMS) have a built-in feature or widely used plugin to help you install Google Analytics without touching a single line of code. They typically just ask for your Measurement ID.
For WordPress: The vast majority of WordPress sites use a theme or a plugin for this. It's safer and easier than manual installation.
- Your Theme Settings: Many themes have a designated field for tracking scripts. Look in your theme options (often under Appearance > Customize or a separate theme panel) for a "Scripts," "Header/Footer Code," or "Integrations" section. You can paste your
gtag.jssnippet there. - Using a Plugin: If your theme doesn't have this feature, a plugin is the best choice. A popular option is WPCode – Insert Headers and Footers. Simply install and activate the plugin, go to its settings page, and paste the entire Google tag snippet into the Header box. This prevents your code from being erased during theme updates.
For Shopify: Shopify makes syncing your store analytics straightforward using their Google & YouTube app, which is the platform's officially recommended method.
- From your Shopify admin, go to Apps. Search for and install the "Google & YouTube" app.
- Once installed, open the app and click Connect Google account. Follow the prompts to sign in to the Google account associated with your Analytics property.
- From the list of GA4 properties that appear, select the one with the correct Measurement ID for your store.
- Click Connect and you're done! The app now handles placing the correct tracking code and can also sync your product catalog for advanced e-commerce reporting.
For Wix or Squarespace: Platforms like Wix and Squarespace have purpose-built integrations designed for simplicity.
- Look for a settings area named something like "Marketing Integrations," "Code Injection," or "External APIs."
- Find the Google Analytics integration option. These platforms almost always have a dedicated field for it.
- Copy your Measurement ID (just the G-XXXXXXXXXX part) from your GA4 Data Stream settings.
- Paste it into the designated field on your builder's dashboard and hit "Save." The platform will now inject the proper tracking code site-wide.
Method 3: Via Google Tag Manager (The Marketer’s Gold Standard)
This is the most powerful and scalable method, preferred by digital marketers and businesses of all sizes. Instead of adding every marketing tracking script (Google Analytics, Meta Pixel, Hotjar, etc.) to your site individually, you add just one snippet: the Google Tag Manager (GTM) container.
You then manage all your different marketing "tags" from within the GTM interface, without ever needing to touch your site's code again.
Steps to set up a GA4 tag in GTM:
- Sign up for a free Google Tag Manager account and create a container for your website.
- GTM will give you two code snippets. Install them on your website as instructed (one high in the
<head>, the other right after the opening<body>tag). This is a one-time setup. - Inside your GTM container, go to Tags > New.
- Name your tag something clear, like "GA4 - Pageview Tracking."
- Under Tag Configuration, choose the Google Analytics: GA4 Configuration tag type.
- Paste your Measurement ID (G-XXXXXXXXXX) into the corresponding field.
- Under Triggering, select the Initialization - All Pages trigger. This tells GTM to fire the GA4 tag on every page of your site.
- Click Save.
- Finally, click the Submit button in the top-right corner, give your version a name, and hit Publish.
How to Check if Google Analytics is Working
Once you've installed the code, you need to verify it's firing correctly. Don't just assume it works!
Check the Realtime Report
The quickest way to see if your tracking is active is by using Google Analytics's built-in Realtime report.
- In Google Analytics, go to Reports > Realtime.
- In a different browser tab (or on your phone), visit your website’s homepage.
Within a minute or two, you should see yourself appear as a visitor in the Realtime report. You’ll see your location on the map and a count of "1" in the "Users in Last 30 Minutes" card. If you see this, your setup is a success!
Use the Google Tag Assistant
For a more advanced check, you can use Google's debugging tool. Visit tagassistant.google.com, enter your website URL, and it will open your site in a new tab with a debug panel. You can then navigate your site and see exactly what tags (including your Google Analytics tag) are firing on each page.
Final Thoughts
Getting Google Analytics installed correctly is your gateway to data-driven decision-making. Whether you choose to add the code manually, use a simple CMS integration, or adopt the professional standard with Google Tag Manager, the key is to get that first stream of data flowing so you can learn and grow.
Once your website starts sending data to Google Analytics, the next challenge is turning all those metrics into meaningful insights that you can act on. This is where we built Graphed. We let you connect your Google Analytics account in seconds and create dashboards with simple, natural language. Instead of digging through complex reports, you can just ask questions like "Show me a chart of my top traffic sources last month" and instantly get the answers you need to understand performance.
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!
What SEO Tools Work with Google Analytics?
Discover which SEO tools integrate seamlessly with Google Analytics to provide a comprehensive view of your site's performance. Optimize your SEO strategy now!
Looker Studio vs Metabase: Which BI Tool Actually Fits Your Team?
Looker Studio and Metabase both help you turn raw data into dashboards, but they take completely different approaches. This guide breaks down where each tool fits, what they are good at, and which one matches your actual workflow.