Analytics Event Tracking is a Laravel package by pascalbaljet to send Laravel events to Google Analytics from the server-side easily. This package works by fetching the GA client ID on the client-side (via a blade directive) and sending it to the backend for storage in the user’s session, so later events have access to the client ID when sending info to GA.
It integrates with Laravel’s event system, and you only need to implement the ShouldBroadcastToAnalytics
interface to send your events to GA.
Here’s an example from the package author’s writeup about the package:
namespace AppEvents;
use AppOrder;
use TheIconicTrackingGoogleAnalyticsAnalytics;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
use ProtoneMediaAnalyticsEventTrackingShouldBroadcastToAnalytics;
class OrderWasPaid implements ShouldBroadcastToAnalytics
{
use Dispatchable, SerializesModels;
public $order;
public function __construct(Order $order)
{
$this->order = $order;
}
// optional
public function withAnalytics(Analytics $analytics)
{
$analytics->setEventValue($this->order->sum_in_cents / 100);
}
// optional
public function broadcastAnalyticsActionAs(Analytics $analytics)
{
return 'CustomEventAction';
}
}
The package’s main features include:
Learn More
Check out the author’s post Tracking events with Google Analytics and a new Laravel package for more details about the creation of this package.
You can learn more about this package, get full installation instructions, and view the source code on GitHub at pascalbaljetmedia/laravel-analytics-event-tracking.
Filed in:
News