Manually Connect Google Analytics and WordPress with Code
If you’d prefer not to use a plugin, it’s also fairly simple to add the Google Analytics tracking code directly to your WordPress theme via your functions.php file.
If you go the manual route, it’s essential that you use a child theme so that your tracking code won’t be overwritten every time you update your WordPress theme. Before you make any changes, though, it’s always good practice to make a backup.
Once your child theme is active, you’ll need to paste your Google Analytics Tracking Code right before the closing </head> tag in your header.php file.
To find your Google Analytics tracking code, go to your account and access your tracking info as we described in the previous method. You can then copy the script from the box under Google Site Tag:
To add it to your WordPress site, go to Appearance > Theme Editor. If your child theme is inheriting template files from its parent, you won’t see header.php here. You can click on the name of your parent theme in the file list to access it:
Once you’ve located and selected header.php, paste the Google Analytics Tracking Code right before the </head> tag in the Editor:
Then, make sure to click on the Update File button. Google Analytics tracking code is now being added to WordPress.
« How to Add Metabox | Save & Get Meta box Value in WordPress
How to Create a Universal Analytics Property (when GA4 is the default)? »