Google Analytics Glossary

Add Google Analytics to PHP

Image of Iron Brands

Published on Nov 23, 2023 and edited on May 7, 2024 by Iron Brands

So your PHP website is up and running- but how can you improve its performance? Well, Google Analytics can give you valuable insights on the performance and traffic of your website. Here is how to add GA to your PHP website, step by step.

Let's dive in!

  1. Integrating Google Analytics
    1. Set up Google Analytics
    2. Add the GA4 Tracking Code to Your Website
    3. Verify the Integration
    4. (Optional) Implement Event Tracking
    5. Create New Properties (Optional)
  2. Final Thoughts

Before we dig in you need to know that Google Analytics is complex and a bit clunky. There are other options that give you the same insights in a slick and straightforward dashboard.

Simple Analytics is one of them. A privacy-friendly and simple analytics tool - just the insights you need in a straightforward dashboard. (And its also free yes).

Can you spot the difference between the dashboards?

All right, now let's get into answering your question!

Integrating Google Analytics

Set up Google Analytics

  • Log into your GA profile (or create one, if you haven't already).
  • Create a GA property for your website.
  • Inspect the property and note down your Measurement ID. You will need it later.

Add the GA4 Tracking Code to Your Website

The tracking code should be added to every page you want to track. In a PHP website, this is often done by inserting the code into a header file that's included in every page.

Place the GA tracking script in the <head> tag of your header file (e.g., header.php):

<head>
  <!-- Global site tag (gtag.js) - Google Analytics -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'G-XXXXXXXXXX', { 'send_page_view': true });
  </script>
  ...
</head>

Make sure teplace G-XXXXXXXXXX with your actual Measurement ID!

Verify the Integration

After implementing the tracking code, visit your website and then check the real-time reports in Google Analytics to ensure that your visits are being recorded.

(Optional) Implement Event Tracking

Google Analytics 4 is highly event-centric. You may want to track specific actions on your website, like button clicks or form submissions. This requires adding additional JavaScript to trigger events. For instance:

document.getElementById("your-button-id").addEventListener("click", function() {
  gtag('event', 'click', {
    'event_category': 'Button',
    'event_label': 'Your Button Label'
  });
});

This snippet sends an event to GA whenever the specified button is clicked.

Create New Properties (Optional)

To track multiple websites, create more GA properties and use the corresponding Measurement IDs. This prevents GA from conflating the data.

Final Thoughts

Adding Google Analytics to your PHP app can give you great insights. However, ask yourself: is Google Analytics the right tool for you?

GA is an overpowered solution for straightforward analytics. If you're looking for a simple and intuitive dashboard with the insights you need, there are better alternatives.

If this resonates with you, feel free to give Simple Analytics a spin. You just need to add the script to your app and off you go. This takes about one minute- and there is a free version as well!

Enjoy!