diff --git a/src/GA4MeasurementProtocol.php b/src/GA4MeasurementProtocol.php index a9e0344..beec29c 100644 --- a/src/GA4MeasurementProtocol.php +++ b/src/GA4MeasurementProtocol.php @@ -2,25 +2,43 @@ namespace Freshbitsweb\LaravelGoogleAnalytics4MeasurementProtocol; +use Closure; use Exception; use Illuminate\Support\Facades\Http; class GA4MeasurementProtocol { - private string $clientId = ''; + /** + * @var string|Closure + */ + private $clientId; + + protected string $measurementId; + + protected string $apiSecret; private bool $debugging = false; - public function __construct() + /** + * GA4MeasurementProtocol constructor. + * @param string $measurementId + * @param string $apiSecret + * @param string|Closure $clientId + */ + public function __construct(string $measurementId, string $apiSecret, $clientId = 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->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; @@ -36,17 +54,13 @@ public function enableDebugging(): self public function postEvent(array $eventData): 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, + 'client_id' => $this->clientId instanceof Closure ? ($this->clientId)() : $this->clientId, 'events' => [$eventData], ]); diff --git a/src/GA4ServiceProvider.php b/src/GA4ServiceProvider.php index 2a85db7..4f42821 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'); }