PHP code example of vipertecpro / app-analytics

1. Go to this page and download the library: Download vipertecpro/app-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/ */

    

vipertecpro / app-analytics example snippets


use Vipertecpro\AppAnalytics\Facades\Analytics;
use Vipertecpro\AppAnalytics\Facades\RemoteConfig;

Analytics::logScreenView('Checkout');
Analytics::logEvent('purchase_started', ['plan' => 'pro', 'price' => 9.99, 'trial' => true]);
Analytics::setUserProperty('plan', 'pro');
Analytics::setUserId($user->id);          // never an email or phone number

if (RemoteConfig::bool('new_checkout')) {
    // the new flow
}

$title = RemoteConfig::string('welcome_title');
$limit = RemoteConfig::int('max_items', 20);

use Native\Mobile\Attributes\On;
use Vipertecpro\AppAnalytics\Events\RemoteConfigFetched;
use Vipertecpro\AppAnalytics\Events\RemoteConfigFetchFailed;

public function mount(): void
{
    RemoteConfig::fetchAndActivate();             // asynchronous
}

#[On(RemoteConfigFetched::class)]
public function onFlags(bool $activated): void
{
    // RemoteConfig already reads the new values; re-render whatever depends on them
}

#[On(RemoteConfigFetchFailed::class)]
public function onFlagsFailed(string $reason): void
{
    // not_configured | throttled | network | error — the old values and defaults keep working
}

Event::listen(ConsentUpdated::class, function (ConsentUpdated $event) {
    Analytics::setConsent(
        analytics: $event->choices['analytics'] === true,
        ads: $event->choices['ads'] === true,
    );
});

'remote_config' => [
    'defaults' => [
        'new_checkout' => false,
        'welcome_title' => 'Welcome!',
        'max_items' => 20,
    ],
    'minimum_fetch_interval' => 3600,   // seconds; 0 while testing, 12 hours is a good production value
],

Analytics::logEvent(string $name, array $parameters = []): void;
Analytics::logScreenView(string $screenName, ?string $screenClass = null): void;
Analytics::setUserProperty(string $name, ?string $value): void;
Analytics::setUserId(?string $id): void;
Analytics::setCollectionEnabled(bool $enabled): void;
Analytics::setConsent(bool $analytics, bool $ads, ?bool $adPersonalization = null): void;
Analytics::resetData(): void;
Analytics::status(): array;      // configured, collectionEnabled, platform, appInstanceId, projectId, reason
Analytics::isConfigured(): bool;

RemoteConfig::fetchAndActivate(?int $minimumFetchInterval = null): void;
RemoteConfig::string(string $key, ?string $default = null): ?string;
RemoteConfig::bool(string $key, bool $default = false): bool;
RemoteConfig::int(string $key, int $default = 0): int;
RemoteConfig::float(string $key, float $default = 0.0): float;
RemoteConfig::json(string $key, array $default = []): array;
RemoteConfig::source(string $key): string;      // remote | default | static
RemoteConfig::all(): array;                      // key => [value, source]
RemoteConfig::setDefaults(array $defaults): void;
RemoteConfig::refresh(): void;