Closed krievley closed 8 months ago
Universal Analytics in env ANALYTICS_VIEW_ID=
Google Analytics 4 in env ???
Hi,
I think this package coud interest you : https://github.com/MyOutDeskLLC/Laravel-Analytics-V4
It could be used in a laravel nova package ;)
spatie/analytics has just updated to V5 and now supports GA4.
Could it be as easy as cloning this repo and updating it's composer to include Spatie's updated package?
Edit: I gave this a shot and I got relatively far but I'm having an issue.
What I did:
Added this to .env
:
ANALYTICS_PROPERTY_ID=346xxxxxx
The above replaces the old value of ANALYTICS_VIEW_ID
.
Now I'm stuck because it seems the Tighten Nova package references the old view ID somewhere. For example, in the TestCase environment setup this is called:
$app->config->set('analytics.view_id', getenv('ANALYTICS_VIEW_ID'));
The error below indicates Spatie's module is having an issue as it's never and looks for $propertyId
:
Unresolvable dependency resolving [Parameter #1 [ <required> string $propertyId ]] in class Spatie\Analytics\Analytics {"userId":1,"exception":"[object] (Illuminate\\Contracts\\Container\\BindingResolutionException(code: 0): Unresolvable dependency resolving [Parameter #1 [ <required> string $propertyId ]] in class Spatie\\Analytics\\Analytics at /Users/eugene/Code/bestagent/vendor/laravel/framework/src/Illuminate/Container/Container.php:1141)
Any help would be appreciated @krievley
Edit 2:
I took this quite far now and I'm documenting my progress in case someone else wants to do a pull request.
Issues at hand:
$propertyId
.With regards to GA4 API, there are no such things as ga:pageviews
or ga:year
.
There is an entire page dedicated to the translation of the tags: https://developers.google.com/analytics/devguides/migration/api/reporting-ua-to-ga4-dims-mets
For example:
ga:pageviews
GA4 equivalent is screenPageViews
ga:year
GA4 equivalent is year
Tighten's library used the old names so these will have to be translated.
The Spatie API call that's different can be seen here: https://github.com/spatie/laravel-analytics#all-other-google-analytics-queries
public function get(Period $period, array $metrics, array $dimensions = [], int $limit = 10, array $orderBy = [], FilterExpression $dimensionFilter = null): Collection
It appears the Tighten library in MetricDiffTrait
uses ->performQuery
:
$analyticsData = app(Analytics::class)
->performQuery(
$period,
$metric,
[
'metrics' => $metric,
'dimensions' => $dimensions,
'samplingLevel' => 'HIGHER_PRECISION',
]
);
Whereas the Spatie method is now call ->get
. Additionally Spatie accept Arrays whereas the Tighten library sends strings.
Finally, to get around the dependency injection issue, I had to instantiate Analytics using a parameter:
use Spatie\Analytics\Analytics;
$analyticsData = app(Analytics::class,['propertyId' => '346xxxxxx'])
However, I think if you use the Facade instead at Spatie\Analytics\Facades\Analytics
you get better results.
All in all this is above my paygrade. I'm guessing if I'm lucky and I go as far as updating the tests we're looking at around 10 or more hours to get it work. For now unfortunately my client won't pay that.
Deadline: July 1, 2023
Universal Analytics will be replaced with Google Analytics 4. More information can be found here.