A small Laravel package that drops the Google Analytics 4 tracking snippet into your Blade layout. One directive, one env var, no ceremony.
- PHP 8.3+
- Laravel 10.x, 11.x, 12.x, or 13.x
composer require ronald2wing/laravel-ga4The service provider is auto-registered.
Add your measurement ID to .env:
GA4_MEASUREMENT_ID=G-XXXXXXXXXXOptionally publish the config:
php artisan vendor:publish --tag=ga4-configForwarded as the third argument to gtag('config', id, parameters):
// config/ga4.php
'parameters' => [
'send_page_view' => true,
'anonymize_ip' => true,
],Place @ga4 in your layout's <head>:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ config('app.name') }}</title>
@ga4
</head>
<body>
@yield('content')
</body>
</html>The directive renders the tracking snippet, or nothing at all if no measurement ID is configured — so it's safe to drop into any layout unconditionally.
Ga4Tag::isEnabled() lets you guard analytics-aware UI:
@use(Ronald2Wing\LaravelGa4\Ga4Tag)
@if(app(Ga4Tag::class)->isEnabled())
<button data-track="sign_up">Sign Up</button>
@endifGa4Tag implements Htmlable and Stringable, so {{ $tag }} and {!! $tag !!} both work if you'd rather inject the instance directly.
composer test # run tests
composer check # lint + test
composer lint # Pint dry-run
composer pint # Pint fix
composer analyse # PHPStanMIT © Ronald2Wing