[Chartjs]-Load data from Google Analytics in a Chart.js chart

3πŸ‘

βœ…

It might be too late but for anyone who is in need, try the following. For your case you have your $analyticsData collection, which is a good start. Chartjs uses lists to map and compare data.
So for example we want to see a chart of date vs visitors

//transfer all of this code out of the routes to your controller

$analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::days(30));

//send these two variables to your view

$dates = $analyticsData->pluck('date');
$visitors = $analyticsData->pluck('visitors');

In your script, the labels should receive $dates in a list as follows;

labels: {!! json_encode($dates) !!},

And data in datasets receive $visitors as a list;

data: {!! json_encode($visitors) !!}

See this article on my website for more in-depth explanations.

Leave a comment