Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RChutchev opened this issue May 19, 2024 · 28 comments
Closed

Comments

@RChutchev
Copy link

I've same problem (#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 #38 (comment)

@RChutchev
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

@bezhanSalleh could you please participate?

@bezhanSalleh
Copy link
Owner

for some reason i wasn't watching this repo. apologies for the very late reply.

@RChutchev
Copy link
Author

for some reason i wasn't watching this repo. apologies for the very late reply.

Hi, okay, keep me posted, I'm still waiting for any updates.

@bezhanSalleh
Copy link
Owner

for some reason i wasn't watching this repo. apologies for the very late reply.

Hi, okay, keep me posted, I'm still waiting for any updates.

ok, so i have read your comments but could you clarify a couple of things: first about the versioning you mentioned;
caz Package v1 => Filament v2 and Plugin v2 => Filament v3/Filament Widgets v3 and second the code snippets; are those the fixes you provided or the current implementation returns faulty data. because the snippets are the same as in v2?

@RChutchev
Copy link
Author

RChutchev commented Oct 14, 2024

Yes, okay, sorry for the misunderstanding.
You are a little bit wrong about that, with Filament 2 I've installed "bezhansalleh/filament-google-analytics": "^2.0.0", exact version is 2.0.0. Yes, composer install your package 2.0.0 with Filament 2 with Laravel 10. That's the reason why I write in the first message v.2 with Filament 2.
image

Also, GitHub Dependabot can update some packages to newly available, anyway, 2.0.0 is compatible with Filament 2, but the next one isn't, and fixes for these errors are not available in 2.0.0. I can't fix it by myself because that's weird to add something to the v.2 branch for the v.1 branch, but if I add this fix to the v.1 branch I didn't get it b/c already use the 2.0.0 version.

@bezhanSalleh
Copy link
Owner

bezhanSalleh commented Oct 15, 2024

Yes, okay, sorry for the misunderstanding. You are a little bit wrong about that, with Filament 2 I've installed "bezhansalleh/filament-google-analytics": "^2.0.0", exact version is 2.0.0. Yes, composer install your package 2.0.0 with Filament 2 with Laravel 10. That's the reason why I write in the first message v.2 with Filament 2. image

Also, GitHub Dependabot can update some packages to newly available, anyway, 2.0.0 is compatible with Filament 2, but the next one isn't, and fixes for these errors are not available in 2.0.0. I can't fix it by myself because that's weird to add something to the v.2 branch for the v.1 branch, but if I add this fix to the v.1 branch I didn't get it b/c already use the 2.0.0 version.

ok, so i'm not gonna bore ya with details but i remember now so use the 1.1.2 version, it should resolve your issue.
if not let me know.

@RChutchev
Copy link
Author

RChutchev commented Oct 22, 2024

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.

@bezhanSalleh, hello, we have downgraded package, yes, it fixed 500 error, but this error still here.
Today is Oct. 22, and data is correct, but if I'll choose yesterday data, it's show me data for 20-21 Oct, but should be 21-21 Oct, am I right?

@RChutchev
Copy link
Author

@bezhanSalleh also, looks like something wrong with "Avg. Sessions Duration".
This card show me about 02:00:00 every time, but real value is 33.933530647058824.

image
image
This value coming from your SessionDuration trait
image

@bezhanSalleh
Copy link
Owner

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.

@bezhanSalleh, hello, we have downgraded package, yes, it fixed 500 error, but this error still here. Today is Oct. 22, and data is correct, but if I'll choose yesterday data, it's show me data for 20-21 Oct, but should be 21-21 Oct, am I right?

yeah, I have checked and the only inconsistency was for the page views, which has already been fixed and released for v2 => Filament v3. The v1 => Filament v2 is now released https://github.com/bezhanSalleh/filament-google-analytics/releases/tag/1.1.3

@RChutchev
Copy link
Author

RChutchev commented Oct 22, 2024

@bezhanSalleh, yes, okay, I'll check it now, but what about session duration?
GA returns the value in seconds, but this value is shown incorrectly.

In public function trajectoryValueAsTimeString()
the return value should be something like

sprintf('%d:%d:%d', $this->value / 3600, floor($this->value / 60) % 60, floor($this->value) % 60)

