Skip to content

Moved configuring to service provider. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
44 changes: 29 additions & 15 deletions src/GA4MeasurementProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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],
]);

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