This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How to Add Google Analytics With Custom PHP Website

Updated on May 30, 2022

5 Min Read

Key Takeaways

  • Gain valuable insights into your website traffic and user behavior with Google Analytics.
  • Learn how to easily add the Google Analytics tracking code to your PHP website for comprehensive data analysis.

For a business owner, customers are the most important assets. It is a priority for a business owner to get awareness about the audience visiting their websites so that they can optimize their website accordingly to attract more visitors. There a number of analytics software that provide these services for free; however, the best to stand out is Google Analytics (GA).

integrating google

You might also like: How To Host PHP On DigitalOcean

Google analytics enables you to:

  • Get information about the visitor’s geographical location, web browser’s information, resolution of their screen and the languages they speak.
  • Track their activity on your website, average page views and most viewed pages, etc.
  • Track the visiting time of your audience. With this information, you can schedule your posts accordingly.
  • Track the referral path as well. For example are the visitors coming from social media or organic?
  • Track the keywords through which visitors are coming towards your website.
  • Track the clicks on specific URLs and much more.

When it comes to hosting a website that uses PHP, finding the best hosting for PHP is crucial. However, once you have a reliable PHP hosting solution in place, you can begin to take advantage of tools like Google Analytics to help grow your online presence. Google Analytics is currently the most powerful analytics tool available, and businesses that want to expand online can benefit greatly from it. With full support and documentation available for free, businesses can easily utilize this tool to add different dimensions and metrics to their reports. Additionally, for more advanced users, Google Analytics API is also easily accessible. By connecting your webmaster tools to Google Analytics, you can easily exploit your data to your advantage and make informed decisions for your website’s growth.

Method of Adding Google Analytics Code to Your Website

To add Google Analytics to your website, you first need to have a Google Account. If you still don’t have one, then all you need to do is head over to Google Analytics and signup for a new Google Account. After making an account, sign in and add Google Analytics to your account. Enter the name of the account, Site URL, and other details.

After you are done setting it up, you now need to collect Google analytics code to begin tracking of your domain. Asynchronous Google analytics code selection is recommended as it reduces the chance of a lag in your server due to a third party intervention. The asynchronous Google analytics code should be added to the <head> of your site. You can add additional scripts to the <head> as well. You should embed Google analytics code on every page you want to track.

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Alternative Method

The above method is the easiest method for deployment of Google Analytics. Alternatively, Google Analytics tracking code can also be added via a custom function as well. In this way, all your modifications will be available on a single file custom_functions.php. There is no significant advantage of using this method; however, if you want to integrate analytics through function rather than thesis interface then this method will suit you. You should include this file on every page you want to track.

function add_my_google_analytics() {

?>

<script type="text/javascript">

 var _gaq = _gaq || [];

 _gaq.push(['_setAccount', 'UA-XXXXXXXX']);

 _gaq.push(['_trackPageview']);

 (function() {

var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

ga.src = ('https:' == document.location.protocol ? 'https://ssl' :

'http://www') + '.google-analytics.com/ga.js';

var s = document.getElementsByTagName('script')[0];

s.parentNode.insertBefore(ga, s);

 })();

</script>

<?php

}

add_action('wp_head', 'add_my_google_analytics',99);

In the above code, replace UA-XXXXXXX with the unique tracking code. Google Analytics code is pasted within the script tag of the function, and only the UA-XXXXXXXX will change.

Your Google Analytics is now deployed for your website. You can now visit your Google Analytics dashboard; however, you will not see the metrics for a while as the tracking takes a bit of time.

The Administrative Dashboard gives administrators direct access to critical Encompass tools for speedier and more convenient community administration. The Dashboard serves as an admin home page to get to the key components of your community.

Integrate Google Analytics With Custom PHP Website

Till then, you should acquaint yourself with the Google Analytics platform and get used to the tracking metrics. You are about to board a data train that is going to help you take your business to the next big leap. Happy tracking!

Supercharged Managed PHP Hosting – Improve Your PHP App Speed by 300%

Q. How can I log in as an administrator in PHP?

    Following are the steps to create an admin login page using PHP:

  1. Create Database
  2. Create Table
  3. Create Table Structure
  4. Add admin login information
  5. Create a folder that includes, where your XAMPP is installed, For the WAMP server, it should be in “C:\wamp64\www\” and on Linux “/opt/lampp/htdocs”.
  6. Run XAMPP server
  7. Start MySQL and Apache services from XAMPP Panel.
  8. Goto http://localhost/loginPage/ in your browser.

Q. How to add Google Analytics to a PHP website?

Here’s how you can add Google Analytics to your PHP website:
1. Go to Google Analytics, create an account, and get your GA4 Measurement ID.
2. In your Google Analytics dashboard, find the Global Site Tag (gtag.js) script.
3. Open your header.php file (or any common template file that loads on all pages) and paste the code before the closing tag.
4. If editing locally, re-upload the file to your server.
5. Visit your website and check the Google Analytics real-time reports to ensure tracking is working.

If you’re using Cloudways hosting, enable caching properly to avoid delays in tracking updates.

Q. How to add Google Tag in a PHP website?

Follow the steps below to add Google Tag in a PHP site:

1.  Go to Google Tag Manager and set up a new container for your website.
2. After setup, Google provides two code snippets: one for thesection and one for the.
3. Insert the snippets in your PHP files:
4. Add the first snippet inside header.php before.
5. Add the second snippet inside body.php right after.
6. Save and upload changes.
7. Open Google Tag Manager’s Preview mode and visit your site to check if tags fire correctly.

Q. How do I add Google Analytics events to my website?

To track events like clicks and form submissions, use Google Analytics event tracking. Add an onclick attribute to elements you want to track, like this:
Sign Up
For dynamic tracking, integrate Google Tag Manager with triggers and variables. If your site is hosted on Cloudways, its optimized infrastructure ensures smooth tracking without slowing down your site.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Inshal Ali

Inshal is a Content Marketer at Cloudways. With background in computer science, skill of content and a whole lot of creativity, he helps business reach the sky and go beyond through content that speaks the language of their customers. Apart from work, you will see him mostly in some online games or on a football field.

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour