Laravel Analytics v5 with support for GA4 has been released
We've released a new major version of our Laravel Analytics package. The most important new feature of this release is the support for GA4.
Exploring the package
Laravel Analytics aims to be the most straightforward package to fetch analytics data. We built it to display key analytics data in the admin sections of the sites we build.
Getting access to a Google API can be a daunting task, and that's why we provide very clear instructions in our readme on how to authenticate with Google and how to get a JSON file containing the required credentials.
With the credentials file in your app and the package installed (it's a simple composer require
away), you're ready to fetch some data from Analytics.
Let's take a look at two simple examples. In this first snippet, we will get the total number of visitors and page count for the past seven days.
use Spatie\Analytics\Facades\Analytics;
use Spatie\Analytics\Period;
Analytics::fetchTotalVisitorsAndPageViews(Period::days(7));
Behind the scenes, Google Analytics responds with a very verbose, and slightly confusing response. The package transforms that response to a handy Collection
in which each item is an array that holds keys activeUsers
, screenPageViews
, and pageTitle
.
Want to get the top browsers? Here's how to do that
use Spatie\Analytics\Facades\Analytics;
use Spatie\Analytics\Period;
Analytics::fetchTopBrowsers(Period::days(7));
The function returns a Collection
in which each item is an array that holds keys screenPageViews
and browser
.
The package contains many more functions to work with Analytics data. We've also included a method called get
that can be used to build up any Google Analytics query you desire.
What is GA4?
In the intro, I mentioned that our package now supports GA4, which non-surprisingly stands for Google Analytics 4. It is the newest version of Analytics. Behind the scenes, the whole back end has changed, and you must also update the property displayed on every page. You'll find more info in this blog post.
Everyone using the previous version of Google Analytics is required to upgrade to GA4 by July 1, 2023. On that date, all old versions of Analytics will not collect any data anymore.
The API to interact with Google Analytics has changed as well. The newly released version of Laravel Analytics supports that new API.
In closing
With 3 million downloads, Laravel Analytics is one of the most popular packages to work with Analytics data. We've made almost no changes to our API, so users that are already using the package should have an easy time upgrading to the latest version.
A big thank you to my colleague Tim, who code up this new major version of our package.
If you're looking for a privacy friendlier alternative to Google Analytics, do take a look at Fathom Analytics which we use for a couple of projects as well.
Of course, Laravel Analytics isn't the first Spatie package. Please look at this extensive list of Laravel and PHP packages we've made before. I'm sure there's something there for your next project. If you want to support our open-source efforts, consider picking up one of our paid products or subscribe at Mailcoach and/or Flare.
Hey Freek, would you be ok with having a link to an alternative package https://github.com/gtmassey/laravel-analytics?
We've built it some months ago and we based it on initial work on spatie/laravel-analytics. We've worked hard to make our package extensible and we also support complex and reusable filtering.