spatie/laravel-googletagmanager
Laravel package for easy Google Tag Manager integration. Manage the JavaScript dataLayer from PHP, push variables and events, and include GTM scripts cleanly in your app with simple configuration and helpers.
Pros:
config, publishes, ServiceProvider).Illuminate\Auth\Events\Registered) for dynamic tag firing, enabling granular control over GTM triggers.@gtm.push() for inline JavaScript injection, integrating seamlessly with Laravel’s templating engine.config/gtm.php, adhering to Laravel’s 12-factor principles.Cons:
Html::script()), which may not suit headless or API-first architectures.Illuminate\Auth\Events\Login).app.js or API responses), increasing complexity. Consider pairing with a frontend framework (e.g., Vue/React) for dynamic tagging.Cache::remember), GTM tags may not fire. Requires explicit cache bypassing (e.g., Cache::forget() before tag injection).gtm.js) adds ~1KB to page weight; may require lazy-loading for performance-critical apps.app.js).Cart::itemAdded → ecommerce.add event).composer require spatie/laravel-googletagmanager
php artisan vendor:publish --provider="Spatie\GoogleTagManager\GoogleTagManagerServiceProvider"
config/gtm.php with container ID and preview mode.@gtm.push() in views or @include('gtm::script') for global tags.use Spatie\GoogleTagManager\GoogleTagManager;
use Illuminate\Support\Facades\Event;
Event::listen('Illuminate\Auth\Events\Login', function ($event) {
GoogleTagManager::push(['event' => 'user_login']);
});
GoogleTagManager::push([
'event' => 'purchase',
'ecommerce' => [
'purchase' => ['actionField' => ['id' => 'T12345']]
]
]);
dataLayer pushes).config/gtm.php post-testing.guzzlehttp/guzzle (for GTM API, if used).app.blade.php or Inertia’s shared layout.@script directives or Livewire hooks.index.html or via API response headers.config/gtm.php, reducing deployment risks.How can I help you explore Laravel packages today?