Scout APM is PHP application performance monitoring designed for developers. With tracing logic that ties issues back to the line of code causing them, you can pinpoint n+1 queries, memory leaks, and other abnormalities in real time so you can knock them out and get back to building a great product. Start your free 14-day trial today and get the performance insight you need in less than 4 minutes.

Laravel Analytics v2 has been released

Original – by Freek Van der Herten – 2 minute read

One of our more popular packages is laravel-analytics. The package makes it easy to fetch information such as pageviews, top referrers, etc... from the Google Analytics API. In our Blender-based projects we use the fetched data to display a nice chart in the admin section: Screen Shot 2016-06-19 at 19.33.53

Laravel-analytics is one of the first packages we ever created. Since the first release the preferred way of authenticating with Google has changed. We also learned a lot on how to create readable code. That's why we did a cleanup of the code and published a new major version of the package.


Learning how to authenticate with a Google API can be quite a pain. A previous post already touched on this:

If you’ve ever worked with some API’s by Google then you know their documentation can be very confusing. It’s not that they don’t have documentation, but code examples of common use cases are simply not present. You must wade through a lot of pages to learn basic things such as how to make an authorized request.

To prevent potential tableflips on your part there's some documentation included to guide you through Google's authentication process.


Once the package is installed this is how to retrieve visitor and pageview data for the past week.

$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));

$analyticsData is a collection in which each item is an array that holds keys date, visitors and pageViews.

These are the other functions the package provides:

public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection;

public function fetchTopReferrers(Period $period, int $maxResults = 20): Collection;

public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection;

To perform all other queries on the Google Analytics resource you can call performQuery. Google's Core Reporting API provides more information on on which metrics and dimensions might be used.

public function performQuery(Period $period, string $metrics, array $others = [])

Check out the documentation on GitHub to learn more. If you like laravel-analtyics, be sure to check out our other Laravel packages as well.

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

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks 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.