-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
Comment on lines
+37
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
} | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.