that gives us a correct value but without leading zero

@RChutchev
Copy link
Author

RChutchev commented Oct 22, 2024

@bezhanSalleh
Moreover, we cannot install 1.1.3, because this package version works only with Filament 3 and requires upgrading Filament to ver. 3

    - bezhansalleh/filament-google-analytics 1.1.3 requires filament/widgets ^3.0 -> satisfiable by filament/widgets[v3.0.0, ..., v3.2.119].

@RChutchev
Copy link
Author

RChutchev commented Oct 22, 2024

@bezhanSalleh Also, the average session duration for today is incorrect, it's yesterday's data.
image
image
Because the average session duration for today in my GA is 12 min, 7 sec but in your package, the RAW value is 33.933530647058824, that's yesterday's data.
image

@bezhanSalleh
Copy link
Owner

@bezhanSalleh Moreover, we cannot install 1.1.3, because this package version works only with Filament 3 and requires upgrading Filament to ver. 3

    - bezhansalleh/filament-google-analytics 1.1.3 requires filament/widgets ^3.0 -> satisfiable by filament/widgets[v3.0.0, ..., v3.2.119].

man, i need to sleep... ben up closing to 48hrs now... was in the wrong branch when made the release 🤦🏻‍♂️
should be fine now, just give it a couple of minutes so it gets updated in packagist.

@RChutchev
Copy link
Author

@bezhanSalleh Moreover, we cannot install 1.1.3, because this package version works only with Filament 3 and requires upgrading Filament to ver. 3

    - bezhansalleh/filament-google-analytics 1.1.3 requires filament/widgets ^3.0 -> satisfiable by filament/widgets[v3.0.0, ..., v3.2.119].

man, i need to sleep... ben up closing to 48hrs now... was in the wrong branch when made the release 🤦🏻‍♂️ should be fine now, just give it a couple of minutes so it gets updated in packagist.

Okay, bro, no problems, check it out when you can.
And I'll try to update when the packagist will be updated.

@bezhanSalleh
Copy link
Owner

@bezhanSalleh, yes, okay, I'll check it now, but what about session duration? GA returns the value in seconds, but this value is shown incorrectly.

In public function trajectoryValueAsTimeString() the return value should be something like

sprintf('%d:%d:%d', $this->value / 3600, floor($this->value / 60) % 60, floor($this->value) % 60)

that gives us a correct value but without leading zero

i know, but the api response for duration is in seconds so instead we use Carbon to properly create the required timestamp and peroperly format it.

@bezhanSalleh
Copy link
Owner

@bezhanSalleh Also, the average session duration for today is incorrect, it's yesterday's data. image image Because the average session duration for today in my GA is 12 min, 7 sec but in your package, the RAW value is 33.933530647058824, that's yesterday's data. image

new releases with fix Filam.v2 => 1.1.4 & Filam.v3 => 2.0.3. cheers!

@RChutchev
Copy link
Author

RChutchev commented Oct 22, 2024

@bezhanSalleh Also, the average session duration for today is incorrect, it's yesterday's data. image image Because the average session duration for today in my GA is 12 min, 7 sec but in your package, the RAW value is 33.933530647058824, that's yesterday's data. image

new releases with fix Filam.v2 => 1.1.4 & Filam.v3 => 2.0.3. cheers!

Yes, @bezhanSalleh, I so appreciate your work and only two things that I see as a possible bug.
At least the sessionDurationToday() function has today's data inconsistency.
You are getting today as Period::days(1), which gives us from 22 Oct 2024 to 23 Oct 2024, but that's wrong because today is 23 Oct, which means the date is supposed to be 23 Oct—23 Oct 2024. If you use a Period function, it's required to set Today() as the start and end date Period::create(Carbon::today(), Carbon::today()). Also, the same way to get Today used in some other functions.
The second one is function sessionDurationYesterday(), at least this one gives us yesterday as a period from 2024-10-21 to 22-10-2024, which is not right, because yesterday is 22 Oct—22 Oct. And yes, same way to get Yesterday used in some other functions.

This way to get today's and yesterday's dates used for both packages for Filament 2 and Filament 3, IDK why exactly this way is used in your function, but maybe I got something wrong?

