Google Analytics is the top most and free web analytics tool offered by the search engine giant Google. You can implement the script in the header section of the pages to track the hit count and other user behaviors. Google also offers 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 the website tracking code of Google Analytics can be used directly on most of the content management systems like WordPress. But there are PHP websites which may not have an option to insert the UA code. In such a case instead of inserting the complete script 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 Global Site Tag and can’t use PHP implementation.
How to Add Google Analytics Code Using PHP Implementation?
Follow the below steps:
- Creating Analytics tracking code
- Create PHP file
- Upload PHP file to your server
- Add PHP tag in template pages
- Verify PHP implementation
1. Getting Tracking Code
Login to you Google Analytics account and navigate to “Admin” section. Analytics has three component for each site – Account –> Property –> View. Select the “Account” and then “Property” you wanted to implement PHP tag. Click on the “Tracking Code” option under “Tracking Info” section.
You will see the tracking ID and the website tracking script, Global Site Tag (gtag.js). Normally, you can use this script as it is or insert the tracking ID on your site for adding Google Analytics. Learn more on how to create tracking code for your site.
2. Creating a PHP File
Follow the below steps for PHP implementation method:
- Copy the “Global Site Tag” script under “Website tracking” section text box.
- Open Notepad in your PC or TextEdit in your Mac 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 cPanel. 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 web browser. 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 “Tracking Code” section, click on the “Send test traffic button”. The status should show 1 active visitor as shown below:
Summary
Earlier, Google was showing PHP implementation method as an option in Google Analytics account. However, it has been removed now and you will only see the tracking code method. We use PHP implementation method on some of our site and it works. This will also help you to insert a single line of PHP code instead of pasting the entire tracking code on your pages.
14 Comments
Leave your reply.