bezhanSalleh / filament-google-analytics

Google Analytics integration with Filamentphp (FilamentAdmin)
MIT License
162 stars 28 forks source link

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

Open RChutchev opened 4 months ago

RChutchev commented 4 months ago

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 commented 4 months ago

Something like this should fix the problem with the visitors feature.

Also, looks like, you should replace period with Carbon::today() otherwise we get data for yesterday. b/c real data is Today: [], and Yesterday: [{"date":"2024-05-18T01:30:17.000000Z","activeUsers":3,"screenPageViews":79},{"date":"2024-05-17T01:30:17.000000Z","activeUsers":12,"screenPageViews":102}]

in Traits/Visitors.php

[2024-05-19 03:24:29] env.INFO: Today: [{"date":"2024-05-18T01:24:29.000000Z","activeUsers":3,"screenPageViews":79}]
    private function visitorsToday(): array
    {
        $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::create(Carbon::today(), Carbon::today()));

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

Same problem may happen with Page Views, if data for yesterday will be 0. I think better to add '?? 0' here too.

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

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

In src/Traits/PageViews.php Same problem with Today's data, I mean yesterday's data and should be replaced with something like this.

    private function pageViewsToday(): array
    {
        $analyticsData = Analytics::fetchTotalVisitorsAndPageViews(Period::create(Carbon::today(), Carbon::today()));

        return [
            'result' => $analyticsData->last()['screenPageViews'] ?? 0,
            'previous' => $analyticsData->first()['screenPageViews'] ?? 0,
        ];
    }

It's possible that other widgets have the same problem, but I haven't tested that.

RChutchev commented 4 months ago

Please double check everything, or I can help you with a fix if needed, but inconsistent data is the last thing users like...

And I really appreciate your work.

RChutchev commented 4 months ago

About data inconsistency, for example page-views widget.

"Today" data Screenshot 2024-05-19 at 7 32 23 AM Screenshot 2024-05-19 at 7 32 59 AM

and "yesterday" data

Screenshot 2024-05-19 at 7 32 40 AM Screenshot 2024-05-19 at 7 32 12 AM

As you can see, real GA data for yesterday marked as today's data with your package.

RChutchev commented 4 months ago

@bezhanSalleh could you please participate?