How to Include Google Analytics Tracking Code with PHP Updated on August 15, 2023 by Christopher Maiorana 1 Minutes, 16 Seconds to Read In this article, we’ll show you how you can include your Google Analytics tracking code with PHP. Be sure to check out our full guide on signing up for Google Analytics. In order for these steps to work properly, your site should already be coded with PHP. For most content management systems (like WordPress or Joomla), this is already the standard. If you are unsure about whether or not your site is coded with PHP, be sure to check with your developer. The reason you will need PHP is because we will be using an “include” code. This means we will be including the code from one file into another. PHP is required in order to accomplish this so make sure you are web hosting with PHP. Copy your tracking code from your Google Analytics account. Create a new file in your document root called gtag.txt (you can name this file anything you want, this is only an example) Paste your Google tracking script in the gtag.txt file Insert this code into your website’s index file (typically index.php: <?php include_once "gtag.txt";?>This code “includes” the content of your “gtag” file in your site’s code. If your index file loads on every page (as it does in a CMS like WordPress), the tracking code will also load on every page. Congratulations, now you know how to include Google Analytics tracking code with PHP! Check out our Website Tutorials section for more helpful guides on how to build a successful site. Share this Article CM Christopher Maiorana Content Writer II Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog. More Articles by Christopher Related Articles How to Google Analytics to WordPress Without a Plugin Secure Matomo Analytics With The Security Info Plugin How to Migrate to Google Analytics 4 From UA How to Include Google Analytics Tracking Code with PHP Enabling Google Analytics in Koken Adding Google Analytics to Ghost Debug Google Analytics with Google Analytics Debugger How to use Google Analytics with PHP-Nuke Adding Google Analytics to a Container in Google Tag Manager Installing Google Analytics on Your Website