Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions src/GA4MeasurementProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand Down
24 changes: 21 additions & 3 deletions src/GA4ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Freshbitsweb\LaravelGoogleAnalytics4MeasurementProtocol;

use Exception;
use Illuminate\Support\Facades\Blade;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand All @@ -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.');
}
Comment on lines +28 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that someone will face this exception for any request just after installing the package until they set the .env vars?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This called only on resolving 'ga4'. Same as in the constructor.


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;
}
Comment on lines +37 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the benefit of passing the closure from here instead of keeping the login inside the facade?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Service class should not know about configuration. But provider must configuring service.

);
});
}

public function bootingPackage()
public function bootingPackage(): void
{
Blade::component('google-analytics-4-measurement-protocol::components.google-analytics-client-id', 'google-analytics-client-id');
}
Expand Down