I offer to replace today's functions with this way (example)

$results = $this->get('averageSessionDuration', 'date', Period::create(Carbon::today(), Carbon::today())); 

And yesterday's functions (example)

$results = $this->get('averageSessionDuration', 'date', Period::create(Carbon::yesterday(), Carbon::yesterday()));

@bezhanSalleh
Copy link
Owner

@bezhanSalleh Also, the average session duration for today is incorrect, it's yesterday's data. image image Because the average session duration for today in my GA is 12 min, 7 sec but in your package, the RAW value is 33.933530647058824, that's yesterday's data. image

new releases with fix Filam.v2 => 1.1.4 & Filam.v3 => 2.0.3. cheers!

Yes, @bezhanSalleh, I so appreciate your work and only two things that I see as a possible bug. At least the sessionDurationToday() function has today's data inconsistency. You are getting today as Period::days(1), which gives us from 22 Oct 2024 to 23 Oct 2024, but that's wrong because today is 23 Oct, which means the date is supposed to be 23 Oct—23 Oct 2024. If you use a Period function, it's required to set Today() as the start and end date Period::create(Carbon::today(), Carbon::today()). Also, the same way to get Today used in some other functions. The second one is function sessionDurationYesterday(), at least this one gives us yesterday as a period from 2024-10-21 to 22-10-2024, which is not right, because yesterday is 22 Oct—22 Oct. And yes, same way to get Yesterday used in some other functions.

This way to get today's and yesterday's dates used for both packages for Filament 2 and Filament 3, IDK why exactly this way is used in your function, but maybe I got something wrong?

I offer to replace today's functions with this way (example)

$results = $this->get('averageSessionDuration', 'date', Period::create(Carbon::today(), Carbon::today())); 

And yesterday's functions (example)

$results = $this->get('averageSessionDuration', 'date', Period::create(Carbon::yesterday(), Carbon::yesterday()));

yeah, so after i had some shut eye i reviewed it thoroughly.
we are never getting tomorrows data, for instance for today filter we are getting todays and yesterdays. because we need yesterday's data so we could generate the diff and see if its trending up or down.
anyways, so the issue was in the metric diff and both sessions and average session data were inconsistent, so depending on when you setup your analytics and your analytics timezone we need to do a couple of checks for todays filter to see which is there and adjust the trend and time accordingly.

return match (true) {
    ($results->containsOneItem() && ($results->first()['date'])->isYesterday()) => [
        'previous' => $results->first()['value'],
        'result' => 0,
    ],
    ($results->containsOneItem() && ($results->first()['date'])->isToday()) => [
        'previous' => 0,
        'result' => $results->first()['value'],
    ],
    $results->isEmpty() => [
        'previous' => 0,
        'result' => 0,
    ],
    default => [
        'previous' => $results->last()['value'] ?? 0,
        'result' => $results->first()['value'] ?? 0,
    ]
};

now update your composer to make sure you are on the latest version based on your filament version and hopefully this will be it. so let me know. cheers!

@bezhanSalleh
Copy link
Owner

@RChutchev i will have a look over the weekend to make sure the others will have the same fix. cause i know the page views and unique visitors do require the same check.

@RChutchev
Copy link
Author

@bezhanSalleh Also, the average session duration for today is incorrect, it's yesterday's data. image image Because the average session duration for today in my GA is 12 min, 7 sec but in your package, the RAW value is 33.933530647058824, that's yesterday's data. image

new releases with fix Filam.v2 => 1.1.4 & Filam.v3 => 2.0.3. cheers!

Yes, @bezhanSalleh, I so appreciate your work and only two things that I see as a possible bug. At least the sessionDurationToday() function has today's data inconsistency. You are getting today as Period::days(1), which gives us from 22 Oct 2024 to 23 Oct 2024, but that's wrong because today is 23 Oct, which means the date is supposed to be 23 Oct—23 Oct 2024. If you use a Period function, it's required to set Today() as the start and end date Period::create(Carbon::today(), Carbon::today()). Also, the same way to get Today used in some other functions. The second one is function sessionDurationYesterday(), at least this one gives us yesterday as a period from 2024-10-21 to 22-10-2024, which is not right, because yesterday is 22 Oct—22 Oct. And yes, same way to get Yesterday used in some other functions.
This way to get today's and yesterday's dates used for both packages for Filament 2 and Filament 3, IDK why exactly this way is used in your function, but maybe I got something wrong?
I offer to replace today's functions with this way (example)

