ethar's avatar
Level 5

more function on spatie/google analytics packages

this is the first time I used spatie/google analytics packages to get website visitors

https://github.com/spatie/laravel-analytics

its provide some function, but I want more than that like: 1- total active now and visited pages. 2- country and number of user fore each country.

any help

0 likes
5 replies
ethar's avatar
Level 5

@sinnbeck for example


        $analyticsData = Analytics::performQuery(
            Period::years(1),
            'ga:sessions',
            [
                'metrics' => 'ga:sessions, ga:pageviews',
                'dimensions' => 'ga:yearMonth'
            ]
        );
        return $analyticsData;

but i got error

ethar's avatar
Level 5

@sinnbeck TypeError Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given, called in /home4/panorax1/lcode/vendor/laravel/framework/src/Illuminate/Http/Response.php on line 65

ethar's avatar
Level 5

to getTopOs For Example


    public function fetchTopOs(Period $period, int $maxResults = 10): Collection
    {
        $response = $this->performQuery(
            $period,
            'ga:sessions',
            [
                'dimensions' => 'ga:operatingSystem',
                'sort' => '-ga:sessions',
            ]
        );

        $topOs = collect($response['rows'] ?? [])->map(function (array $osRow) {
            return [
                'operatingSystem' => $osRow[0],
                'sessions' => (int) $osRow[1],
            ];
        });

        if ($topOs->count() <= $maxResults) {
            return $topOs;
        }

        return $this->summarizeTopOS($topOs, $maxResults);
    }

Please or to participate in this conversation.