Class 'Analytics' not found

Viewed 339

I just downloaded this package on my localhost. Spatie/laravel-analytics its a Google Analytics Package i have followed the documenation of package and it works offline.


Offline configuration Laravel 8.1.2 Spatie/Analytics 3.11.0 PHP 7.3.27 (Windows)
Online configuration Laravel 8.1.2 Spatie/Analytics 3.11.0 PHP 7.4 (Linux) - no access to composer
It doesn't work, I get the following error:

Class 'Analytics' not found A class import is missing You have a missing class import. Try importing this class: Google\Service\Analytics.

My controller

<?php

namespace App\Http\Controllers;

use Analytics;
use Carbon\Carbon;
use Spatie\Analytics\Period;

class Analyticsdash extends Component
{
    public function render()
    {
        $analyticsConfig = config('analytics');

        $startDate = Carbon::now()->subYear();
        $endDate = Carbon::now();
        $period = Period::create($startDate, $endDate);

        $analyticsData = Analytics::performQuery(
            $period,
            'ga:sessions',
            [
                'metrics' => 'ga:sessions, ga:pageviews',
                'dimensions' => 'ga:yearMonth',
            ]
        );
        $sessions = '';
        $pageviews = '';
        foreach ($analyticsData as $data) {
            if ($sessions) {
                $sessions .= ', ';
                $pageviews .= ', ';
            }
            $sessions .= $data[1];
            $pageviews .= $data[2];
        }
        dd($analyticsData);
    }
}

Thanks in advance.

1 Answers

You're instantiating :

$analyticsData = Analytics::performQuery(...)

but as the error says, you don't import any class called "Analytics" (that it can find, at least) - so Spatie Analytics probably doesn't provide a facade and will have to be imported with the full class name.

It's suggesting what it thinks is the most appropriate class (Google\Service\Analytics) but if it's the Spatie one you want to import then you need to import it as follows (looking at their Github repo) :

use Spatie\Analytics\Analytics;
Related