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.

Laravel

Similar Articles

PHP develop your own package with local composer environment

PHP develop your own package with local composer environment

Developing your own PHP composer package locally & integrating local file symlink within your application can speeds up development immensely.
Read more...
Laravel Zero: A Micro-framework for console applications

Laravel Zero: A Micro-framework for console applications

Get leverage of laravel magic to create console applications within a micro framework.
Read more...
Convert webpage to PDF or image in laravel

Convert webpage to PDF or image in laravel

Browsershot is package to convert webpage to image or PDF in laravel.
Read more...