When you’re setting up something like Google Analytics 4 (GA4) in WordPress, it might feel tempting to just drop your tracking code into your parent theme. But here’s the problem: every time your parent theme updates, your changes are overwritten. That means your GA4 code will be removed, and you’ll have to add it again after each update.

The safer and smarter solution is to use a child theme.

What’s the Difference Between a Parent Theme and a Child Theme

  • Parent Theme: This is the main theme you install in WordPress (e.g. Astra, Elementor, Divi). It controls the design, features, and overall layout of your site.
  • Child Theme: Think of this as a “mini-theme” that sits on top of your parent theme. It inherits all the styling and functionality of the parent theme but lets you add your own tweaks (like your GA4 code) without risking them being lost during updates.

Peace of Mind: Using a child theme won’t break your site’s look or layout. It simply acts as a safe layer where your customisations live.

How to Quickly Create a Child Theme

Don’t worry—it’s easier than it sounds.

  1. Create a New Folder
  • Go to wp-content/themes in your WordPress installation.
  • Create a folder called my-child-theme (you can name it anything).

2. Add a style.css File
Inside your new folder, create a file called style.css and add this code:

style.css

/* 
Theme Name: My Child Theme 
Description: A child theme for my WordPress site 
Author: Your Name 
Template: your-parent-theme-folder-name 
*/

Replace your-parent-theme-folder-name with the folder name of your parent theme (for example, astra).

3. Add a functions.php File
Create a new file called functions.php in the same folder. This is where you’ll place your GA4 code or other snippets that need to be in the <head> of your page. Example:

functions.php

<?php

function my_child_theme_google_analytics() { ?>
  <!-- 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 }
add_action( 'wp_head', 'my_child_theme_google_analytics', 10 );

?>

Swap G-XXXXXXXXXX with your own GA4 Measurement ID.

4. Activate Your Child Theme

  • In your WordPress dashboard, go to Appearance > Themes.
  • You’ll see your new child theme listed.
  • Activate it. Your site will still look exactly the same—only now, your GA4 code (or any other script you added) is safely tucked away in your child theme.

Why This Matters

By placing your code in a child theme:

  • Your code won’t vanish when your theme updates.
  • Your site stays secure and stable.
  • You gain the flexibility to add other customisations later without risk.

It’s a simple extra step that protects your hard work and ensures your analytics tracking is always live.

👉 Next Step: Head over to my full guide on How to Add Google Analytics 4 to WordPress (No Plugin Needed) for the complete walkthrough.