phplaravellaravel-5google-analytics-api

How to use Laravels spatie/laravel analytics package


I just downloaded this package for Laravel.

spatie/laravel-analytics

Its a Google Anayltics package, and I followed all the steps for setting up an account. What I'm having trouble is calling the methods. For example when it says:

Here is an example to retrieve visitors and pageview data for the current day and the last seven days.

$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));

I tried doing this in my function like this:

<?php

namespace App\Http\Controllers\Admin;

use Carbon\Carbon;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Spatie\Analytics\Analytics;
use Illuminate\Support\Collection;

class DashboardController extends Controller {


        public function index() { 

            $analytics = Analytics::fetchVisitorsAndPageViews(Period::days(7));  
            dd($analytics);

            return view('admin.dashboard-v2');
        }

}

Its giving me errors like:

Non-static method Spatie\Analytics\Analytics::fetchVisitorsAndPageViews() should not be called statically

Am I missing something here? I couldn't find any specific documentation online except for the Github ReadMe file


Solution

  • If you want to use the facade to access the class, you'll need to change use Spatie\Analytics\Analytics; to use Analytics;. That should take care of that error.

    If you are going to use Period::days(7) then you will need to add use Spatie\Analytics\Period; because that's an actual static method, not a facade.