Top 6 Ways You Can Add Google Analytics to WordPress Without Plugin

Google Analytics is a powerful tool for tracking and analyzing your website traffic, providing insights that can help optimize your content and marketing strategies. While using a plugin is the simplest way to add Google Analytics to your WordPress site, some prefer to do it without adding another plugin to their site. Here are six methods to add Google Analytics to WordPress without plugin, along with step-by-step instructions and considerations for each method.
1. Adding Code Directly to Your Theme’s header.php
File
One of the most straightforward methods to add Google Analytics to your WordPress site is by directly inserting the tracking code into your theme’s header.php
file.
Steps:
- Get Your Google Analytics Tracking Code:
- Sign up or log in to your Google Analytics account and set up a property for your website.
- Navigate to the Admin section, then go to Tracking Info > Tracking Code, and copy the Global Site Tag (
gtag.js
).
- Insert Code into
header.php
:
- In your WordPress dashboard, go to Appearance > Theme Editor.
- Find and open
header.php
from your theme files. - Paste the copied tracking code right before the closing
</head>
tag. - Save the changes.
Considerations:
- This method is simple but risky because updates to your theme can overwrite the
header.php
file, removing the tracking code.
Also See: 10 Best Content Locker WordPress Plugins for 2024
2. Using a Child Theme
To avoid losing your changes during theme updates, you can add the Google Analytics code to a child theme.
Steps:
- Create a Child Theme:
- Create a new folder in
wp-content/themes/
named after your child theme. - Create a
style.css
file with the following header:css /* Theme Name: Your Theme Child Template: your-theme */
Replaceyour-theme
with the name of your parent theme. - Create a
functions.php
file.
- Copy Header from Parent Theme:
- Copy the
header.php
file from the parent theme into the child theme folder. - Open the copied
header.php
and paste the Google Analytics code just before the</head>
tag.
- Activate the Child Theme:
- Go to Appearance > Themes in your WordPress dashboard and activate the child theme.
Considerations:
- This approach keeps your customizations safe during theme updates.
3. Adding Code via Theme Customizer or Custom Code Sections
Some themes offer a custom code section where you can add scripts without editing theme files directly.
Steps:
- Access the Customizer:
- Go to Appearance > Customize.
- Look for a section like Additional CSS, Custom Scripts, or Header/Footer Scripts.
- Insert the Code:
- Paste the Google Analytics tracking code into the appropriate section.
- Save and publish the changes.
Considerations:
- This method varies depending on your theme and may not be available in all themes.
Also See: How to Setup Gmail SMTP in WordPress the Right Way (To Send Emails) – Easy Guide
4. Using the functions.php
File
You can also add the tracking code by editing the functions.php
file of your theme or child theme.
Steps:
- Edit
functions.php
:
- In your WordPress dashboard, go to Appearance > Theme Editor and open
functions.php
. - Add the following code to insert the tracking script into the header:
function add_google_analytics() { ?> <!-- Global site tag (gtag.js) - 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> <?php } add_action('wp_head', 'add_google_analytics');
- Replace
YOUR_TRACKING_ID
with your actual Google Analytics tracking ID.
- Save Changes:
- Save the changes to
functions.php
.
Considerations:
- Editing
functions.php
is safer than editingheader.php
directly, especially if you use a child theme.
5. By Creating Your Own Plugin
If you prefer not to edit theme files, you can create a custom plugin to add the tracking code.
Steps:
- Create the Plugin File:
- Create a new folder named
site-specific-plugin
inwp-content/plugins/
. - Create a PHP file named
site-specific-plugin.php
and add the following code:<?php /* Plugin Name: Site Specific Plugin Description: A site-specific plugin to add custom code. */ function add_google_analytics() { ?> <!-- Global site tag (gtag.js) - 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> <?php } add_action('wp_head', 'add_google_analytics');
- Activate the Plugin:
- Zip the folder, upload it via Plugins > Add New > Upload Plugin, and activate it.
Considerations:
- This method keeps your tracking code intact regardless of theme changes.
6. Using a Third-Party Service with Script Manager
Some third-party services or CDNs offer script management that allows you to inject scripts into your WordPress site without directly editing code.
Steps:
- Choose a Service:
- Services like Cloudflare can inject scripts into your site.
- Sign up and configure your website in the service.
- Add the Script:
- Use the service’s interface to add the Google Analytics tracking code to your site’s head section.
Considerations:
- This method can be more complex to set up and may require additional configuration.
Conclusion
Adding Google Analytics to your WordPress site without a plugin is entirely possible and offers greater control and customization. Whether you choose to edit your theme files directly, use a child theme, or employ a custom plugin, the key is to ensure the tracking code is correctly placed in the header of your site. Each method has its pros and cons, so choose the one that best fits your comfort level and website management style.
By following these steps, you can efficiently track your website’s performance and make data-driven decisions to improve your content and user experience.
Discover more from Aqila Media
Subscribe to get the latest posts sent to your email.