-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGA4ServiceProvider.php
More file actions
53 lines (46 loc) · 1.92 KB
/
GA4ServiceProvider.php
File metadata and controls
53 lines (46 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Freshbitsweb\LaravelGoogleAnalytics4MeasurementProtocol;
use Exception;
use Illuminate\Support\Facades\Blade;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
class GA4ServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
/*
* This class is a Package Service Provider
*
* More info: https://github.com/spatie/laravel-package-tools
*/
$package->name('laravel-google-analytics-4-measurement-protocol')
->hasConfigFile()
->hasViews()
->hasRoute('web');
}
public function registeringPackage(): void
{
$this->app->bind('ga4', function () {
if (config('google-analytics-4-measurement-protocol.measurement_id') === null
|| config('google-analytics-4-measurement-protocol.api_secret') === null
) {
throw new Exception('Please set .env variables for Google Analytics 4 Measurement Protocol as per the readme file first.');
}
return new GA4MeasurementProtocol(
config('google-analytics-4-measurement-protocol.measurement_id'),
config('google-analytics-4-measurement-protocol.api_secret'),
function () {
$clientId = session(config('google-analytics-4-measurement-protocol.client_id_session_key'));
if (empty($clientId)) {
throw new Exception('Please use the package provided blade directive or set client_id manually before posting an event.');
}
return $clientId;
}
);
});
}
public function bootingPackage(): void
{
Blade::component('google-analytics-4-measurement-protocol::components.google-analytics-client-id', 'google-analytics-client-id');
}
}