Laravel Package for Server-Side GA Event Tracking

News

May 15th, 2020

laravel-ga-events.jpg

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 App\Events;

use App\Order;
use TheIconic\Tracking\GoogleAnalytics\Analytics;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use ProtoneMedia\AnalyticsEventTracking\ShouldBroadcastToAnalytics;

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:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.