PHPackages                             protonemedia/laravel-analytics-event-tracking - 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. [API Development](/categories/api)
4. /
5. protonemedia/laravel-analytics-event-tracking

AbandonedArchivedLibrary[API Development](/categories/api)

protonemedia/laravel-analytics-event-tracking
=============================================

Laravel package to easily send events to Google Analytics

1.6.0(3y ago)243192.9k↓22.1%25[3 issues](https://github.com/protonemedia/laravel-analytics-event-tracking/issues)MITPHPPHP ^8.1 || ^8.2

Since May 7Pushed 2y ago6 watchersCompare

[ Source](https://github.com/protonemedia/laravel-analytics-event-tracking)[ Packagist](https://packagist.org/packages/protonemedia/laravel-analytics-event-tracking)[ Docs](https://github.com/protonemedia/laravel-analytics-event-tracking)[ GitHub Sponsors](https://github.com/pascalbaljet)[ RSS](/packages/protonemedia-laravel-analytics-event-tracking/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (17)Used By (0)

> ⚠️ This package is unmaintained and doesn't work with GA4.

Laravel Analytics Event Tracking
================================

[](#laravel-analytics-event-tracking)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8a285d0db41c362453553f5e36974a88c5b78ae4c4ea902535383347b3b3596c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70726f746f6e656d656469612f6c61726176656c2d616e616c79746963732d6576656e742d747261636b696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/protonemedia/laravel-analytics-event-tracking)[![run-tests](https://github.com/protonemedia/laravel-analytics-event-tracking/workflows/run-tests/badge.svg)](https://github.com/protonemedia/laravel-analytics-event-tracking/workflows/run-tests/badge.svg)[![Quality Score](https://camo.githubusercontent.com/8906694f9c6611979906a42accf71fe6450341295d0d8b5091f21a6028c9b13f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f70726f746f6e656d656469612f6c61726176656c2d616e616c79746963732d6576656e742d747261636b696e672e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/protonemedia/laravel-analytics-event-tracking)[![Total Downloads](https://camo.githubusercontent.com/2951632447c5905a4b2a59923220359eb5871ff1106c65ca4abc2bc977e0e86e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70726f746f6e656d656469612f6c61726176656c2d616e616c79746963732d6576656e742d747261636b696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/protonemedia/laravel-analytics-event-tracking)[![Buy us a tree](https://camo.githubusercontent.com/130148911f548b001b2ac68a32c0a06559977ca60ada3bf480c72ae4ea093175/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54726565776172652d2546302539462538432542332d6c69676874677265656e)](https://plant.treeware.earth/protonemedia/laravel-analytics-event-tracking)

Laravel package to easily send events to [Google Analytics](https://analytics.google.com/)

Sponsor this package!
---------------------

[](#sponsor-this-package)

❤️ We proudly support the community by developing Laravel packages and giving them away for free. If this package saves you time or if you're relying on it professionally, please consider [sponsoring the maintenance and development](https://github.com/sponsors/pascalbaljet). Keeping track of issues and pull requests takes time, but we're happy to help!

Laravel Splade
--------------

[](#laravel-splade)

**Did you hear about Laravel Splade? 🤩**

It's the *magic* of Inertia.js with the *simplicity* of Blade. [Splade](https://github.com/protonemedia/laravel-splade) provides a super easy way to build Single Page Applications using Blade templates. Besides that magic SPA-feeling, it comes with more than ten components to sparkle your app and make it interactive, all without ever leaving Blade.

Features
--------

[](#features)

- Use [Laravel Events](https://laravel.com/docs/7.x/events) to track events with GA.
- [Blade Directive](https://laravel.com/docs/7.x/blade#introduction) to easily store the Client ID.
- Full access to the [underlying library](https://github.com/theiconic/php-ga-measurement-protocol).
- API calls to GA are queued.
- Easy to configure.
- Compatible with Laravel 9.0.
- PHP 8.0 or higher required.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require protonemedia/laravel-analytics-event-tracking
```

Configuration
-------------

[](#configuration)

Publish the config and view files:

```
php artisan vendor:publish --provider="ProtoneMedia\AnalyticsEventTracking\ServiceProvider"
```

Set your [Google Analytics Tracking ID](https://support.google.com/analytics/answer/1008080) in the `.env` file or in the `config/analytics-event-tracking.php` file.

```
GOOGLE_ANALYTICS_TRACKING_ID=UA-01234567-89
```

This package supports [Google Analytics 4](https://blog.google/products/marketingplatform/analytics/new_google_analytics/) as of version 1.2.1. Please republish the view file if you're upgrading to a new Google Analytics 4 property.

Blade Directive
---------------

[](#blade-directive)

This package comes with a `@sendAnalyticsClientId` directive that sends the [Client ID](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#clientId) from the GA front-end to your Laravel backend and stores it in the session.

It uses the [Axios HTTP library](https://github.com/axios/axios) the make an asynchronous POST request. Axios was choosen because it is provided by default in Laravel in the `resources/js/bootstrap.js` file.

Add the directive somewhere after initializing/configuring GA. The POST request will only be made if the `Client ID` isn't stored yet or when it's refreshed.

```

  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-01234567-89', { 'send_page_view': false });
  gtag('event', 'page_view', { 'event_callback': function() {
      @sendAnalyticsClientId
  }});

```

If you don't use Axios, you have to implement this call by yourself. By default the endpoint is `/gaid` but you can customize it in the configuration file. The request is handled by the `ProtoneMedia\AnalyticsEventTracking\Http\StoreClientIdInSession` class. Make sure to also send the [CSRF token](https://laravel.com/docs/7.x/csrf).

Broadcast events to Google Analytics
------------------------------------

[](#broadcast-events-to-google-analytics)

Add the `ShouldBroadcastToAnalytics` interface to your event and you're ready! You don't have to manually bind any listeners.

```
