filament-google-analytics icon indicating copy to clipboard operation
filament-google-analytics copied to clipboard

Fix for error with empty "Unique users" for today/yesterday on Filament v.2

Open RChutchev opened this issue 9 months ago • 4 comments

I've same problem (https://github.com/bezhanSalleh/filament-google-analytics/issues/38) with your package on ver. 2.0.0, with Filament v.2, with about to empty GA data, on test environment. We're unable to use package without pass all tests on Test environment (b/c some time it's possible to have 0 as "today unique users" for example at 0:01AM for current day.

We're unable to update to Filament v.3 for some reasons. Can u please add same fix for your package (2.0.0 ver) b/c 2.0.0 ver last supported version for Filament v.2, or add support for Filament v.2 to 2.0.1 package version if 2.0.1 works with Filament v.2.

vendor/bezhansalleh/filament-google-analytics/src/Traits/Visitors.php

Undefined array key 1 {"userId":1,"exception":"[object] (ErrorException(code: 0): Undefined array key 1 at .../vendor/laravel/framework/src/Illuminate/Collections/Collection.php:1781)

It's require to modify this function

private function visitorsToday(): array
    {
        $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::days(1));

        return [
            'result' => $analyticsData[0]['activeUsers'],
            'previous' => $analyticsData[1]['activeUsers'],
        ];
    }

and here too, to be sure that we are never get 500 if data empty...

    private function visitorsYesterday(): array
    {
        $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::create(Carbon::yesterday()->clone()->subDay(), Carbon::yesterday()));

        return [
            'result' => $analyticsData[0]['activeUsers'],
            'previous' => $analyticsData[1]['activeUsers'],
        ];
    }

Originally posted by @RChutchev in https://github.com/bezhanSalleh/filament-google-analytics/issues/38#issuecomment-2119046697

RChutchev avatar May 19 '24 00:05 RChutchev