It is possible to add the Google Analytics tracking code to the header or footer of the site’s theme without a plugin’s help.

However, we do not recommend this method. The smallest misstep can cause your entire site to crash or malfunction. Added to that, you may need to update the code if you change the theme or carry out any major updates to your site.

You may also need to debug tracking issues and constantly check site performance, making it hard to scale. Furthermore, changing the theme code can present a security risk.

With these disclaimers out of the way, there are 3 ways to add the Google Analytics tracking code to your theme.

  • Add tracking code through the Header.php file
  • Add tracking code through the Footer.php file
  • Add tracking code through the Functions.php file

Add Code Through The Header.PHP File

You will need to access your files through your cPanel or FTP client portal. We will use the Bluehost cPanel for this example.

Once you log in to your cPanel, go to File Manager under the Files tab. Head over to the public_html root folder.

In this folder, go to wp-content » themes. Locate the active theme you are using on your site and select it. Finally, find the header.php file within this file and right-click to Edit.

cpanel edit header

In the code editor, find the <head> code line located near the top. Paste the Google Analytics tracking code right before the closing </head> tag. Then, Save the edit.

cpanel add google tracking id header

You have just added the Google Analytics tracking code manually on your WordPress theme through the header.php file.

Add Tracking Code Through The Footer.PHP File

Adding the tracking code to the footer is almost identical to adding it to the header. The only difference is that you will add it to the footer.php file, not the header.php file.

To get started, use your cPanel or an FTP client portal and locate the public_html root folder like in the previous process.

Follow the same process you did while inserting the code to the header.php file, but this time, find the footer.php file.

Finally, insert your Google Analytics tracking code just before the closing </body> tag in the footer.php file.

cpanel add google tracking id footer

Add that’s it! You can now successfully add the tracking code to your theme’s header or footer through cPanel or an FTP client.

We insert the tracking code in the header or footer so that it can track every page.

Add Tracking Code Through The Functions.PHP File

For this method, we will use the WordPress dashboard. Here, go to Appearance »Theme Editor.

You will see a list of files on the right under Theme Files; locate the ‘functions.php’ file.

Scroll to the bottom of this page, and just before the closing tag, paste this code:

1 &amp;amp;lt;?php
2 add_action('wp_footer', 'add_Googleanalytics');
3 function add_Googleanalytics() { ?&amp;amp;gt;
4 // Paste your Google Analytics code here
5 &amp;amp;lt;?php }
6 ?&amp;amp;gt;

In Line 4, paste the actual tracking code from your Google Analytics dashboard.

cpanel add google tracking id functions

Next, hit the Update File button, and you should have added your Google Analytics tracking code.

Remember that adding the tracking code manually or through WPCode may take a little time before metrics start trickling in on your Google Analytics dashboard.

Congratulations! You can now use Google Analytics to monitor your WordPress website performance. Next, let us look at some commonly asked questions.