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.