Adding Google Analytics to your WordPress block theme might seem challenging, especially if you’re looking for the Theme Editor option in the WordPress dashboard. Here’s the truth: you cannot do this via Appearance > Theme Editor. Unlike traditional themes, block themes often don’t display a functions.php
file or a header.php
file there.
The only way to add Google Analytics code is by directly accessing your website files through FTP. Let’s walk you through the process step by step.
Why You Can’t Use the Theme Editor
Block themes are built differently. They follow a modern structure that prioritizes flexibility and performance, but this means traditional files like header.php
may not even exist or be accessible through the WordPress dashboard. As a result, the functions.php file (where we need to add the Google Analytics code) must be accessed directly through your website's file system.
How to Add Google Analytics Code Using FTP
Here’s how to insert the Google Analytics tracking code into your block theme’s functions.php
file using an FTP client like FileZilla or WinSCP.
1. Prepare Your Google Analytics Code
- Log in to your Google Analytics account.
- Go to Admin > Data Streams > Web and select your site’s data stream.
- Copy the provided Google Analytics tracking code (
gtag.js
) with your unique tracking ID (e.g.,G-XXXXXXX
).
2. Connect to Your Server Using FTP
- Download and install an FTP client like FileZilla or WinSCP.
- Use your hosting credentials (FTP username, password, and host) to connect to your server.
- Navigate to the
wp-content/themes/
directory.
3. Locate or Create the functions.php
File
- Open the folder of your active theme (e.g.,
/wp-content/themes/your-theme-name/
). - Look for a file named
functions.php
:- If the file exists, download it to your computer as a backup.
- If the file does not exist, create one:
- Open a text editor (e.g., Notepad or VS Code).
- Save a blank file named
functions.php
.
- Add the following code to the
functions.php
file:
<?php
// Add Google Analytics tracking code to the head section
function insert_google_analytics() {
?>
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'YOUR_TRACKING_ID');
</script>
<!-- End Google Analytics -->
<?php
}
add_action('wp_head', 'insert_google_analytics');
- Replace
YOUR_TRACKING_ID
with your actual Google Analytics tracking ID.
4. Upload the File
- If you downloaded an existing
functions.php
file, re-upload it to the same directory after making changes. - If you created the file, upload it to your theme’s folder.
For Child Theme Users
If you’re using a child theme, the process is similar, but you need to create a functions.php
file in the child theme directory:
- Go to
/wp-content/themes/your-child-theme/
. - If a
functions.php
file doesn’t exist, create one. - Paste the same code (as shown above) into this file and upload it.
This ensures your Google Analytics code is added safely without affecting the parent theme.
Why This is the Only Way
You might wonder why you can’t just use the Theme Editor or other methods:
- Theme Editor doesn’t display the
functions.php
orheader.php
file in block themes, making it unusable for this task. - Plugins can create unnecessary load on your site or cause conflicts with block themes.
- Editing files directly via the dashboard (if available) is risky and could cause errors that lock you out of your site.
Using FTP ensures full control and avoids problems during theme updates.
Conclusion
If you’re trying to add Google Analytics to your WordPress block theme, there’s only one reliable and future-proof way: editing the functions.php
file via FTP. Whether you’re working with your main theme or a child theme, take the time to access your site files using FileZilla or WinSCP, and make the necessary edits.
This method ensures your customizations stay intact, your site functions smoothly, and your tracking is up and running without any hiccups. Follow these steps, and you’ll have Google Analytics set up in no time! 🚀
I thought this post was a God send because I have been trying to figure this out for a long time however I could not get it to work. I have been adding GA code manually to hundreds of websites over the years but now I have switched to the new block theme and am having a difficult time figuring out how to add Analytics to Twenty-Twentyfive.
I tried adding it to my child theme and when that did not work I added id directly to the functions file of the parent theme and it still did not work. When I added it to the parent theme it caused the site to crash. Any ideas?
Thanks for your comment, Alex. Did you add your GA tracking ID to the code?