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!

A Laravel package to retrieve Google Analytics data

If you need to retrieve some data from your Google Analytics account in Laravel 5, then laravel-analytics is the package for you. Assuming the analytics tracking code is installed on your site, the package allows you to determine which pages are visited the most, which browsers are used most to visit your site, and lots more.

When using it in conjunction with a chart library like chart.js you could make some nice visualisations of the traffic on your website:


The package can also cache the responses of the Google API for a certain amount of time.

In addition to the standard methods to fetch visitors, pageviews, etc for a certain period there is also a method with which you can fire of you own query. Take a look at the core reporting API  to get an idea of what’s possible.

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.
  • Juukie14

    Awesome! Thanks for sharing!

  • Many thanks for this, been looking for something similar and this package seem to do it 🙂 would have been nice if the provided methods accepted a data range rather than just last x number of days.

  • Eric Gagnon

    Is there any way to use it with multiple view ids?

    • Currently not. Feel free to submit a pull request to add that functionality.

    • Roark McColgan

      One way is to publish the config file:

      php artisan vendor:publish –provider=”SpatieLaravelAnalyticsLaravelAnalyticsServiceProvider”

      then set the ‘siteId’ config value on the fly:
      Config::set(‘laravel-analytics.siteId’, ‘ga:XXXXXX’);

      before calling LaravelAnalytics.

  • Petar Gabud

    Hi. I getting an error on local L5 installation:

    Google_Exception in Resource.php line 163:
    (get) missing required param: ‘ids’Have no idea why that happening. Cannot find that error on google search. I made step by step from your github tutorial.

    • Nick Shontz

      Did you ever get a resolution? i’m running into the same issue.

      • arkhas

        I’m running the same issue too, any update ? 🙂

  • gabi rusu

    hey guys, i added this part in my index action:

    $analyticsData = LaravelAnalytics::getVisitorsAndPageViews(30);
    return view(‘admin.dashboard’, compact(‘analyticsData’));

    but from what i’ve seen the getVisitorsAndPageViews method is not static; it’s public… any ideea on what should I do?

    I get this error: Non-static method SpatieLaravelAnalyticsLaravelAnalytics::getVisitorsAndPageViews() should not be called statically, assuming $this from incompatible context

    • you probably imported the class and not the facade. Make sure there is a line at the beginning of your file that reads
      “use LaravalAnaltyics;”

  • Siawash Kasra

    Google_Auth_Exception in OAuth2.php line 357:Error refreshing the OAuth2 token, message: ‘{
    “error” : “invalid_grant”

    I am implementing the laravel-analytics package but it is giving me the above error every time, I don’t know what to do I am new to this!

    • I’ve provided detailed instructions in the readme of the package. If it still isn’t working after following those instructions, please look through the issues on GitHub, maybe someone else had the same problem as you. If it still fails, please create an issue on GitHub.

      • adsofts

        I try to use this package but the readme installation procedure for API credentials is out of time. On Google Console steps does not match. I’m not able to find the clientId and the siteId.
        It is possible to have an update of the readme according to the actual Google Console ?
        Thank you

    • Did you manage to get this working? I am getting the same error

  • Interuptic

    Nice one! Perhaps a chart.js template file for visualizing getVisitorsAndPageViews would be nice goodie as well 🙂

  • Marllon

    i try to perform a query witch ‘Analytics::performQuery(Period::days(10),’ga:sessions’);’ but it returns a object and not the results.

    • adsofts

      So you know how to find siteId and clientId in GoogleConsole ? (the steps in package readme are differents from what i see on GoogleConsole). Can you help me please ?