Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Laravel Analytics v5 with support for GA4 has been released

Original – by Freek Van der Herten – 3 minute read

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.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Vytautas Smilingis avatar

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.

obeyimmigrate avatar

Take full advantage of the chance to make memories that will last a lifetime by losing yourself in the spectacular. basket random

Angel Brown avatar

That's a great update. I've been waiting for that one! Tree Removal, Venice, FL

Thái Nguyên Khôi avatar

Does version 4.1.0 support GA4. I'm using laravel-9 so can't install version 5. Thank you

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.