PHP code example of luketowers / laravel-ga4-event-tracking
1. Go to this page and download the library: Download luketowers/laravel-ga4-event-tracking library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
luketowers / laravel-ga4-event-tracking example snippets
namespace App\Events;
use App\Order;
use LukeTowers\GA4EventTracking\ShouldBroadcastToAnalytics;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class OrderWasCreated implements ShouldBroadcastToAnalytics
{
use Dispatchable, SerializesModels;
public $order;
public function __construct(Order $order)
{
$this->order = $order;
}
}
use Carbon\Carbon;
use LukeTowers\GA4EventTracking\GA4;
use LukeTowers\GA4EventTracking\ShouldBroadcastToAnalytics;
use Illuminate\Queue\SerializesModels;
class OrderSubmitted extends Event implements ShouldBroadcastToAnalytics
{
use SerializesModels;
protected Carbon $submittedAt;
public function __construct(
public Order $order
) {
$this->submittedAt = now();
}
public function eventOccurredAt(): Carbon
{
return $this->submittedAt;
}
}
namespace App\Events;
use App\Order;
use LukeTowers\GA4EventTracking\ShouldBroadcastToAnalytics;
use LukeTowers\GA4EventTracking\GA4;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class OrderWasCreated implements ShouldBroadcastToAnalytics
{
use Dispatchable, SerializesModels;
public $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function withGA4Parameters(GA4 $ga4): array
{
$eventData = [
'transaction_id' => $order->id,
'value' => $order->amount_total,
'currency' => 'USD',
'tax' => $order->amount_tax,
'shipping' => $order->amount_shipping,
'items' => [],
'event_category' => config('app.name'),
'event_label' => 'Order Created',
];
foreach ($order->items as $item) {
$eventData['items'][] = [
'id' => $item->sku ?: 'p-' . $item->product->id,
'name' => $item->title,
'brand' => $item->product->brand->name ?? '',
'category' => $item->product->category->title ?? '',
'quantity' => $item->quantity,
'price' => $item->price,
'variant' => $item->variant->title ?? '',
];
}
return $eventData;
}
public function broadcastGA4EventAs(GA4 $ga4): string
{
return 'purchase';
}
}
namespace App\Providers;
use LukeTowers\GA4EventTracking\Listeners\DispatchAnalyticsJob;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
DispatchAnalyticsJob::class,
],
];
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.