$results = $this->get('averageSessionDuration', 'date', Period::create(Carbon::today(), Carbon::today())); 

And yesterday's functions (example)

$results = $this->get('averageSessionDuration', 'date', Period::create(Carbon::yesterday(), Carbon::yesterday()));

yeah, so after i had some shut eye i reviewed it thoroughly. we are never getting tomorrows data, for instance for today filter we are getting todays and yesterdays. because we need yesterday's data so we could generate the diff and see if its trending up or down. anyways, so the issue was in the metric diff and both sessions and average session data were inconsistent, so depending on when you setup your analytics and your analytics timezone we need to do a couple of checks for todays filter to see which is there and adjust the trend and time accordingly.

return match (true) {
    ($results->containsOneItem() && ($results->first()['date'])->isYesterday()) => [
        'previous' => $results->first()['value'],
        'result' => 0,
    ],
    ($results->containsOneItem() && ($results->first()['date'])->isToday()) => [
        'previous' => 0,
        'result' => $results->first()['value'],
    ],
    $results->isEmpty() => [
        'previous' => 0,
        'result' => 0,
    ],
    default => [
        'previous' => $results->last()['value'] ?? 0,
        'result' => $results->first()['value'] ?? 0,
    ]
};

now update your composer to make sure you are on the latest version based on your filament version and hopefully this will be it. so let me know. cheers!

Yes, I'll test it now. Thank you so much!
I hope now everything will work well.

Also, I opened a pull request to add a Russian language translation, I hope it will be merged, maybe someone else requires it too

@bezhanSalleh
Copy link
Owner

bezhanSalleh commented Oct 24, 2024

Screenshot 2024-10-24 at 3 02 40 AM Screenshot 2024-10-24 at 3 02 29 AM
Screen.Recording.2024-10-24.at.3.03.02.AM.mov

@RChutchev as you can see in the above screenshots, you can see we don't have anything for 24, so now the sessions and avg. session duration will have the correct data. it shows 00:00:00 for today but still gets the result for yesterday 00:08:03 and calculates the diff.
I had a couple of days off, that's why i have been able to respond and check this quickly. but i will merge it when i do the fixes in the next release. cheers!

@RChutchev
Copy link
Author

Screenshot 2024-10-24 at 3 02 40 AM Screenshot 2024-10-24 at 3 02 29 AM
Screen.Recording.2024-10-24.at.3.03.02.AM.mov
@RChutchev as you can see in the above screenshots, you can see we don't have anything for 24, so now the sessions and avg. session duration will have the correct data. it shows 00:00:00 for today but still gets the result for yesterday 00:08:03 and calculates the diff. I had a couple of days off, that's why i have been able to respond and check this quickly. but i will merge it when i do the fixes in the next release. cheers!

Yes, right, I just updated the package to 1.2.0 for local env, and yes, it now shows me 0 for empty values for sessions and average. session duration, and when some data comes from GA I am sure data will also be shown.
Yes, looks like now diff for the session duration info works well, thanks!
Okay, that's good, thank you so much for the quick fixes.
Okay, thanks, cheers!

@RChutchev
Copy link
Author

RChutchev commented Oct 25, 2024

@bezhanSalleh, hi, I've tested the last released version, and it looks like the "sessions count" and the "average sessions duration" for today do not work well, and always show 0 as values whatever Google Analytics has value.
image
image

@bezhanSalleh
Copy link
Owner

bezhanSalleh commented Oct 27, 2024

That might be the issue with your cache setting, because the base spatie package caches the query result for 24hrs.
So you might wanna adjust that according to your needs.

@RChutchev
Copy link
Author

That might be the issue with your cache setting, because the base spatie package caches the query result for 24hrs. So you might wanna adjust that according to your needs.

Thanks a lot, yep, I forgot about the Spatie package cache.
Now that works great for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants