think.studio / laravel-notification-tracker
Track status of notifications sent by application.
1.4.0
2023-09-25 09:19 UTC
Requires
- php: ^8.1
- illuminate/support: ^9.0|^10.0
- think.studio/laravel-json-field-cast: ^2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.20
- orchestra/testbench: ^8.5
- phpunit/phpunit: ^10.2
- psalm/plugin-laravel: ^2.8
- vimeo/psalm: ^5.13
README
Track status of notifications sent by application.
Installation
Install the package via composer:
composer require think.studio/laravel-notification-tracker
You can publish the config file with:
php artisan vendor:publish --provider="NotificationTracker\ServiceProvider" --tag="config"
Configuration
public function register() { // cancel default migrations files \NotificationTracker\NotificationTracker::ignoreMigrations(); // cancel default web routes implementation \NotificationTracker\NotificationTracker::ignoreRoutes(); // change class names what stored in database \NotificationTracker\NotificationTracker::classMap([ 'registration_confirmation' => \App\Notifications\RegistrationNotification::class, ]); }
Usage
For your notification please implement Interface Trackable
, use trait HasTracker
.
use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; use NotificationTracker\Notification\HasTracker; use NotificationTracker\Notification\Trackable; class CertifiedNotification extends Notification implements ShouldQueue, Trackable { use Queueable, HasTracker; public Document $document; public function __construct(Document $document) { $this->document = $document; } public function via($notifiable = null) { return ['mail', 'custom']; } public function toMail($notifiable) { $message = (new MailMessage)->subject('Certificate created'); $message->line('Thank you!'); // Initialise tracker return $this->tracker() // You can add metadata to channel row. Using callback, or passing key->value ->trackerMeta(fn(\JsonFieldCast\Json\AbstractMeta $meta, $trackedChannel) => $meta->toMorph('document', $this->document)) ->trackerMeta('document_category', $this->document->category?->name) // Save tracked data ->trackMailMessage($message, $notifiable); } public function toCustom($notifiable) { /** @var \NotificationTracker\Models\TrackedChannel $trackedChannel */ $trackedChannel = $this->tracker()->track('custom', $notifiable); return [ 'subject' => 'Foo', 'body' => "Foo {$trackedChannel->getClickTrackerUrl('https://test.com')} {$trackedChannel->getPixelImageHtml()}", ]; } }