PHP code example of artisanpack-ui / analytics

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/ */

    

artisanpack-ui / analytics example snippets


// Track page views programmatically
trackPageView('/products', 'Products Page');

// Track custom events
trackEvent('button_click', ['button_id' => 'signup']);

// Get visitor statistics
$stats = analyticsStats(DateRange::last30Days());

'ai' => [
	'features' => [
		'analytics.insight_summary' => [ 'enabled' => true ],
		'analytics.explain_anomaly' => [ 'enabled' => true ],
		'analytics.segment_insight' => [ 'enabled' => true ],
		'analytics.digest_email'    => [ 'enabled' => true ],
	],
],

use ArtisanPackUI\Analytics\Jobs\SendDigestEmailJob;

$schedule->call( function () {
	AnalyticsDigestPreference::where( 'cadence', 'weekly' )->each( function ( $preference ) {
		SendDigestEmailJob::dispatch(
			userId: $preference->user_id,
			email: $preference->user->email,
			items: $weeklyDigestItems( $preference->user ),
			periodLabel: 'Last 7 days',
		);
	} );
} )->weekly();

return [
    // Enable/disable tracking
    'enabled' => env('ANALYTICS_ENABLED', true),

    // Active providers
    'active_providers' => ['local'], // Options: local, google, plausible

    // Local provider settings
    'local' => [
        'connection' => null,
        'table_prefix' => 'analytics_',
        'anonymize_ip' => true,
        'queue_processing' => true,
        'queue_name' => 'analytics',
    ],

    // Privacy settings
    'privacy' => [
        'consent_/     'core' => [
    //         'multi_tenant' => [
    //             'enabled'   => env('ARTISANPACK_MULTI_TENANT_ENABLED', false),
    //             'resolvers' => [
    //                 ArtisanPackUI\Analytics\Resolvers\DomainResolver::class,
    //             ],
    //         ],
    //     ],
    //
    // The keys below are deprecated compatibility settings, still honoured
    // and carried onto the shared configuration at boot.
    'multi_tenant' => [
        'enabled' => false,
    ],

    // Dashboard settings
    'dashboard' => [
        'prefix' => 'analytics',
        'middleware' => ['web', 'auth'],
        'cache_duration' => 300,
    ],
];

// 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
bash
php artisan vendor:publish --tag=analytics-config