PHP code example of multek / laravel-business-metrics
1. Go to this page and download the library: Download multek/laravel-business-metrics 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/ */
multek / laravel-business-metrics example snippets
enum BusinessEventType: string
{
case UserSignedUp = 'user_signed_up';
case RfqCreated = 'rfq_created';
case OrderPaid = 'order_paid';
// ...
}
use Multek\BusinessMetrics\Reports\BusinessReport;
class ActivationRateReport extends BusinessReport
{
public function table(): string
{
return 'analytics.activation_rate';
}
public function schema(): string
{
return <<<'SQL'
CREATE TABLE IF NOT EXISTS analytics.activation_rate (
cohort_week DATE PRIMARY KEY,
signups BIGINT NOT NULL DEFAULT 0,
activated BIGINT NOT NULL DEFAULT 0,
activation_rate NUMERIC(5,2) NOT NULL DEFAULT 0,
updated_at TIMESTAMPTZ DEFAULT NOW()
)
SQL;
}
public function query(): string
{
return <<<'SQL'
INSERT INTO analytics.activation_rate (cohort_week, signups, activated, activation_rate)
SELECT
date_trunc('week', s.occurred_at)::date AS cohort_week,
COUNT(DISTINCT s.actor_user_id) AS signups,
COUNT(DISTINCT a.actor_user_id) AS activated,
ROUND(
COUNT(DISTINCT a.actor_user_id)::numeric
/ NULLIF(COUNT(DISTINCT s.actor_user_id), 0) * 100, 2
) AS activation_rate
FROM public.business_events s
LEFT JOIN public.business_events a
ON a.actor_user_id = s.actor_user_id
AND a.event_name = 'onboarding_completed'
AND a.occurred_at BETWEEN s.occurred_at AND s.occurred_at + INTERVAL '7 days'
WHERE s.event_name = 'user_signed_up'
AND s.occurred_at >= NOW() - INTERVAL '3 weeks'
GROUP BY 1
ON CONFLICT (cohort_week) DO UPDATE SET
signups = EXCLUDED.signups,
activated = EXCLUDED.activated,
activation_rate = EXCLUDED.activation_rate,
updated_at = NOW()
SQL;
}
public function schedule(): string
{
return '0 */6 * * *'; // every 6 hours
}
}
// This happens inside the package — you don't write this code:
$schedule->job(new ProcessReportJob($reportClass))
->cron($report->schedule()) // uses YOUR cron from schedule()
->withoutOverlapping();
use Multek\BusinessMetrics\Traits\HasBusinessEvents;
class Order extends Model
{
use HasBusinessEvents;
protected string $businessEntityType = 'order';
protected function businessEventProperties(): array
{
return [
'value' => $this->total,
'currency' => 'BRL',
];
}
}
// Then anywhere in your code:
$order->emitBusinessEvent('order_created');
$order->emitBusinessEvent('order_paid', ['payment_method' => 'pix']);
// Within a transaction:
$order->emitBusinessEventInTransaction('order_paid');
sql
CREATE USER grafana_ro WITH PASSWORD 'secure_password';
GRANT USAGE ON SCHEMA analytics TO grafana_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA analytics TO grafana_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA analytics GRANT SELECT ON TABLES TO grafana_ro;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.