Download the PHP package luketowers/laravel-ga4-event-tracking without Composer
On this page you can find all versions of the php package luketowers/laravel-ga4-event-tracking. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download luketowers/laravel-ga4-event-tracking
More information about luketowers/laravel-ga4-event-tracking
Files in luketowers/laravel-ga4-event-tracking
Package laravel-ga4-event-tracking
Short Description Simplifies using the Measurement Protocol for Google Analytics 4 to track events in Laravel applications.
License MIT
Homepage https://github.com/LukeTowers/laravel-ga4-event-tracking
Informations about the package laravel-ga4-event-tracking
Laravel Google Analytics 4 Measurement Protocol Event Tracking
Simplifies using the Measurement Protocol for Google Analytics 4 to track events in Laravel applications.
Installation
1) Install package via Composer
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
&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:
4) Include the sendGA4ClientID
directive in your layout file after the Google Analytics Code tracking code.
The client_id
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
This package provides two ways to send events to Google Analytics 4:
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. For example:
The sendEvent()
method will return an array with the status of the request.
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.
There are additional methods that let you customize the call sent to GA4.
broadcastGA4EventAs
With this method you can customize the name of the Event Action. By default, we use the class name with the class's namespace removed. This method gives you access to the underlying GA4
class instance as well.
eventOccurredAt
With this method you can customize the time that the event occurred at. This will be used in the timestamp_micros
parameter sent to the Measurement Protocol. By default, we use the current time. You must return an instance of Carbon\Carbon
in order for it to be used.
withGA4Parameters
With this method you can set the parameters of the event being sent.
Handle framework and 3rd-party events
If you want to handle events where you can't add the ShouldBroadcastToAnalytics
interface, you can manually register them in your EventServiceProvider
using the DispatchAnalyticsJob
listener.
Debugging Mode
You can also enable debugging mode by calling enableDebugging()
method before calling the sendEvent()
method. Like so - GA4::enableDebugging()->sendEvent($eventData)
. The sendEvent()
method will return the response (array) from Google Analytics request in that case.
Testing
or
Security
If you discover any security related issues, please use the Report a vulnerability button instead of using the issue tracker.
Credits:
This package is a fork of the following projects:
- protonemedia/laravel-analytics-event-tracking: Original package, but only supports Universal Analytics.
- daikazu/laravel-ga4-event-tracking: Forked from the original package to support Google Analytics 4 but the package was not maintained and it was not compatible with Laravel 10.
- accexs/laravel-ga4-event-tracking: Forked from
daikazu
's package but was missing some features and was not cleanly forked.
License
This package is open-sourced software licensed under the MIT License.
All versions of laravel-ga4-event-tracking with dependencies
illuminate/bus Version ^8.0 || ^9.0 || ^10.0 || ^11.0
illuminate/queue Version ^8.0 || ^9.0 || ^10.0 || ^11.0
illuminate/http Version ^8.74 || ^9.0 || ^10.0 || ^11.0
illuminate/validation Version ^8.0 || ^9.0 || ^10.0 || ^11.0
guzzlehttp/guzzle Version ^7.5