### Install laravel-posthog via Composer Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Install the package via Composer. ```bash composer require jeffersongoncalves/laravel-posthog ``` -------------------------------- ### Default config/posthog.php Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Default configuration file for the package. It reads environment variables with sensible defaults, including a 10-second timeout. ```php // 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), ]; ``` -------------------------------- ### Publish posthog-config Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Publish the configuration file to your Laravel project. ```bash php artisan vendor:publish --tag=posthog-config ``` -------------------------------- ### Fetch insights and session recordings Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Fetch insights and session recordings. insights returns all insights, while sessionRecordings accepts an optional array of parameters such as 'limit'. ```php $insights = PostHog::insights(); $recordings = PostHog::sessionRecordings(['limit' => 20]); ``` -------------------------------- ### Capture an event with PostHog facade Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Use the PostHog facade to capture a custom event with a distinct user ID and optional properties. The event name and properties are passed as arguments. ```php use JeffersonGoncalves\PostHog\Facades\PostHog; PostHog::capture('signup_completed', 'user_123', [ 'plan' => 'pro', '$current_url' => 'https://example.com/signup', ]); ``` -------------------------------- ### Run tests with composer test Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Runs the test suite for the package using Composer's test script. ```bash composer test ``` -------------------------------- ### Configure PostHog credentials in .env Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Set your PostHog credentials in your .env file. POSTHOG_PROJECT_API_KEY is the public write-only key needed for capture, batch, and feature flags. POSTHOG_PERSONAL_API_KEY and POSTHOG_PROJECT_ID are only required for read endpoints (persons, HogQL, insights, session recordings). ```env POSTHOG_HOST=https://us.i.posthog.com POSTHOG_PROJECT_API_KEY=phc_your_project_api_key POSTHOG_PERSONAL_API_KEY=phx_your_personal_api_key POSTHOG_PROJECT_ID=12345 ``` -------------------------------- ### Run HogQL query Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Run a HogQL query against PostHog events. The query string is passed directly to the query method. ```php $result = PostHog::query( 'SELECT event, count() FROM events WHERE timestamp > now() - interval 7 day GROUP BY event ORDER BY count() DESC LIMIT 10' ); ``` -------------------------------- ### Batch multiple events with PostHog Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Batch multiple events in a single call to reduce API requests. Each event is an associative array with 'event' and 'distinct_id' keys. ```php PostHog::batch([ ['event' => 'pageview', 'distinct_id' => 'user_1'], ['event' => 'signup', 'distinct_id' => 'user_2'], ]); ``` -------------------------------- ### Identify a user and alias anonymous session Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Identify a user with properties and merge an anonymous session into the identified person using the alias method. The alias call links the anonymous ID to the identified user. ```php PostHog::identify('user_123', [ 'email' => 'user@example.com', 'plan' => 'pro', ]); // Merge an anonymous session into an identified person PostHog::alias('user_123', 'anon_abc'); ``` -------------------------------- ### Evaluate feature flags with PostHog Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Evaluate feature flags for a user. featureFlags returns all flags, featureFlag returns a single flag's value (false, true, or variant key), and isFeatureEnabled returns a boolean where a variant counts as enabled. ```php // 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 } ``` -------------------------------- ### Fetch persons data Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Retrieve person data for a given distinct ID using the persons method. ```php $persons = PostHog::persons('user_123'); ``` -------------------------------- ### Handle PostHog API exceptions Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Handle API errors by catching PostHogException. Any non-2xx response throws this exception, which exposes the decoded error body via errorBody(). ```php use JeffersonGoncalves\PostHog\Exceptions\PostHogException; try { PostHog::persons('user_123'); } catch (PostHogException $e) { logger()->error($e->getMessage(), $e->errorBody()); } ``` -------------------------------- ### Check group-based feature flag Source: https://github.com/jeffersongoncalves/laravel-posthog/blob/main/README.md Check a feature flag with group-based targeting by passing a groups map as the third argument. The groups map associates group type keys with group IDs. ```php PostHog::isFeatureEnabled('beta-dashboard', 'user_123', ['company' => 'acme-inc']); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.