PHP code example of nugsoft / retention-extractor

1. Go to this page and download the library: Download nugsoft/retention-extractor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nugsoft / retention-extractor example snippets


'clients' => [
    'model' => \App\Models\Business::class,
    'external_id' => 'id',            // must never change for a given business
    'name' => 'business_name',
    'contact_phone' => 'phone',
    'contact_email' => 'email',
    'scope' => fn ($query) => $query->where('is_active', true),   // optional
    'ended' => 'deleted_at',                                      // optional
],

'metrics' => [
    'login_count_7d'  => ['table' => 'sessions', 'count' => '*', 'via' => 'business_id', 'date' => 'created_at'],
    'transactions_7d' => ['table' => 'sales', 'count' => '*', 'via' => 'business_id', 'date' => 'created_at'],
    'transaction_value_7d' => ['table' => 'sales', 'sum' => 'total', 'via' => 'business_id', 'date' => 'created_at'],
],

'items_sold_7d' => [
    'table' => 'sale_items',
    'sum' => 'quantity',
    'via' => ['sale_id' => ['sales', 'id', 'business_id']],
    'date' => 'created_at',
],

'last_activity' => ['table' => 'sales', 'via' => 'business_id', 'date' => 'created_at'],

'subscription' => [
    'table' => 'subscriptions',
    'via' => 'business_id',
    'start' => 'starts_at',
    'end' => 'ends_at',
    'status' => 'status',
    'status_map' => ['paid' => 'active', 'lapsed' => 'expired', 'void' => 'cancelled'],
],

'subscription' => [
    'table' => 'licences',
    'via' => 'business_id',
    'branch' => 'branch_id',
    'start' => 'start_date',
    'end' => 'end_date',
],

'via' => 'school_branch_id',

'where' => ['deleted_at' => null],

'via' => ['branch_id' => ['branches', 'id', 'client_id', ['deleted_at' => null]]],

'via'       => 'school_branch_id',    // each branch is a client
'pick'      => 'current',             // started and ends last, else the next to begin
'grace'     => 'grace_ends_at',       // free access after the end
'deadline'  => 'license_expires_at',  // payment deadline, caps end and grace
'cancelled' => 'revoked_at',          // set once the term was terminated
'where'     => ['deleted_at' => null, 'revoked_at' => null],

'licence' => [
    'table'  => 'facilities',
    'via'    => 'id',
    'status' => ['column' => 'status', 'granted' => 'Active', 'revoked' => 'Suspend'],
],

'licence' => [
    'table'   => 'branch_subscriptions',
    'via'     => 'school_branch_id',
    'ceiling' => ['column' => 'license_expires_at'],
],

Event::listen(LicenceApplied::class, function (LicenceApplied $event): void {
    // The client is the branch there, so its external id is the branch id.
    BranchSubscription::forgetCachesForBranch((int) $event->licence->externalId);
});

'licence' => [
    'driver' => \App\Retention\PosCreamLicence::class,
],

'where' => ['deleted_at' => null],

'blocks'        => ['suspended_at'],               // set on any row: they may not work
'term_ends'     => ['end_date', 'grace_ends_at'],  // rows past these are not asked
'term_starts'   => ['start_date'],                 // rows not yet begun are not asked
'client_blocks' => [                               // set on the client's own row: they may not work
    'table' => 'school_branches', 'key' => 'id', 'columns' => ['suspended_at'],
],

'login_count_7d' => [
    'table' => 'sessions',
    'distinct' => 'user_id',                                  // people, not rows
    'via' => ['user_id' => ['users', 'id', 'business_id']],   // no tenant column of its own
    'date' => 'last_activity',
    'date_format' => 'timestamp',                             // a unix integer, not a datetime
],

'login_count_7d' => [
    'table' => 'audit_trail',
    'distinct' => 'user_id',
    'via' => 'business_id',
    'date' => 'created_at',
    'where' => ['action' => ['login', 'signed_in']],
],

'login_count_7d' => [
    'table' => 'user_auth_logs',
    'distinct' => 'user_id',
    'via' => ['user_id' => ['users', 'id', 'facility_id', ['is_support' => false]]],
    'date' => 'login_at',
    'where' => ['deleted_at' => null],
],
bash
php artisan retention:push --dry-run
bash
php artisan retention:status