New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

jeffersongoncalves/laravel-gtm

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.
6,920 4
Install
composer require jeffersongoncalves/laravel-gtm
Latest Version:3.1.0
PHP:^8.2|^8.3|^8.4
Laravel:12 · 13
Older releases:11
License:MIT
Last Updated:Oct 16, 2026
Links: GitHub  ·  Packagist
Maintainer: jeffersongoncalves

Laravel Gtm

Laravel Google Tag Manager

Buy Me A Coffee

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

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.

Requirements

  • PHP 8.2+
  • Laravel 11.0+
  • spatie/laravel-settings ^3.0 (installed automatically)

Installation

You can install the package via composer:

composer require jeffersongoncalves/laravel-gtm

Run the migrations to create the settings table and seed the GTM settings:

php artisan migrate

Configuration

Setting your GTM ID

The GTM ID is stored in the database via spatie/laravel-settings. Configure it at runtime using any of the following approaches:

Via dependency injection
use JeffersonGoncalves\Gtm\Settings\GtmSettings;

$settings = app(GtmSettings::class);
$settings->gtm_id = 'GTM-XXXXXX';
$settings->save();
Via helper function
$settings = gtm_settings();
$settings->gtm_id = 'GTM-XXXXXX';
$settings->save();

Publishing the settings migration (optional)

If you need to customize the settings migration:

php artisan vendor:publish --tag=gtm-settings-migrations

Usage

Add the GTM head script inside your <head> tag, as high as possible:

@include('gtm::head')
{{-- or use the Blade directive --}}
@gtmHead

Add the GTM body noscript immediately after the opening <body> tag:

@include('gtm::body')
{{-- or use the Blade directive --}}
@gtmBody

The scripts are only rendered when a valid GTM ID is configured. A valid ID matches the pattern GTM-XXXXXXX (uppercase letters and digits).

Reading the current GTM ID

// 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;

Updating the GTM ID at runtime

$settings = gtm_settings();
$settings->gtm_id = 'GTM-NEWID';
$settings->save();

For EEA/UK visitors Google requires Consent Mode v2. laravel-cookie-consent (4.2+) sends the consent signals: turn on its consent_mode setting and include its head view before this package's snippet, so the tags start with the right consent state:

@include('cookie-consent::cookie-consent-head')
@include('gtm::head')

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/laravel-cookie-consent

This Laravel package provides a simple and elegant way to implement cookie conse...

18,107 5
jeffersongoncalves/laravel-cep

A simple and efficient PHP package for querying Brazilian postal codes (CEP). Th...

6,525 3
jeffersongoncalves/laravel-pwa-favicon

This Laravel package serves a spec-shaped PWA `manifest.json` (with Android dens...

5,222 3
jeffersongoncalves/laravel-favicon

This Laravel package serves a favicon.ico route, a browserconfig.xml for Windows...

4,205 2
jeffersongoncalves/laravel-whatsapp-widget

This Laravel package provides a simple yet customizable WhatsApp widget for your...

3,761 9

Version History

Version Released PHP Laravel License
3.1.0 ^8.2|^8.3|^8.4 ^12.61.1|^13.12.0 MIT
3.0.0 ^8.2|^8.3|^8.4 ^12.61.1|^13.12.0 MIT
2.0.4 ^8.2|^8.3|^8.4 ^11.0|^12.0|^13.0 MIT
2.0.3 ^8.2|^8.3 ^11.0|^12.0|^13.0 MIT
2.0.2 ^8.2|^8.3 ^11.0|^12.0|^13.0 MIT