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 analytics features to better understand your audience and improve your website's effectiveness.
2,302 2
Install
composer require jeffersongoncalves/laravel-gtag
Latest Version:v3.0.0
PHP:^8.2|^8.3|^8.4
License:MIT
Last Updated:Sep 6, 2026
Links: GitHub  ·  Packagist
Maintainer: jeffersongoncalves

Laravel Gtag

Laravel Google Analytics

Buy Me A Coffee

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

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.

  1. Remove the old config file: config/gtag.php
  2. Remove the GTAG_ID environment variable (no longer used)
  3. Publish and run the settings migration
  4. 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.

Related Packages

jeffersongoncalves/filament-cookie-consent

This Filament package provides a simple and elegant way to implement cookie cons...

14,776 8
jeffersongoncalves/filament-cep-field

The Filament CEP Field is a custom input component designed specifically for Bra...

4,489 8
jeffersongoncalves/filament-refresh-sidebar

Automatically refresh the Filament sidebar navigation when certain events occur,...

2,543 11
jeffersongoncalves/filament-logo

A simple yet effective Filament plugin that automatically adds the Filament logo...

13,517 5
jeffersongoncalves/filament-ace-editor-field

A Laravel Filament v4 field that integrates the Ace code editor into your forms,...

10,311 4