Build your own google analytics dashboard with Laravel

Build your own google analytics dashboard with Laravel


Using google analytics dashboard on a regular basis can be a cumbersome for people like me, especially when you just need to check a few metrics regularly. I made my own mini dashboard to only show required data from google analytics the way I wants to see it.

We have fetched data from Google Analytics API using package spatie/laravel-analytics. Like all other packages from spatie, this package is also well documented & updated. It took me a time of one nice coffee to build highcharts graph with this API which becomes really handy at the end.

Check out the documentation on GitHub to learn more about how you can set up this package.

Some handfull functions from this package:

  • Fetch pageviews data for last 30 days
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(30));

  • This package also has set of handull functions like
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.

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

// Query example
$analyticsData = Analytics::performQuery(
        'metrics' => 'ga:sessions, ga:pageviews, ga:users',
        'dimensions' => 'ga:yearMonth'

Google Core Reporting API provides more information about which metrics & dimensions can be used to build your own query.