Where to Put Google Analytics Code in Divi?
If you've built your website with the powerful Divi theme, one of your first steps after going live is connecting it to Google Analytics. This is how you'll understand your audience, see where your traffic comes from, and track which content is performing best. This article will show you exactly where and how to add your Google Analytics tracking code to your Divi site. We'll cover three simple methods for every comfort level, from beginner to advanced.
First, Get Your Google Analytics Tracking Code
Before you can add analytics to Divi, you need to grab your tracking code from your Google Analytics 4 property. This code acts as the bridge between your website and Google's servers, allowing data to flow from user visits into your reports. You'll need the "Global site tag" (gtag.js) for most implementation methods.
Here’s how to find it:
- Log in to your Google Analytics account.
- If you have multiple properties, make sure you've selected the correct one from the dropdown in the top-left corner.
- In the bottom left, click the gear icon for 'Admin'.
- In the 'Property' column, click on 'Data Streams'.
- Select your website's data stream from the list. It will usually be named after your site URL.
- Under the 'Google tag' section, click on 'View tag instructions'.
- A new screen will load. Click the 'Install manually' tab.
- You will see a JavaScript code snippet. This is your Global Site Tag. Click the copy icon to copy the entire block of code to your clipboard.
The code will look something like this, with your unique Measurement ID filled in:
<-- 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>
Now that you have your code, let's explore the best ways to add it to Divi.
Method 1: The Easiest Way Using Divi's Built-In Integration
For most users, the simplest and most direct method is using the integration options built right into the Divi theme. This approach doesn't require extra plugins, meaning one less thing to update and maintain. It's clean, effective, and takes less than a minute.
Follow these steps:
- From your WordPress dashboard, navigate to Divi → Theme Options.
- In the Divi Theme Options panel, click on the 'Integration' tab at the top.
- You'll see several boxes for adding code snippets to different parts of your site. Locate the box labeled "Add code to the
<head>of your blog". - Paste the entire Google Analytics tracking code you copied earlier into this box.
- Scroll down and click the green 'Save Changes' button.
That's it! Because this option is part of the core Divi theme, the code will remain safely in place even when you update Divi. This is the recommended method for its simplicity and reliability.
Method 2: Using a Plugin like Site Kit by Google
While the Divi integration method is great, sometimes you might prefer a plugin-based solution. A dedicated plugin can offer additional features, such as showing key website stats directly within your WordPress dashboard, saving you a trip to the Google Analytics website. A plugin is also a good option if your organization's policy requires one for managing scripts.
The best plugin for this job is Site Kit by Google, as it’s the official plugin from Google for WordPress. It connects your site to multiple Google services - including Analytics - through a simple setup wizard.
Here's how to set it up:
- In your WordPress dashboard, go to Plugins → Add New.
- In the search bar, type "Site Kit by Google" and press Enter.
- Find the plugin in the search results and click 'Install Now' and then 'Activate'.
- Once activated, you’ll see a prompt to begin the setup. Click the 'Start Setup' button.
- You'll be guided through a few steps to connect your website with your Google account. You’ll need to sign into the Google account associated with your Analytics property.
- Site Kit will automatically verify that you own the website.
- Next, it will prompt you to connect services. Click to connect Search Console first, and then it will ask about connecting Google Analytics.
- Select your Google Analytics account, property, and data stream from the dropdowns when prompted.
- Finalize the setup, and Site Kit will automatically add the correct tracking code to your website for you.
The main advantage here is convenience. Site Kit not only adds the code but also provides a sleek dashboard inside WordPress where you can see top-performing pages, acquisition channels, and other high-level metrics without leaving your site.
Method 3: The Advanced Option (For Developers) using a Child Theme
If you're a developer or a more advanced user comfortable with editing code, you might want to add the tracking script manually via a child theme. This method keeps your customizations separate from the parent Divi theme, ensuring they are not overwritten during an update, and it gives you full control without relying on the theme's options panel.
Warning: This method involves editing theme files. It's not recommended for beginners unless you're confident with code and are already using a child theme.
There are two ways to do this within a child theme: editing the header.php or, the better option, using functions.php.
Adding to functions.php (Recommended)
Using the functions.php file and a WordPress hook is the cleanest, most professional way to add third-party scripts. This method programmatically injects your code in the <head> section without needing to modify the template files directly.
- Ensure you have a Divi child theme created and activated on your site.
- From your WordPress dashboard, go to Appearance → Theme File Editor.
- On the right, make sure your Divi Child Theme is selected, and click to open its Theme Functions (functions.php) file.
- Add the following PHP code snippet at the bottom of the file. Be sure to replace
G-XXXXXXXXXXwith your actual Measurement ID.
add_action('wp_head', 'add_google_analytics_code'),
function add_google_analytics_code() { ?>
<!-- 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>
<?php }- Click 'Update File' to save your changes.
This code hooks into the standard WordPress wp_head action to print your Google Analytics tag just before the closing </head> element on all of your pages.
Don't Skip This: How to Verify Your Setup
After you’ve added the tracking code, it’s crucial to make sure it's working correctly. You don't want to realize weeks from now that no data has ever been recorded. Here are two easy ways to check:
1. Use the Realtime Report
This is the quickest and most direct way to verify functionality.
- Log into your Google Analytics account.
- Navigate to Reports → Realtime.
- In a new browser tab, or on your phone, open your website.
- Within a minute or so, you should see yourself appearing as a visitor in the realtime report. You'll see the number of "Users" in the last 30 minutes change to '1' or higher, indicating activity.
If you see activity, congratulations! It's working!
2. View Page Source
This is a slightly more technical method but equally effective.
- Go to your website in Chrome or Firefox.
- Right-click on the page and select ‘View Page Source’.
- A new tab will open, showing all the HTML code of your page.
- Press Ctrl + F (or Cmd + F on a Mac) to open a search box and enter your Measurement ID (e.g., G-XXXXXXXXXX).
- If the search highlights your tracking code, it's successfully present on the page, meaning it was added correctly.
Final Thoughts
Successfully adding Google Analytics to your Divi site is an important step in understanding any website's performance. Whether you choose the built-in Divi method, the official Site Kit plugin, or the manual advanced method, you'll have the necessary tools to start tracking data and making data-driven decisions for your business.
Now that your data is flowing into Google Analytics, don't let the insights go to waste. Engage with what the data tells you. Google Analytics can be complex, but its insights are powerful. Instead of struggling with reports, focus on asking questions like, “Which channels are driving the most sales?” or “How did my recent ad campaigns perform?” Leverage these insights and customize dashboards to suit your decisions. Empower your analytics and put yourself back in control.
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.