Skip to content

Instantly share code, notes, and snippets.

@chris-sev
Created June 12, 2023 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-sev/45202ac1c1ec14ebc53720600bb83cc4 to your computer and use it in GitHub Desktop.
Save chris-sev/45202ac1c1ec14ebc53720600bb83cc4 to your computer and use it in GitHub Desktop.
laravel bentonow.com event tracking
<?php
namespace App\Actions\Bento;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Lorisleiva\Actions\Concerns\AsAction;
class TrackBentoEvent
{
use AsAction;
public function handle($user = null, $name, $details = null)
{
if (app()->isLocal()) return;
if (!$user) $user = auth()->user() ?? []; // We have un-authed demos so dont bail if we dont have a user
$singleEvent = [
'email' => $user->email ?? '',
'type' => $name,
];
if ($details) {
$singleEvent['details'] = $details;
}
// make the call to bento
try {
Http::withBasicAuth(config('services.bento.publishable_key'), config('services.bento.secret_key'))
->post('https://app.bentonow.com/api/v1/batch/events', [
'site_uuid' => 'YOUR_SITE_UUID_HERE',
'events' => [$singleEvent]
]);
} catch (Exception $e) {
Log::error('Error tracking Bento event', ['message' => $e->getMessage()]);
throw new Exception($e->getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment