Skip to content

Docs ‐ Events

papamarfo edited this page Jul 30, 2026 · 1 revision

Introduction to Events

Ussd dispatches two events you can listen for without touching any core classes: Speso\Ussd\Events\StateEntered, fired every time a state is resolved and rendered (including the very first request of a session), and Speso\Ussd\Events\SessionTerminated, fired once when a session ends — whether that's a normal Terminated state or an unhandled exception. Both carry the current Context, so you have the session's uid(), gid() and input() to hand; StateEntered also carries the resolved state's class name.

Listening for Session Events

<?php

namespace App\Listeners;

use Illuminate\Support\Facades\Log;
use Speso\Ussd\Events\StateEntered;

class LogUssdNavigation
{
    public function handle(StateEntered $event): void
    {
        Log::info("Ussd session {$event->context->uid()} entered {$event->state}");
    }
}

Register listeners the usual Laravel way — via Event::listen() in a service provider, or an auto-discovered listener class.

Use Cases

  • Logging a session's navigation path for debugging.
  • Tracking drop-off: which states users most often abandon a session at, using SessionTerminated alongside the last StateEntered for that session.
  • Feeding an analytics pipeline without coupling your State classes to any specific analytics tool.

Clone this wiki locally