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

v2.0.0 2026-02-21 02:16 UTC

This package is auto-updated.

Last update: 2026-02-22 23:42:54 UTC


README

Laravel Google Analytics

Laravel Google Analytics

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.