PHPackages                             devpro/laravel-ga4-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. devpro/laravel-ga4-event-tracking

Abandoned → [luketowers/laravel-ga4-event-tracking](/?search=luketowers%2Flaravel-ga4-event-tracking)Library

devpro/laravel-ga4-event-tracking
=================================

Simplifies using the Measurement Protocol for Google Analytics 4 to track events in Laravel applications.

v2.1.2(12mo ago)2141.5k↓25.7%8MITPHPPHP ^8.1CI passing

Since Mar 6Pushed 12mo ago4 watchersCompare

[ Source](https://github.com/LukeTowers/laravel-ga4-event-tracking)[ Packagist](https://packagist.org/packages/devpro/laravel-ga4-event-tracking)[ Docs](https://github.com/LukeTowers/laravel-ga4-event-tracking)[ RSS](/packages/devpro-laravel-ga4-event-tracking/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (9)Versions (9)Used By (0)

Laravel Google Analytics 4 Measurement Protocol Event Tracking
==============================================================

[](#laravel-google-analytics-4-measurement-protocol-event-tracking)

[![Version](https://camo.githubusercontent.com/4b9594596a6abdeb7b308050e099ae3004c6e505b35ad0e53eb3fcfaf6a9c8ae/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6c756b65746f776572732f6c61726176656c2d6761342d6576656e742d747261636b696e673f736f72743d73656d766572267374796c653d666c61742d737175617265)](https://github.com/luketowers/laravel-ga4-event-tracking/releases)[![Tests](https://camo.githubusercontent.com/9be7a4b585697eacb2d6ae0cd276cf512cf276e946e9044f1f975dc638d7c038/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6c756b65746f776572732f6c61726176656c2d6761342d6576656e742d747261636b696e672f72756e2d74657374732e796d6c3f266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/luketowers/laravel-ga4-event-tracking/actions)[![License](https://camo.githubusercontent.com/5a1a7ecc906d47d6a93e102762bfc164ee2a0b476049c753063c732865497f4c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c756b65746f776572732f6c61726176656c2d6761342d6576656e742d747261636b696e673f6c6162656c3d6f70656e253230736f75726365267374796c653d666c61742d737175617265)](https://packagist.org/packages/luketowers/laravel-ga4-event-tracking)

Simplifies using the [Measurement Protocol for Google Analytics 4](https://developers.google.com/analytics/devguides/collection/protocol/ga4) to track events in Laravel applications.

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

[](#installation)

1. Install package via Composer

```
composer require luketowers/laravel-ga4-event-tracking
```

2. Set `GA4_MEASUREMENT_ID` and `GA4_MEASUREMENT_PROTOCOL_API_SECRET` in your .env file.

> Copy from `Google Analytics > Admin > Data Streams > [Select Site] > Measurement ID` &amp; `Google Analytics > Admin > Data Streams > [Select Site] > Measurement Protocol API secrets` respectively.

3. Optional: Publish the config / view files by running this command in your terminal:

```
php artisan vendor:publish --tag=ga4-event-tracking.config --tag=ga4-event-tracking.views
```

4. Include the `sendGA4ClientID` directive in your layout file after the Google Analytics Code tracking code.

```

@sendGA4ClientID

```

The [`client_id`](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#payload_post_body) is required to send an event to Google Analytics. This package provides a Blade directive which you can put in your layout file after the Google Analytics Code tracking code. It grabs the current user's GA `client_id` from either the `ga()` or `gtag()` helper functions injected by Google Analytics and makes a POST request to your application to store the `client_id` in the session, which is later used by the `DispatchAnalyticsJob` when sending events to GA4.

If you do not use this blade directive, you will have to handle retrieving, storing, and sending the `client_id` yourself. You can use `GA$::setClientId($clientId)` to set the `client_id` manually.

Usage
-----

[](#usage)

This package provides two ways to send events to Google Analytics 4:

### Directly via the `GA4` facade:

[](#directly-via-the-ga4-facade)

Sending event directly is as simple as calling the `sendEvent($eventData)` method on the `GA4` facade from anywhere in your backend to post event to Google Analytics 4. `$eventData` contains the name and params of the event as per this [reference page](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#login). For example:

```
GA4::sendEvent([
    'name' => 'login',
    'params' => [
        'method' => 'Google',
    ],
]);
```

The `sendEvent()` method will return an array with the status of the request.

### Broadcast events to GA4 via the Laravel Event System

[](#broadcast-events-to-ga4-via-the-laravel-event-system)

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

```
