think.studio/laravel-notification-tracker

Track status of notifications sent by application.

1.4.0 2023-09-25 09:19 UTC

This package is auto-updated.

Last update: 2024-10-25 11:42:56 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

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()}",
        ];
    }
}

Credits

  • Think Studio