PHPackages                             label84/laravel-tagmanager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. label84/laravel-tagmanager

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

label84/laravel-tagmanager
==========================

Easier way to add Google Tag Manager to your Laravel application.

v1.5.2(1mo ago)4598.6k↓31.6%2[1 issues](https://github.com/Label84/laravel-tagmanager/issues)MITPHPCI passing

Since Dec 22Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/Label84/laravel-tagmanager)[ Packagist](https://packagist.org/packages/label84/laravel-tagmanager)[ RSS](/packages/label84-laravel-tagmanager/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (12)Versions (20)Used By (0)

Laravel TagManager
==================

[](#laravel-tagmanager)

[![Latest Stable Version](https://camo.githubusercontent.com/3efe31a361a21f50cfa9ee3f67c9f96e96f01beaee12b989031478b412e4fb2e/68747470733a2f2f706f7365722e707567782e6f72672f6c6162656c38342f6c61726176656c2d7461676d616e616765722f762f737461626c653f7374796c653d666c61742d737175617265)](https://packagist.org/packages/label84/laravel-tagmanager)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/71262696009cb03b5cdccb0970d39d377cc761acd9d1ed27e7fc7dc496c39884/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6162656c38342f6c61726176656c2d7461676d616e616765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/label84/laravel-tagmanager)

Easier way to add Google Tag Manager to your Laravel application. Including support for User-ID, E-commerce and Server Side Events (Measurement Protocol).

- [Laravel support](#laravel-support)
- [Installation](#installation)
- [Usage](#usage)
    - [Events](#events)
    - [User-ID](#user-id)
    - [Ecommerce (GA4)](#ecommerce-ga4)
        - [Ecommerce Item](#ecommerce-item)
        - [Ecommerce Events](#ecommerce-events)
- [Server Side Events](#server-side-events)
    - [Measurement Protocol](#measurement-protocol)
    - [Measurement Protocol Debug Mode](#measurement-protocol-debug-mode)
- [Tests](#tests)
- [License](#license)

Laravel support
---------------

[](#laravel-support)

VersionRelease13.x^1.512.x^1.511.x^1.5Installation
------------

[](#installation)

Read the article on Medium.com for a more detailed instruction: [Medium: Add Google Analytics to your Laravel application with Google Tag Manager](https://tjardo.medium.com/add-google-analytics-to-your-laravel-application-with-google-tag-manager-c32eb0e1dc9a).

### 1. Install package

[](#1-install-package)

Install the package

```
composer require label84/laravel-tagmanager
```

### 2. Add the head and body tag to your (layout) view

[](#2-add-the-head-and-body-tag-to-your-layout-view)

Add the head tag directly above the closing `` tag and the body tag directly after the opening `` tag.

```

```

### 3. Add the TagManagerMiddleware to your Kernel

[](#3-add-the-tagmanagermiddleware-to-your-kernel)

Add the `TagManagerMiddleware` to your 'web' middleware group in your `bootstrap/app.php` file.

```
// bootstrap/app.php

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        //
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->web(append: [
            \Label84\TagManager\Http\Middleware\TagManagerMiddleware::class,
        ]);
    })
```

### 4. Publish the config file

[](#4-publish-the-config-file)

Publish the config file. This will generate a `tagmanager.php` file in your config directory.

```
php artisan vendor:publish --provider="Label84\TagManager\TagManagerServiceProvider" --tag="config"
```

### 5. Add your GTM ID to your .env

[](#5-add-your-gtm-id-to-your-env)

```
// .env

GOOGLE_TAG_MANAGER_ID=GTM-XXXXXX
GOOGLE_TAG_MANAGER_ENABLED=true
```

Go to `https://tagmanager.google.com` and copy the 'Container ID' of the account (it looks like GTM-XXXXXXX).

Usage
-----

[](#usage)

```
use Label84\TagManager\Facades\TagManager;

TagManager::push(['foo' => 'bar']);
```

### Events

[](#events)

You can also use the following methods. These will automatically set the correct event key and value.

```
use Label84\TagManager\Facades\TagManager;

TagManager::event('kissed', ['status' => 'failed', 'count' => 0]);
TagManager::login(['foo' => 'bar']);
TagManager::register(['foo' => 'bar']);
```

You can find a list of recommended events on:

### User-ID

[](#user-id)

This package also supports the User-ID feature.

To start using the User-ID feature you've to add the `TagManagerUserIdMiddleware` in your 'web' middleware group directly after the `TagManagerMiddleware`.

```
// bootstrap/app.php

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        //
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->web(append: [
            \Label84\TagManager\Http\Middleware\TagManagerMiddleware::class,
            \Label84\TagManager\Http\Middleware\TagManagerUserIdMiddleware::class,
        ]);
    })
```

By default the 'id' of the User model will be used. You change the key in `config/tagmanager.php`.

More information:

### Ecommerce (GA4)

[](#ecommerce-ga4)

You can use the following snippets to trigger an Ecommerce event with Google Analytics 4 (GA4).

#### Ecommerce item

[](#ecommerce-item)

The `TagManagerItem` class allows you to easily create an Ecommerce item. You can set extra parameters with dynamic calls. Method names are used as keys and automatically converted to underscore case.

```
use Label84\TagManager\TagManagerItem;

new TagManagerItem(string $id, string $name, float $price, float $quantity);
```

##### Example: create item

[](#example-create-item)

```
use Label84\TagManager\TagManagerItem;

$item1 = new TagManagerItem('12345', 'Triblend Android T-Shirt', 15.25, 1);
$item1->itemBrand('Google')       // will add the item parameter { item_brand: 'Google' }
      ->itemCategory('Apparel')   // will add the item parameter { item_category: 'Apparel' }
      ->itemVariant('Gray');      // will add the item parameter { item_variant: 'Gray' }
```

#### Ecommerce events

[](#ecommerce-events)

The items parameter can be a single `TagManagerItem` item or an array of `TagManagerItem` items. You can also use plain arrays if you don't want to use the TagManagerItem class.

```
use Label84\TagManager\Facades\TagManager;

// Product views and interactions
TagManager::viewItemList($items);
TagManager::viewItem($items);
TagManager::selectItem($items);

// Promotion views and interactions
TagManager::viewPromotion($items);
TagManager::selectPromotion($items);

// Pre-purchase interactions
TagManager::addToWishList(string $currency, float $value, $items);
TagManager::addToCart($items);
TagManager::removeFromCart($items);
TagManager::viewCart(string $currency, float $value, $items);

// Purchases, checkouts, and refunds
TagManager::beginCheckout($items);
TagManager::addPaymentInfo(string $currency, float $value, string $paymentType, $items, string $coupon = '');
TagManager::addShippingInfo(string $currency, float $value, string $shippingTier, $items, string $coupon = '');
TagManager::purchase(string $transactionId, string $affiliation, string $currency, float $value, float $tax, float $shipping, $items, string $coupon = '');
TagManager::refund(string $transactionId, string $affiliation, string $currency, float $value, float $tax, float $shipping, $items, string $coupon = '');
```

##### Example: call event with item

[](#example-call-event-with-item)

```
use Label84\TagManager\Facades\TagManager;

TagManager::purchase('00001', 'Google', 'EUR', 12.10, 2.10, 0, [
    new TagManagerItem('12345', 'Triblend Android T-Shirt', 10.00, 1),
]);
```

More information: [https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client\_type=gtm](https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtm)

Server Side Events
------------------

[](#server-side-events)

The Google Analytics Measurement Protocol allows developers to make HTTP requests to send raw user interaction data directly to Google Analytics servers. This allows developers to measure how users interact with their business from almost any environment. Developers can then use the Measurement Protocol to:

- Measure user activity in new environments.
- Tie online to offline behavior.
- Send data from both the client and server.

### Measurement Protocol

[](#measurement-protocol)

You need complete the general installation steps first, such as adding the `.env` variables, adding the head and body tags to your layout file and adding the `TagManagerMiddleware`.

Add the following extra variables to your .env file.

```
// .env

# Found in the Google Analytics UI under Admin > Data Streams > choose your stream > Measurement ID. The measurement_id isn't your Stream ID.
GOOGLE_MEASUREMENT_ID=G-XXXXXX
# Found in the Google Analytics UI under Admin > Data Streams > choose your stream > Measurement Protocol API Secrets
GOOGLE_MEASUREMENT_PROTOCOL_API_SECRET=XXXXXX
```

Add the following snippet to the head of your blade layout file (below the existing `x-tagmanager-head` tag).

```

```

```
use Label84\TagManager\Facades\MeasurementProtocol;

MeasurementProtocol::event('some_event', ['foo' => 'bar']);

// Set a specific User-ID for this event (you can customize the key in the config file)
MeasurementProtocol::user($someUser)->event('some_event', ['foo' => 'bar']);
```

You can view the events directly in the Google Analytics UI under Realtime &gt; Events.

### Measurement Protocol Debug Mode

[](#measurement-protocol-debug-mode)

You can enable the debug mode by calling the `debug()` method. This will return a JSON validation response instead of sending the request to Google Analytics. If there are any errors, they will be returned in the validation messages response. If there are no errors, the validation response array will be empty.

```
use Label84\TagManager\Facades\MeasurementProtocol;

dd(
    MeasurementProtocol::debug()->event('some_event', ['foo' => 'bar'])
);
```

Tests
-----

[](#tests)

```
./vendor/bin/pint
./vendor/bin/phpstan analyze
```

License
-------

[](#license)

[MIT](https://opensource.org/licenses/MIT)

###  Health Score

55

—

FairBetter than 97% of packages

Maintenance92

Actively maintained with recent releases

Popularity43

Moderate usage in the ecosystem

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~101 days

Recently: every ~223 days

Total

17

Last Release

36d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/78c37d1496f19445af6a812d2275c34bbd08dead68aebae70229d7ff0670ed85?d=identicon)[tjardo](/maintainers/tjardo)

---

Top Contributors

[![tjardoo](https://avatars.githubusercontent.com/u/31533540?v=4)](https://github.com/tjardoo "tjardoo (20 commits)")

---

Tags

google-analyticslaravellaravel-package

###  Code Quality

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/label84-laravel-tagmanager/health.svg)

```
[![Health](https://phpackages.com/badges/label84-laravel-tagmanager/health.svg)](https://phpackages.com/packages/label84-laravel-tagmanager)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[illuminate/pagination

The Illuminate Pagination package.

12234.1M1.0k](/packages/illuminate-pagination)[illuminate/pipeline

The Illuminate Pipeline package.

9349.2M282](/packages/illuminate-pipeline)[illuminate/redis

The Illuminate Redis package.

8314.6M375](/packages/illuminate-redis)[spatie/laravel-export

Create a static site bundle from a Laravel app

674146.0k6](/packages/spatie-laravel-export)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
