New

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

astrogoat/gtm

A Google Tag Manager app for Strata
13,898
Install
composer require astrogoat/gtm
Latest Version:4.7.0
PHP:^8.0
Laravel:8 – 11
License:MIT
Last Updated:Aug 25, 2025
Links: GitHub  ·  Packagist
Maintainer: tonning

A GTM (Google Tag Manager) app for Strata

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Installation

You can install the package via composer:

composer require astrogoat/gtm

If you plan on using the flash-functionality you must install the GoogleTagManagerMiddleware, after the StartSession middleware:

// app/Http/Kernel.php

protected $middleware = [
    ...
    \Illuminate\Session\Middleware\StartSession::class,
    \Spatie\GoogleTagManager\GoogleTagManagerMiddleware::class,
    ...
];

Usage

This will require two includes to be added to BRAND/layout/app.blade.php

<head> 
    ...
    @include('gtm::header-script')
</head>

<body>
    ...
    @include('gtm::body-script')
</body>

Your base dataLayer will also be rendered here. To add data, use the set() function.

// HomeController.php

public function index()
{
    GoogleTagManager::set('pageType', 'productDetail');

    return view('home');
}

This renders:

<html>
  <head>
    <script>dataLayer = [{"pageType":"productDetail"}];</script>
    <script>/* Google Tag Manager's script */</script>
    <!-- ... -->
  </head>
  <!-- ... -->
</html>

Flashing data for the next request

The package can also set data to render on the next request. This is useful for setting data after an internal redirect.

// ContactController.php

public function getContact()
{
    GoogleTagManager::set('pageType', 'contact');

    return view('contact');
}

public function postContact()
{
    // Do contact form stuff...

    GoogleTagManager::flash('formResponse', 'success');

    return redirect()->action('ContactController@getContact');
}

After a form submit, the following dataLayer will be parsed on the contact page:

<html>
  <head>
    <script>dataLayer = [{"pageType":"contact","formResponse":"success"}];</script>
    <script>/* Google Tag Manager's script */</script>
    <!-- ... -->
  </head>
  <!-- ... -->
</html>

Other Simple Methods

// Retrieve your Google Tag Manager id
$id = GoogleTagManager::id(); // GTM-XXXXXX

// Check whether script rendering is enabled
$enabled = GoogleTagManager::isEnabled(); // true|false

// Add data to the data layer (automatically renders right before the tag manager script). Setting new values merges them with the previous ones. Set also supports dot notation.
GoogleTagManager::set(['foo' => 'bar']);
GoogleTagManager::set('baz', ['ho' => 'dor']);
GoogleTagManager::set('baz.ho', 'doorrrrr');

// [
//   'foo' => 'bar',
//   'baz' => ['ho' => 'doorrrrr']
// ]

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

astrogoat/promobar

A Promobar app for Strata

15,351 0
astrogoat/zaius

A Zaius app for Strata

15,307 0
astrogoat/yotpo

A Yotpo app for Strata

15,213 0
astrogoat/zendesk

A Zendesk app for Strata

15,196 0
astrogoat/talkable

Talkable app for Strata

15,173 0

Version History

Version Released PHP Laravel License
4.7.0 ^8.0 ^8.37 || ^9.0|^10.0|^11.0 MIT
4.6.0 ^8.0 ^8.37 || ^9.0|^10.0|^11.0 MIT
4.5.0 ^8.0 ^8.37 || ^9.0|^10.0|^11.0 MIT
4.4.0 ^8.0 ^8.37 || ^9.0|^10.0|^11.0 MIT
4.3.0 ^8.0 ^8.37 || ^9.0|^10.0|^11.0 MIT