Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Laravel Analytics v2 has been released

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.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band. He loves waffles and butterflies.
  • Thanks for updating a nicely done package! been using it on all my project and has been very helpful to grab google analytics data.

    it would be helpful to see some info on “whats new in v2” and “to move form v1 to v2 you need to do X and Y”

    • Basically everything has changed. There’s a new way of authorizing with Google and all the method names have changed. The package is not that big. If you read the readme you should know enough to move to v2. If you have any further questions on v2, feel free to open up an issue on GitHub.

      • Many thanks for the clarifications and the reply. Will surely look into it and move to v2 on first chance.

  • Raffaele Ricci

    This package seems really handy, but I haven’t upgraded to php 7 yet and it looks like it’s a requirement for the v2 🙁
    By the way I recently subscribed to the newsletter and I find it really interesting! Thank you for sharing your work and knowledge!

  • Pingback: Laravel Analytics v2 - Laravel News()

  • Pingback: Community News: Recent posts from PHP Quickfix (06.22.2016) – SourceCode()

  • Pingback: Laravel Analytics v2 | 神刀安全网()

  • Pingback: Laravel Analytics v2 - Javascript Solutions()

  • Pingback: Laravel Analytics v2 – Rifix Blog()

  • Roark McColgan

    Hi Freek,

    I’m having a bit of trouble getting the package to work, I am running Laravel 5.2.45. When I add the package via “composer require spatie/laravel-analytics” it is pulling in ^1.4. and it seems the facade and service provider are still “SpatieLaravelAnalytics” and not “SpatieAnalytics” as specified in the readme. Along with that it seems there are substantial changes to the config file generated.

    Which version, to your knowledge, is the best version to pull in for L5.2?

    Thanks Freek!

    • Roark McColgan

      Right, since I’m still running PHP 5 (cough, cough) I cant use v2,

      While using V1.4, I a getting o-auth errors:
      “Error refreshing the OAuth2 token, message: ‘{
      “error” : “invalid_grant”,
      “error_description” : “Invalid issuer: null”
      Any Ideas? Thanks!

      • Sorry, I’m not supporting v1 of the package anymore. Upgrade to PHP 7 / v2 of laraval-analytics 🙂

        • Roark McColgan

          No problem! Thanks Freek

  • Trevor Albrecht

    Can this package be used to get analytics data from multiple views dynamically? We are building an application to put all our marketing clients in one place to be able to create custom reports for each. LOVE all your work!

  • Roy Vincent Lisondra Niepes

    Hi, will this work on laravel 5.0? planning to test this tomorrow.

  • joostman_nl

    Is it possible to get all the unique users and all pageviews total?

  • What if the view_id becomes dynamic?

  • Lailanie Almero


    PHP Fatal error: Class ‘SpatieLaravelAnalyticsAnalyticsServiceProvider’ not found in C:xampphtdocsjpt-dashvendorlaravelframeworksrcIlluminateFoundat
    onProviderRepository.php on line 146

    Class ‘SpatieLaravelAnalyticsAnalyticsServiceProvider’ not found

    I am having problem in my provider….

  • Mohamed Elbiheiry

    sorry but i’m having a problem here , i managed to make the analytics on google and now i want to call it on my admin panel .
    it gives me this error always :
    Undefined index: service_account_credentials_json
    can you help me ?