This plugin seamlessly integrates Google Tag Manager (GTM) into your website, enabling streamlined management and deployment of marketing tags, analytics, and tracking pixels. With easy-to-implement script inclusion and customizable options, it simplifies the process of monitoring user interactions and gathering valuable insights without altering your site's core code. Perfect for marketers and developers looking to enhance their tracking capabilities with minimal effort.
- PHP 8.2+
- Laravel 11.0+
spatie/laravel-settings^3.0 (installed automatically)
You can install the package via composer:
composer require jeffersongoncalves/laravel-gtmRun the migrations to create the settings table and seed the GTM settings:
php artisan migrateThe GTM ID is stored in the database via spatie/laravel-settings. Configure it at runtime using any of the following approaches:
use JeffersonGoncalves\Gtm\Settings\GtmSettings;
$settings = app(GtmSettings::class);
$settings->gtm_id = 'GTM-XXXXXX';
$settings->save();$settings = gtm_settings();
$settings->gtm_id = 'GTM-XXXXXX';
$settings->save();If you need to customize the settings migration:
php artisan vendor:publish --tag=gtm-settings-migrationsAdd the GTM head script inside your <head> tag, as high as possible:
@include('gtm::head')
{{-- or use the Blade directive --}}
@gtmHeadAdd the GTM body noscript immediately after the opening <body> tag:
@include('gtm::body')
{{-- or use the Blade directive --}}
@gtmBodyThe scripts are only rendered when a valid GTM ID is configured. A valid ID matches the pattern
GTM-XXXXXXX(uppercase letters and digits).
// Via helper
$gtmId = gtm_settings()->gtm_id;
// Via container
$gtmId = app(\JeffersonGoncalves\Gtm\Settings\GtmSettings::class)->gtm_id;
// Via Facade (resolve the underlying settings instance first)
$gtmId = \JeffersonGoncalves\Gtm\Facades\Gtm::getFacadeRoot()->gtm_id;$settings = gtm_settings();
$settings->gtm_id = 'GTM-NEWID';
$settings->save();composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.