| Install | |
|---|---|
composer require ronald2wing/laravel-ga4 |
|
| Latest Version: | 1.1.0 |
| PHP: | ^8.3 |
A lightweight Laravel package for seamless Google Analytics 4 (GA4) integration with Blade directives and secure JavaScript injection.
{!! ga4() !!} in Blade templatescomposer require ronald2wing/laravel-ga4
The package automatically registers its service provider and facade.
Add your GA4 Measurement ID to .env:
GA4_MEASUREMENT_ID=G-XXXXXXXXXX
How to find your Measurement ID:
Add the tracking script to your Blade layout:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Application</title>
</head>
<body>
<!-- Your application content -->
<!-- Add GA4 tracking before closing body tag -->
{!! ga4() !!}
</body>
</html>
The package uses a single environment variable:
GA4_MEASUREMENT_ID=G-XXXXXXXXXX
Publish the configuration file to customize settings:
php artisan vendor:publish --tag=ga4-config
This creates config/ga4.php:
return [
/*
|--------------------------------------------------------------------------
| Google Analytics 4 Measurement ID
|--------------------------------------------------------------------------
|
| Your GA4 measurement ID (format: G-XXXXXXXXXX).
|
*/
'measurement_id' => env('GA4_MEASUREMENT_ID', ''),
];
The simplest way to use the package is with the ga4() helper function:
<!-- In any Blade template, typically in your layout file -->
{!! ga4() !!}
You can also access the GA4 service programmatically using the facade:
use Ronald2Wing\LaravelGa4\Facades\Ga4;
// Get the rendered script
$script = Ga4::render();
// Check if GA4 is configured
if (Ga4::render() !== '') {
// GA4 is active and ready
}
<!-- Only render in production environment -->
@if(app()->environment('production'))
{!! ga4() !!}
@endif
<!-- Only render for authenticated users -->
@auth
{!! ga4() !!}
@endauth
<!-- Only render when measurement ID is set -->
@if(config('ga4.measurement_id'))
{!! ga4() !!}
@endif
<!-- Combine multiple conditions -->
@if(app()->environment('production') && config('ga4.measurement_id'))
{!! ga4() !!}
@endif
If you need to integrate with custom JavaScript, you can access the generated script:
use Ronald2Wing\LaravelGa4\Facades\Ga4;
// Get the raw JavaScript (without script tags)
$service = app('ga4');
$measurementId = config('ga4.measurement_id');
// Or use the facade to get complete HTML
$html = Ga4::render();
For different environments, use different measurement IDs:
# Development (optional - can be empty)
GA4_MEASUREMENT_ID=
# Staging
GA4_MEASUREMENT_ID=G-STG123456
# Production
GA4_MEASUREMENT_ID=G-PRO123456
Symptoms: No GA4 script in HTML output
Solutions:
.env has GA4_MEASUREMENT_ID setG-XXXXXXXXXXphp artisan config:clearSymptoms: Console shows GA4-related errors
Solutions:
Symptoms: Duplicate page_view events in GA4
Solutions:
page_view by defaultgtag('config') calls in your codehtmlspecialchars()# Add to .gitignore
.env
.env.local
.env.*.local
# Development (optional - can be empty)
GA4_MEASUREMENT_ID=
# Staging
GA4_MEASUREMENT_ID=G-STG123456
# Production
GA4_MEASUREMENT_ID=G-PRO123456
# Check for vulnerabilities
composer audit
# Update dependencies
composer update
# Run all tests
composer test
# Generate HTML coverage report
composer test-coverage
# Run tests with verbose output
composer test-verbose
# Run specific test method(s)
composer test-filter testRenderReturnsEmptyStringWhenNotConfigured
# Static analysis with PHPStan
composer analyse
# Check code style (dry run)
composer lint
# Fix code style issues
composer pint
The package maintains 100% test coverage with:
# Install dependencies
composer install
# Run full test suite with linting check
composer check
# Update dependencies
composer update
# Check for outdated packages
composer outdated
# Check for security vulnerabilities
composer audit
# Generate coverage report (HTML)
composer test-coverage
# Run tests with verbose output
composer test-verbose
# Fix code style issues with Pint
composer pint
# Static analysis with PHPStan
composer analyse
This package is open-sourced software licensed under the MIT license.
Contributions are welcome! Please:
Made with ❤️ by Ronald2Wing
If you find this package useful, please consider: