Created
June 12, 2023 15:45
-
-
Save chris-sev/45202ac1c1ec14ebc53720600bb83cc4 to your computer and use it in GitHub Desktop.
laravel bentonow.com event tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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