PHP code example of jeffersongoncalves / laravel-posthog

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

    

jeffersongoncalves / laravel-posthog example snippets


// config/posthog.php
return [
    'host' => env('POSTHOG_HOST', 'https://app.posthog.com'),
    'project_api_key' => env('POSTHOG_PROJECT_API_KEY', ''),
    'personal_api_key' => env('POSTHOG_PERSONAL_API_KEY', ''),
    'project_id' => env('POSTHOG_PROJECT_ID', ''),
    'timeout' => env('POSTHOG_TIMEOUT', 10),
];

use JeffersonGoncalves\PostHog\Facades\PostHog;

PostHog::capture('signup_completed', 'user_123', [
    'plan' => 'pro',
    '$current_url' => 'https://example.com/signup',
]);

PostHog::batch([
    ['event' => 'pageview', 'distinct_id' => 'user_1'],
    ['event' => 'signup', 'distinct_id' => 'user_2'],
]);

PostHog::identify('user_123', [
    'email' => '[email protected]',
    'plan' => 'pro',
]);

// Merge an anonymous session into an identified person
PostHog::alias('user_123', 'anon_abc');

// Every flag evaluated for this person
$flags = PostHog::featureFlags('user_123');

// A single flag: false when off, true when on, the variant key when multivariate
$variant = PostHog::featureFlag('new-pricing', 'user_123');

// Simple boolean check (a variant counts as enabled)
if (PostHog::isFeatureEnabled('new-pricing', 'user_123')) {
    // Show new pricing
}

PostHog::isFeatureEnabled('beta-dashboard', 'user_123', ['company' => 'acme-inc']);

$persons = PostHog::persons('user_123');

$result = PostHog::query(
    'SELECT event, count() FROM events WHERE timestamp > now() - interval 7 day GROUP BY event ORDER BY count() DESC LIMIT 10'
);

$insights = PostHog::insights();

$recordings = PostHog::sessionRecordings(['limit' => 20]);

use JeffersonGoncalves\PostHog\Exceptions\PostHogException;

try {
    PostHog::persons('user_123');
} catch (PostHogException $e) {
    logger()->error($e->getMessage(), $e->errorBody());
}
bash
php artisan vendor:publish --tag=posthog-config