jeffersongoncalves / laravel-gtag
This Laravel package provides a straightforward integration of Google Analytics using Gtag into your Blade templates. It enables you to easily track website visits and user engagement, offering valuable insights into your site's performance. With minimal setup, you can leverage Gtag's powerful analy
Installs: 65
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
pkg:composer/jeffersongoncalves/laravel-gtag
Requires
- php: ^8.2|^8.3
- laravel/framework: ^11.0|^12.0
- spatie/laravel-package-tools: ^1.14.0
- spatie/laravel-settings: ^3.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.27
- orchestra/testbench: ^9.0|^10.0
- pestphp/pest: ^3.7.4
- pestphp/pest-plugin-laravel: ^3.0
This package is auto-updated.
Last update: 2026-02-22 23:42:54 UTC
README
Laravel Google Analytics
This Laravel package provides a straightforward integration of Google Analytics using Gtag into your Blade templates. It enables you to easily track website visits and user engagement, offering valuable insights into your site's performance. With minimal setup, you can leverage Gtag's powerful analytics features to better understand your audience and improve your website's effectiveness.
Settings are stored in the database using spatie/laravel-settings, allowing dynamic management via admin panels or code.
Installation
You can install the package via composer:
composer require jeffersongoncalves/laravel-gtag
Publish and run the settings migrations:
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan vendor:publish --tag=gtag-settings-migrations php artisan migrate
Usage
Add the script tag to your Blade layout (typically in the <head>):
@include('gtag::script')
Configuration via Code
You can update settings at any time using the GtagSettings class, the gtag_settings() helper, or the Gtag facade:
use JeffersonGoncalves\Gtag\Settings\GtagSettings; // Via container $settings = app(GtagSettings::class); $settings->gtag_id = 'G-XXXXXXXXXX'; $settings->enabled = true; $settings->save(); // Via helper gtag_settings()->gtag_id = 'G-XXXXXXXXXX'; gtag_settings()->save(); // Via Facade use JeffersonGoncalves\Gtag\Facades\Gtag; $gtag = Gtag::getFacadeRoot(); $gtag->gtag_id = 'G-XXXXXXXXXX'; $gtag->save();
Available Properties
| Property | Type | Default | Description |
|---|---|---|---|
gtag_id |
?string |
null |
Google Tag ID (e.g., G-XXXXXXXXXX) |
enabled |
bool |
true |
Enable/disable script rendering |
anonymize_ip |
bool |
false |
Anonymize visitor IPs (GDPR compliance) |
additional_config |
array |
[] |
Extra parameters for gtag('config', ...) |
Additional Config Example
$settings = gtag_settings(); $settings->additional_config = [ 'send_page_view' => false, 'cookie_domain' => 'example.com', ]; $settings->save();
Upgrading from v1
Version 2 replaces the config/gtag.php configuration file with database settings via spatie/laravel-settings. This is a breaking change.
- Remove the old config file:
config/gtag.php - Remove the
GTAG_IDenvironment variable (no longer used) - Publish and run the settings migration
- Set your Google Tag ID via code (see Usage above)
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
