1. Go to this page and download the library: Download artisanpack-ui/analytics 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/ */
// routes/console.php or bootstrap/app.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('analytics:cleanup')->daily();
Schedule::command('analytics:aggregate')->hourly();
Schedule::command('analytics:warm-cache')->hourly();
use ArtisanPackUI\Analytics\Events\PageViewRecorded;
use ArtisanPackUI\Analytics\Events\EventTracked;
use ArtisanPackUI\Analytics\Events\SessionStarted;
use ArtisanPackUI\Analytics\Events\GoalCompleted;
use ArtisanPackUI\Analytics\Events\ConsentGiven;
use ArtisanPackUI\Analytics\Events\ConsentRevoked;
// Listen for page views
Event::listen(PageViewRecorded::class, function ($event) {
// $event->pageView contains the page view model
});
// Listen for custom events
Event::listen(EventTracked::class, function ($event) {
// $event->event contains the tracked event
});
// Listen for goal completions
Event::listen(GoalCompleted::class, function ($event) {
// $event->goal contains the completed goal
// $event->conversion contains the conversion record
});
use ArtisanPackUI\Analytics\Contracts\AnalyticsProviderInterface;
use ArtisanPackUI\Analytics\Data\PageViewData;
use ArtisanPackUI\Analytics\Data\EventData;
class MixpanelProvider implements AnalyticsProviderInterface
{
public function trackPageView(PageViewData $data): void
{
// Send to Mixpanel
}
public function trackEvent(EventData $data): void
{
// Send to Mixpanel
}
public function isEnabled(): bool
{
return config('services.mixpanel.enabled', false);
}
public function getName(): string
{
return 'mixpanel';
}
}
// Register in a service provider
Analytics::extend('mixpanel', function ($app) {
return new MixpanelProvider(config('services.mixpanel'));
});
use ArtisanPackUI\Analytics\Models\Site;
use ArtisanPackUI\Core\Contracts\SiteResolver;
use Illuminate\Http\Request;
class TeamResolver implements SiteResolver
{
public function __construct(private Request $request)
{
}
public function currentSiteId(): int|string|null
{
$teamId = $this->request->user()?->current_team_id;
// Return before querying. A null $teamId would become
// where team_id is null, which matches unassigned sites.
if ($teamId === null) {
return null;
}
return Site::where('team_id', $teamId)->value('id');
}
}
// Register in config/artisanpack.php
'core' => [
'multi_tenant' => [
'enabled' => true,
'resolvers' => [
App\Analytics\TeamResolver::class,
],
],
],
use function addFilter;
// Modify page view data before recording
addFilter('analytics.pageview.data', function (array $data) {
$data['custom_dimension'] = 'value';
return $data;
});
// Add custom excluded paths
addFilter('analytics.excluded_paths', function (array $paths) {
$paths[] = '/internal/*';
return $paths;
});
// Modify dashboard query
addFilter('analytics.dashboard.query', function ($query) {
return $query->where('is_bot', false);
});
bash
# Install the package
composer config, assets)
php artisan analytics:install
# Or manually publish and migrate
php artisan vendor:publish --provider="ArtisanPackUI\Analytics\AnalyticsServiceProvider"
php artisan migrate