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

AbandonedArchivedLibrary[API Development](/categories/api)

daikazu/laravel-ga4-event-tracking
==================================

Laravel package to easily send events to Google Analytics 4

43584PHP

Since Nov 28Pushed 2y ago1 watchersCompare

[ Source](https://github.com/daikazu/laravel-ga4-event-tracking)[ Packagist](https://packagist.org/packages/daikazu/laravel-ga4-event-tracking)[ RSS](/packages/daikazu-laravel-ga4-event-tracking/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

Laravel Google Analytics 4 Measurement Protocol EventTracking
=============================================================

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

A Laravel package to use [Measurement Protocol for Google Analytics 4](https://developers.google.com/analytics/devguides/collection/protocol/ga4).

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

[](#installation)

1. Install package via Composer

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

2. Set `MEASUREMENT_ID` and `MEASUREMENT_PROTOCOL_API_SECRET` in your .env file. You can find this information from: Google Analytics &gt; Admin &gt; Data Streams &gt; \[Select Site\] &gt; Measurement Protocol API secrets
3. Optional: You can publish the config file by running this command in your terminal/cmd:

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

4. `client_id` is required to post 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 makes a POST request to the backend to store the client id in the session which is later used to post events to Google Analytics 4.

```

@sendClientID

```

The other option is to call the `setClientId($clientId)` method on the `GA4` facade everytime before calling the `sendEvent()` method.

Usage
-----

[](#usage)

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

1. ### Directly

    [](#directly)

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',
    ],
]);
```

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

2. ### Broadcast events to Google Analytics 4 via the Laravel Event System

    [](#broadcast-events-to-google-analytics-4-via-the-laravel-event-system)

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

```
