Laravel Package for Server-Side GA Event Tracking
Published on by Paul Redmond
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:
- Use Laravel Events to track events with GA.
- Blade Directive to easily store the Client ID.
- Full access to the underlying PHP GA measurement protocol library.
- API calls to GA are queued.
- Easy to configure.
- Compatible with Laravel 6.0 and 7.0.
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.