Build your own google analytics dashboard with Laravel

Build your own google analytics dashboard with Laravel

Mar 10, 2019 / Ramesh Mhetre

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(
    Period::years(1),
    'ga:sessions',
    [
        '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.


Similar Articles

Connect your laravel application to Sqlite DB

Connect your laravel application to Sqlite DB

Quickly connecting with sqlite database within laravel application
Read more...
Laravel v6 & Laravel Vapor is going to release soon 🚀

Laravel v6 & Laravel Vapor is going to release soon 🚀

Laravel v6 is is releasing soon. Serverless laravel is next big thing in laravel community!
Read more...
Compiling assets with Laravel Mix

Compiling assets with Laravel Mix

Laravel Mix provides a fluent API for defining Webpack build steps
Read more...