Google Analytics is the popular and free web analytics tool offered by the search engine giant Google. You can implement the script in header section of pages to track the hit count and other user metrics. Google also offers a hidden PHP implementation method to add Google Analytics code on any web page. In this article, we will explain how to add Google Analytics code using PHP implementation method.
When to Use PHP Implementation Method?
Generally, Google Analytics tracking code can be used directly with most of the content management systems like WordPress. When creating the code, Google also will automatically scan your site and recommend the implementation based on your site builder or content management system. But there are PHP websites which may not have an option to insert the GA4 code. In such a case, instead of inserting the complete script in the header section it is a good idea to use single include line using PHP implementation method.
Remember, you should use PHP implementation only on the sites using PHP pages. Sites having plain HTML or using frontend framework like Bootstrap should have the normal GA4 script and can’t use PHP implementation.
How to Add Google Analytics Code Using PHP Implementation?
Follow the below steps:
- Getting GA4 tracking ID
- Create a PHP file
- Upload PHP file to your server
- Add PHP tag in template pages
- Verify PHP implementation
We do not cover creating a new Analytics account and setting up your site. If you need help, refer our article on how to setup a new GA4 account.
1. Getting Tracking Code
Login to you Google Analytics account and navigate to “Admin” section. Select the “Account” and then “Property” you wanted to implement PHP tag. Go to “Data Streams” section and click on your property name showing under “Web” tab.

Scroll to the bottom on the next page and click on the “View tag instructions” option.

- Go to “Install manually” section and copy your tracking code.

2. Creating a PHP File
Follow the below steps for PHP implementation method:
- Open Notepad in your PC or TextEdit in your Mac or any other text editor app and create a new plain text file.
- Paste the script code in the text file and save the file with the name as “analyticstracking.php”.
- Remember to save the file with .php extension.

3. Uploading PHP File in Server
Login to your server using FTP or File Manager option in hosting panel. Upload the “analyticstracking.php” file into the root directory of your site.

4. Adding PHP Tag in Site Pages
Now open your webpage’s PHP template and insert the following code immediately after the < body > tag.
<?php include_once("analyticstracking.php") ?>
It should look like below on the template file:

Generally, the code is to be inserted only on the “header.php” template file. As this file will be called on all pages of your site, the tracking code will be automatically inserted in header section of all the pages. If you don’t have “header.php” template file, then you should add this code on all page templates of your site.
Save your changes and upload the files back to the server.
5. Verifying Analytics PHP Implementation
First, open one of the pages on your site in a new browser window. Right-click on the page and view the page source. You should be seeing the Google Analytics tracking code on the source code.

You can also verify the implementation from Google Analytics account. Under “Data Streams” section, you should see “Receiving traffic in past 48 hours” against your property indicating the installed code is sending data to your account.

Summary
Earlier, Google was showing PHP implementation method as an option in Google Analytics account. However, that was removed now, and you will only see the tracking code method for website builders / CMS or for manual installation. We use PHP implementation method on some of our sites and it works perfectly. As explained above, you can simply insert a single line of PHP code in the header template file instead of pasting the entire tracking code.






Thank you, we followed exactly what you mentioned above and now it’s fully working!
Good to know it works for you. Many people confuse this with using on plain HTML page where the normal code should be used.
The above worked perfectly for me. Many thanks.
This was my first experience with a PHP website and I didn’t realise I had to insert more than just the UA code. My experience prior to this was only with Wordpress sites so the above was a bit of a new learning curve. But I just followed the above steps and it worked out smoothly.
Again, many thanks for the above post. Much appreciated,
Kind Regards,
Gary
Thanks for the read, good to know it worked for you. There are many PHP sites, other than WordPress, uploading the analytics file is the only way if there are no other built-in options.
Thanks for telling me this method I eagerly want to know the method How to Add Google Analytics through PHP file and I got the solution here. It is the alone article in Google Search to add the proper way.
Thanks for the read, earlier Google show this method in Search Console. Now they don’t show this as an option though it is easy to implement on PHP sites.
Thanks it’s working but i want to know how i can create different kind of custom report
You can get reporting from Analytics dashboard, this is the same for any type of implementation.
We already created a php file as you mentioned in the article. But the code is not working . May i know the updated ones.
We are using in PHP sites and it works properly. You should check the correctness of the code from your Analytics account and use the PHP tag right after body tag.
We checked thoroughly, but the 5th point which you mentioned in your article is not implemented in my site. That is only my problem.
Read the section – when to use PHP implementation method.
Looks your site is pure HTML site and not parsing PHP. In this case just insert the complete Analytics tracking code in header section of all your pages, then it should work.
Hii… actually we updated google analytics code in our website yesterday. we copied code in all my web pages. But i didn’t get any modified code like that above you said. May i know the reason behind of this. My domain name is http://www.novelpatent.com
It’s not a modified code, it’s the same Global Site Tag tracking code. You should create a PHP file and use the tag as explained in the article. Screenshots in the articles are updated with the latest Analytics interface.