From 823c745b95e3ef0622826d07b81198a19e11e4a7 Mon Sep 17 00:00:00 2001 From: Dmytro Kulyk Date: Fri, 30 Apr 2021 12:13:12 +0300 Subject: [PATCH 1/3] Moved configuring to ServiceProvider --- src/GA4MeasurementProtocol.php | 45 ++++++++++++++++++++-------------- src/GA4ServiceProvider.php | 24 +++++++++++++++--- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/GA4MeasurementProtocol.php b/src/GA4MeasurementProtocol.php index a9e0344..753eaa2 100644 --- a/src/GA4MeasurementProtocol.php +++ b/src/GA4MeasurementProtocol.php @@ -2,22 +2,29 @@ namespace Freshbitsweb\LaravelGoogleAnalytics4MeasurementProtocol; +use Closure; use Exception; use Illuminate\Support\Facades\Http; class GA4MeasurementProtocol { + protected string $measurementId; + + protected string $apiSecret; + + protected Closure $clientIdResolver; + private string $clientId = ''; private bool $debugging = false; - public function __construct() + public function __construct(string $measurementId, string $apiSecret, Closure $clientIdResolver = null) { - 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.'); - } + $this->measurementId = $measurementId; + $this->apiSecret = $apiSecret; + $this->clientIdResolver = $clientIdResolver ?? static function () { + throw new Exception('Please set clientId resolver or specify clientId manually.'); + }; } public function setClientId(string $clientId): self @@ -34,31 +41,33 @@ public function enableDebugging(): self return $this; } - public function postEvent(array $eventData): array + public function post(array $payload): array { - if (!$this->clientId && !$this->clientId = session(config('google-analytics-4-measurement-protocol.client_id_session_key'))) { - throw new Exception('Please use the package provided blade directive or set client_id manually before posting an event.'); - } - $response = Http::withOptions([ 'query' => [ - 'measurement_id' => config('google-analytics-4-measurement-protocol.measurement_id'), - 'api_secret' => config('google-analytics-4-measurement-protocol.api_secret'), + 'measurement_id' => $this->measurementId, + 'api_secret' => $this->apiSecret, ], - ])->post($this->getRequestUrl(), [ - 'client_id' => $this->clientId, - 'events' => [$eventData], - ]); + ])->post($this->getRequestUrl(), array_merge($payload, [ + 'client_id' => $this->clientId ?: ($this->clientIdResolver)(), + ])); if ($this->debugging) { return $response->json(); } return [ - 'status' => $response->successful() + 'status' => $response->successful(), ]; } + public function postEvent(array $eventData, array $payload = []): array + { + return $this->post(array_merge($payload, [ + 'events' => [$eventData], + ])); + } + private function getRequestUrl(): string { $url = 'https://www.google-analytics.com'; diff --git a/src/GA4ServiceProvider.php b/src/GA4ServiceProvider.php index 2a85db7..6416a44 100644 --- a/src/GA4ServiceProvider.php +++ b/src/GA4ServiceProvider.php @@ -2,6 +2,7 @@ namespace Freshbitsweb\LaravelGoogleAnalytics4MeasurementProtocol; +use Exception; use Illuminate\Support\Facades\Blade; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -21,14 +22,31 @@ public function configurePackage(Package $package): void ->hasRoute('web'); } - public function registeringPackage() + public function registeringPackage():void { $this->app->bind('ga4', function () { - return new GA4MeasurementProtocol(); + 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() + public function bootingPackage(): void { Blade::component('google-analytics-4-measurement-protocol::components.google-analytics-client-id', 'google-analytics-client-id'); } From 508ab6c02ff9d3ba7657e5c2d2435286a89e9493 Mon Sep 17 00:00:00 2001 From: Dmytro Kulyk Date: Fri, 30 Apr 2021 12:15:01 +0300 Subject: [PATCH 2/3] CS --- src/GA4ServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GA4ServiceProvider.php b/src/GA4ServiceProvider.php index 6416a44..4f42821 100644 --- a/src/GA4ServiceProvider.php +++ b/src/GA4ServiceProvider.php @@ -22,7 +22,7 @@ public function configurePackage(Package $package): void ->hasRoute('web'); } - public function registeringPackage():void + public function registeringPackage(): void { $this->app->bind('ga4', function () { if (config('google-analytics-4-measurement-protocol.measurement_id') === null From 646ffecd2a0906c3cb216496177b414e3734ebbe Mon Sep 17 00:00:00 2001 From: Dmytro Kulyk Date: Sat, 1 May 2021 17:24:15 +0300 Subject: [PATCH 3/3] Cleanup, using only clientId property. --- src/GA4MeasurementProtocol.php | 47 +++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/GA4MeasurementProtocol.php b/src/GA4MeasurementProtocol.php index 753eaa2..beec29c 100644 --- a/src/GA4MeasurementProtocol.php +++ b/src/GA4MeasurementProtocol.php @@ -8,26 +8,37 @@ class GA4MeasurementProtocol { + /** + * @var string|Closure + */ + private $clientId; + protected string $measurementId; protected string $apiSecret; - protected Closure $clientIdResolver; - - private string $clientId = ''; - private bool $debugging = false; - public function __construct(string $measurementId, string $apiSecret, Closure $clientIdResolver = null) + /** + * GA4MeasurementProtocol constructor. + * @param string $measurementId + * @param string $apiSecret + * @param string|Closure $clientId + */ + public function __construct(string $measurementId, string $apiSecret, $clientId = null) { $this->measurementId = $measurementId; $this->apiSecret = $apiSecret; - $this->clientIdResolver = $clientIdResolver ?? static function () { - throw new Exception('Please set clientId resolver or specify clientId manually.'); - }; + $this->setClientId($clientId ?? static function () { + throw new Exception('Please specify clientId manually.'); + }); } - public function setClientId(string $clientId): self + /** + * @param string|Closure $clientId + * @return $this + */ + public function setClientId($clientId): self { $this->clientId = $clientId; @@ -41,33 +52,27 @@ public function enableDebugging(): self return $this; } - public function post(array $payload): array + public function postEvent(array $eventData): array { $response = Http::withOptions([ 'query' => [ 'measurement_id' => $this->measurementId, 'api_secret' => $this->apiSecret, ], - ])->post($this->getRequestUrl(), array_merge($payload, [ - 'client_id' => $this->clientId ?: ($this->clientIdResolver)(), - ])); + ])->post($this->getRequestUrl(), [ + 'client_id' => $this->clientId instanceof Closure ? ($this->clientId)() : $this->clientId, + 'events' => [$eventData], + ]); if ($this->debugging) { return $response->json(); } return [ - 'status' => $response->successful(), + 'status' => $response->successful() ]; } - public function postEvent(array $eventData, array $payload = []): array - { - return $this->post(array_merge($payload, [ - 'events' => [$eventData], - ])); - } - private function getRequestUrl(): string { $url = 'https://www.google-analytics.com';