diff --git a/apps/laravel/laravel12-saas/.claude/skills/laravel/SKILL.md b/apps/laravel/laravel12-saas/.claude/skills/laravel/SKILL.md new file mode 100644 index 00000000..0f94f3ee --- /dev/null +++ b/apps/laravel/laravel12-saas/.claude/skills/laravel/SKILL.md @@ -0,0 +1,57 @@ +--- +name: laravel +description: PostHog integration for Laravel applications +metadata: + author: PostHog + version: dev +--- + +# PostHog integration for Laravel + +This skill helps you add PostHog analytics to Laravel applications. + +## Workflow + +Follow these steps in order to complete the integration: + +1. `basic-integration-1.0-begin.md` - PostHog Setup - Begin ← **Start here** +2. `basic-integration-1.1-edit.md` - PostHog Setup - Edit +3. `basic-integration-1.2-revise.md` - PostHog Setup - Revise +4. `basic-integration-1.3-conclude.md` - PostHog Setup - Conclusion + +## Reference files + +- `EXAMPLE.md` - Laravel example project code +- `laravel.md` - Laravel - docs +- `identify-users.md` - Identify users - docs +- `basic-integration-1.0-begin.md` - PostHog setup - begin +- `basic-integration-1.1-edit.md` - PostHog setup - edit +- `basic-integration-1.2-revise.md` - PostHog setup - revise +- `basic-integration-1.3-conclude.md` - PostHog setup - conclusion + +The example project shows the target implementation pattern. Consult the documentation for API details. + +## Key principles + +- **Environment variables**: Always use environment variables for PostHog keys. Never hardcode them. +- **Minimal changes**: Add PostHog code alongside existing integrations. Don't replace or restructure existing code. +- **Match the example**: Your implementation should follow the example project's patterns as closely as possible. + +## Framework guidelines + +- Create a dedicated PostHogService class in app/Services/ - do NOT scatter PostHog::capture calls throughout controllers +- Register PostHog configuration in config/posthog.php using env() for all settings (api_key, host, disabled) +- Do NOT use Laravel's event system or observers for analytics - call capture explicitly where actions occur +- Remember that source code is available in the vendor directory after composer install +- posthog/posthog-php is the PHP SDK package name +- Check composer.json for existing dependencies and autoload configuration before adding new files +- The PHP SDK uses static methods (PostHog::capture, PostHog::identify) - initialize once with PostHog::init() +- PHP SDK methods take associative arrays with 'distinctId', 'event', 'properties' keys - not positional arguments + +## Identifying users + +Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` headers to maintain correlation. + +## Error tracking + +Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries. diff --git a/apps/laravel/laravel12-saas/.claude/skills/laravel/references/EXAMPLE.md b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/EXAMPLE.md new file mode 100644 index 00000000..d85c6537 --- /dev/null +++ b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/EXAMPLE.md @@ -0,0 +1,2272 @@ +# PostHog Laravel Example Project + +Repository: https://github.com/PostHog/examples +Path: basics/laravel + +--- + +## README.md + +# PostHog Laravel Example + +A Laravel application demonstrating PostHog integration for analytics, feature flags, and error tracking using Livewire for reactive UI components. + +## Features + +- User registration and authentication with Livewire +- SQLite database persistence with Eloquent ORM +- User identification and property tracking +- Custom event tracking (burrito consideration tracker) +- Page view tracking (dashboard, profile) +- Feature flags with payload support +- Error tracking with manual exception capture +- Reactive UI components with Livewire + +## Tech Stack + +- **Framework**: Laravel 11.x +- **Reactive Components**: Livewire 3.x +- **Database**: SQLite +- **Analytics**: PostHog PHP SDK + +## Quick Start + +**Note**: This is a minimal implementation demonstrating PostHog integration. For a production application, you would need to install Laravel via Composer and set up additional dependencies. + +### Manual Setup (Demonstration) + +1. Install dependencies: + ```bash + composer install + ``` + +2. Set up environment: + ```bash + cp .env.example .env + # Edit .env with your PostHog API key + ``` + +3. Configure PostHog in `.env`: + ```env + POSTHOG_API_KEY=your_posthog_api_key + POSTHOG_HOST=https://us.i.posthog.com + POSTHOG_DISABLED=false + ``` + +4. Generate application key: + ```bash + php artisan key:generate + ``` + +5. Create database and run migrations: + ```bash + touch database/database.sqlite + php artisan migrate --seed + ``` + +6. Start the development server: + ```bash + php artisan serve + ``` + +7. Open http://localhost:8000 and either: + - Login with default credentials: `admin@example.com` / `admin` + - Or click "Sign up here" to create a new account + +## PostHog Service + +The `PostHogService` class (`app/Services/PostHogService.php`) wraps the PostHog PHP SDK and provides: + +| Method | Description | +|--------|-------------| +| `identify($distinctId, $properties)` | Identify a user with properties | +| `capture($distinctId, $event, $properties)` | Capture custom events | +| `captureException($exception, $distinctId)` | Capture exceptions with stack traces | +| `isFeatureEnabled($key, $distinctId, $properties)` | Check feature flag status | +| `getFeatureFlagPayload($key, $distinctId)` | Get feature flag payload | + +All methods check `config('posthog.disabled')` and return early if PostHog is disabled. + +## PostHog Integration Points + +### User Registration (`app/Http/Livewire/Auth/Register.php`) +New users are identified and tracked on signup: +```php +$posthog->identify($user->email, $user->getPostHogProperties()); +$posthog->capture($user->email, 'user_signed_up', [ + 'signup_method' => 'form', +]); +``` + +### User Login (`app/Http/Livewire/Auth/Login.php`) +Users are identified on login with their properties: +```php +$posthog->identify($user->email, $user->getPostHogProperties()); +$posthog->capture($user->email, 'user_logged_in', [ + 'login_method' => 'password', +]); +``` + +### User Logout (`routes/web.php`) +Logout events are tracked: +```php +$posthog->capture($user->email, 'user_logged_out'); +``` + +### Page View Tracking +Dashboard and profile views are tracked (`app/Http/Livewire/Dashboard.php`, `app/Http/Livewire/Profile.php`): +```php +$posthog->capture($user->email, 'dashboard_viewed', [ + 'is_staff' => $user->is_staff, +]); + +$posthog->capture($user->email, 'profile_viewed'); +``` + +### Custom Event Tracking (`app/Http/Livewire/BurritoTracker.php`) +The burrito tracker demonstrates custom event capture: +```php +$posthog->identify($user->email, $user->getPostHogProperties()); +$posthog->capture($user->email, 'burrito_considered', [ + 'total_considerations' => $this->burritoCount, +]); +``` + +### Feature Flags (`app/Http/Livewire/Dashboard.php`) +The dashboard demonstrates feature flag checking: +```php +$this->showNewFeature = $posthog->isFeatureEnabled( + 'new-dashboard-feature', + $user->email, + $user->getPostHogProperties() +) ?? false; + +$this->featureConfig = $posthog->getFeatureFlagPayload( + 'new-dashboard-feature', + $user->email +); +``` + +### Error Tracking +Manual exception capture is demonstrated in multiple places: + +**Livewire Components** (`app/Http/Livewire/Dashboard.php`, `app/Http/Livewire/Profile.php`): +```php +try { + throw new \Exception('This is a test error for PostHog tracking'); +} catch (\Exception $e) { + $errorId = $posthog->captureException($e, $user->email); + $this->successMessage = "Error captured in PostHog! Error ID: {$errorId}"; +} +``` + +**API Endpoint** (`app/Http/Controllers/Api/ErrorTestController.php`): +```php +try { + throw new \Exception('Test exception from critical operation'); +} catch (\Throwable $e) { + if ($shouldCapture) { + $posthog->identify($user->email, $user->getPostHogProperties()); + $eventId = $posthog->captureException($e, $user->email); + + return response()->json([ + 'error' => 'Operation failed', + 'error_id' => $eventId, + 'message' => "Error captured in PostHog. Reference ID: {$eventId}", + ], 500); + } +} +``` + +The `/api/test-error` endpoint demonstrates manual exception capture. Use `?capture=true` to capture in PostHog, or `?capture=false` to skip tracking. + + +## Pages + +| Route | Component | PostHog Events | +|-------|-----------|----------------| +| `/` | Login | `user_logged_in` | +| `/register` | Register | `user_signed_up` | +| `/dashboard` | Dashboard | `dashboard_viewed`, feature flag checks | +| `/burrito` | BurritoTracker | `burrito_considered` | +| `/profile` | Profile | `profile_viewed` | +| `/logout` | (route) | `user_logged_out` | + +## Project Structure + +``` +basics/laravel/ +├── app/ +│ ├── Http/ +│ │ ├── Controllers/ +│ │ │ └── Api/ +│ │ │ ├── BurritoController.php # Burrito API endpoint +│ │ │ └── ErrorTestController.php # Error testing endpoint +│ │ └── Livewire/ +│ │ ├── Auth/ +│ │ │ ├── Login.php # Login component +│ │ │ └── Register.php # Registration component +│ │ ├── BurritoTracker.php # Burrito tracker component +│ │ ├── Dashboard.php # Dashboard with feature flags +│ │ └── Profile.php # User profile component +│ ├── Models/ +│ │ └── User.php # User model with PostHog properties +│ └── Services/ +│ └── PostHogService.php # PostHog wrapper service +├── database/ +│ ├── migrations/ # Database migrations +│ └── seeders/ +│ └── DatabaseSeeder.php # Seeds admin user +├── resources/ +│ └── views/ +│ ├── components/ +│ │ └── layouts/ +│ │ ├── app.blade.php # Authenticated layout +│ │ └── guest.blade.php # Guest layout +│ ├── errors/ +│ │ ├── 404.blade.php # Not found page +│ │ └── 500.blade.php # Server error page +│ └── livewire/ +│ ├── auth/ +│ │ ├── login.blade.php # Login form +│ │ └── register.blade.php # Registration form +│ ├── burrito-tracker.blade.php # Burrito tracker UI +│ ├── dashboard.blade.php # Dashboard UI +│ └── profile.blade.php # Profile UI +├── routes/ +│ ├── web.php # Web routes (auth, pages) +│ └── api.php # API routes +└── config/ + └── posthog.php # PostHog configuration +``` + +## Development Commands + +```bash +# Start development server +php artisan serve + +# Run migrations +php artisan migrate + +# Seed database +php artisan migrate:fresh --seed + +# Clear caches +php artisan optimize:clear +``` +--- + +## .env.example + +```example +APP_NAME="PostHog Laravel Example" +APP_ENV=local +APP_KEY= +APP_DEBUG=true +APP_URL=http://localhost:8000 + +DB_CONNECTION=sqlite +# DB_DATABASE will use default database/database.sqlite + +CACHE_DRIVER=file +CACHE_STORE=file + +SESSION_DRIVER=file +SESSION_LIFETIME=120 + +POSTHOG_API_KEY=your_posthog_api_key_here +POSTHOG_HOST=https://us.i.posthog.com +POSTHOG_DISABLED=false + +``` + +--- + +## app/Http/Controllers/Api/BurritoController.php + +```php + $burritoCount]); + + // PostHog: Track event + $posthog->identify($user->email, $user->getPostHogProperties()); + $posthog->capture($user->email, 'burrito_considered', [ + 'total_considerations' => $burritoCount, + ]); + + return response()->json([ + 'success' => true, + 'count' => $burritoCount, + ]); + } +} + +``` + +--- + +## app/Http/Controllers/Api/ErrorTestController.php + +```php +query('capture', 'true') === 'true'; + $user = Auth::user(); + + try { + throw new \Exception('Test exception from critical operation'); + } catch (\Throwable $e) { + if ($shouldCapture) { + // Capture in PostHog + $posthog->identify($user->email, $user->getPostHogProperties()); + $eventId = $posthog->captureException($e, $user->email); + + return response()->json([ + 'error' => 'Operation failed', + 'error_id' => $eventId, + 'message' => "Error captured in PostHog. Reference ID: {$eventId}", + ], 500); + } + + return response()->json([ + 'error' => $e->getMessage(), + ], 500); + } + } +} + +``` + +--- + +## app/Http/Controllers/Controller.php + +```php + 'required|email', + 'password' => 'required', + ]; + + public function login(PostHogService $posthog) + { + $this->validate(); + + if (Auth::attempt(['email' => $this->email, 'password' => $this->password], $this->remember)) { + $user = Auth::user(); + + // PostHog: Identify and track login + $posthog->identify($user->email, $user->getPostHogProperties()); + $posthog->capture($user->email, 'user_logged_in', [ + 'login_method' => 'password', + ]); + + session()->regenerate(); + + return redirect()->intended(route('dashboard')); + } + + $this->addError('email', 'Invalid credentials'); + } + + public function render() + { + return view('livewire.auth.login') + ->layout('components.layouts.guest'); + } +} + +``` + +--- + +## app/Http/Livewire/Auth/Register.php + +```php + 'required|email|unique:users,email', + 'password' => 'required|min:6|confirmed', + ]; + + public function register(PostHogService $posthog) + { + $validated = $this->validate(); + + $user = User::create([ + 'email' => $validated['email'], + 'password' => bcrypt($validated['password']), + 'is_staff' => false, + ]); + + // PostHog: Identify new user and track signup + $posthog->identify($user->email, $user->getPostHogProperties()); + $posthog->capture($user->email, 'user_signed_up', [ + 'signup_method' => 'form', + ]); + + Auth::login($user); + + session()->flash('success', 'Account created successfully!'); + + return redirect()->route('dashboard'); + } + + public function render() + { + return view('livewire.auth.register') + ->layout('components.layouts.guest'); + } +} + +``` + +--- + +## app/Http/Livewire/BurritoTracker.php + +```php +burritoCount = session('burrito_count', 0); + } + + public function considerBurrito(PostHogService $posthog) + { + $this->burritoCount++; + session(['burrito_count' => $this->burritoCount]); + + // PostHog: Track burrito consideration + $user = Auth::user(); + $posthog->identify($user->email, $user->getPostHogProperties()); + $posthog->capture($user->email, 'burrito_considered', [ + 'total_considerations' => $this->burritoCount, + ]); + + $this->dispatch('burrito-considered'); + } + + public function render() + { + return view('livewire.burrito-tracker') + ->layout('components.layouts.app'); + } +} + +``` + +--- + +## app/Http/Livewire/Dashboard.php + +```php +capture($user->email, 'dashboard_viewed', [ + 'is_staff' => $user->is_staff, + ]); + + // Check feature flag + $this->showNewFeature = $posthog->isFeatureEnabled( + 'new-dashboard-feature', + $user->email, + $user->getPostHogProperties() + ) ?? false; + + // Get feature flag payload + $this->featureConfig = $posthog->getFeatureFlagPayload( + 'new-dashboard-feature', + $user->email + ); + } + + public function testErrorWithCapture(PostHogService $posthog) + { + $user = Auth::user(); + + try { + // Simulate an error + throw new \Exception('This is a test error for PostHog tracking'); + } catch (\Exception $e) { + // Capture the exception in PostHog + $errorId = $posthog->captureException($e, $user->email); + + $this->successMessage = "Error captured in PostHog! Error ID: {$errorId}"; + $this->errorMessage = null; + } + } + + public function testErrorWithoutCapture() + { + try { + // Simulate an error without capturing + throw new \Exception('This error was NOT sent to PostHog'); + } catch (\Exception $e) { + $this->errorMessage = "Error occurred but NOT captured in PostHog: " . $e->getMessage(); + $this->successMessage = null; + } + } + + public function render() + { + return view('livewire.dashboard') + ->layout('components.layouts.app'); + } +} + +``` + +--- + +## app/Http/Livewire/Profile.php + +```php +capture($user->email, 'profile_viewed'); + } + + public function testErrorWithCapture(PostHogService $posthog) + { + $user = Auth::user(); + + try { + // Simulate an error + throw new \Exception('This is a test error for PostHog tracking'); + } catch (\Exception $e) { + // Capture the exception in PostHog + $errorId = $posthog->captureException($e, $user->email); + + $this->successMessage = "Error captured in PostHog! Error ID: {$errorId}"; + $this->errorMessage = null; + } + } + + public function testErrorWithoutCapture() + { + try { + // Simulate an error without capturing + throw new \Exception('This error was NOT sent to PostHog'); + } catch (\Exception $e) { + $this->errorMessage = "Error occurred but NOT captured in PostHog: " . $e->getMessage(); + $this->successMessage = null; + } + } + + public function render() + { + return view('livewire.profile') + ->layout('components.layouts.app'); + } +} + +``` + +--- + +## app/Models/User.php + +```php + 'hashed', + 'is_staff' => 'boolean', + ]; + } + + /** + * Get PostHog person properties for this user. + */ + public function getPostHogProperties(): array + { + return [ + 'email' => $this->email, + 'is_staff' => $this->is_staff, + 'date_joined' => $this->created_at->toISOString(), + ]; + } +} + +``` + +--- + +## app/Services/PostHogService.php + +```php + config('posthog.host'), + 'debug' => config('posthog.debug'), + ] + ); + self::$initialized = true; + } + } + + public function identify(string $distinctId, array $properties = []): void + { + if (config('posthog.disabled')) { + return; + } + + PostHog::identify([ + 'distinctId' => $distinctId, + 'properties' => $properties, + ]); + } + + public function capture(string $distinctId, string $event, array $properties = []): void + { + if (config('posthog.disabled')) { + return; + } + + PostHog::capture([ + 'distinctId' => $distinctId, + 'event' => $event, + 'properties' => $properties, + ]); + } + + public function captureException(\Throwable $exception, ?string $distinctId = null): ?string + { + if (config('posthog.disabled')) { + return null; + } + + $distinctId = $distinctId ?? Auth::user()?->email ?? 'anonymous'; + + $eventId = uniqid('error_', true); + + $this->capture($distinctId, '$exception', [ + 'error_id' => $eventId, + 'exception_type' => get_class($exception), + 'exception_message' => $exception->getMessage(), + 'exception_file' => $exception->getFile(), + 'exception_line' => $exception->getLine(), + 'stack_trace' => $exception->getTraceAsString(), + ]); + + return $eventId; + } + + public function isFeatureEnabled(string $key, string $distinctId, array $properties = []): ?bool + { + if (config('posthog.disabled')) { + return false; + } + + return PostHog::isFeatureEnabled($key, $distinctId, $properties); + } + + public function getFeatureFlagPayload(string $key, string $distinctId) + { + if (config('posthog.disabled')) { + return null; + } + + return PostHog::getFeatureFlagPayload($key, $distinctId); + } +} + +``` + +--- + +## artisan + +``` +#!/usr/bin/env php +make(Illuminate\Contracts\Console\Kernel::class); + +$status = $kernel->handle( + $input = new Symfony\Component\Console\Input\ArgvInput, + new Symfony\Component\Console\Output\ConsoleOutput +); + +$kernel->terminate($input, $status); + +exit($status); + +``` + +--- + +## bootstrap/app.php + +```php +withRouting( + web: __DIR__.'/../routes/web.php', + api: __DIR__.'/../routes/api.php', + commands: __DIR__.'/../routes/console.php', + health: '/up', + ) + ->withMiddleware(function (Middleware $middleware) { + // + }) + ->withExceptions(function (Exceptions $exceptions) { + // + })->create(); + +``` + +--- + +## config/app.php + +```php + env('APP_NAME', 'PostHog Laravel Example'), + 'env' => env('APP_ENV', 'production'), + 'debug' => (bool) env('APP_DEBUG', false), + 'url' => env('APP_URL', 'http://localhost'), + 'timezone' => 'UTC', + 'locale' => 'en', + 'fallback_locale' => 'en', + 'key' => env('APP_KEY'), + 'cipher' => 'AES-256-CBC', + + 'providers' => [ + // Laravel Framework Service Providers + Illuminate\Auth\AuthServiceProvider::class, + Illuminate\Broadcasting\BroadcastServiceProvider::class, + Illuminate\Bus\BusServiceProvider::class, + Illuminate\Cache\CacheServiceProvider::class, + Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, + Illuminate\Cookie\CookieServiceProvider::class, + Illuminate\Database\DatabaseServiceProvider::class, + Illuminate\Encryption\EncryptionServiceProvider::class, + Illuminate\Filesystem\FilesystemServiceProvider::class, + Illuminate\Foundation\Providers\FoundationServiceProvider::class, + Illuminate\Hashing\HashServiceProvider::class, + Illuminate\Mail\MailServiceProvider::class, + Illuminate\Notifications\NotificationServiceProvider::class, + Illuminate\Pagination\PaginationServiceProvider::class, + Illuminate\Pipeline\PipelineServiceProvider::class, + Illuminate\Queue\QueueServiceProvider::class, + Illuminate\Redis\RedisServiceProvider::class, + Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, + Illuminate\Session\SessionServiceProvider::class, + Illuminate\Translation\TranslationServiceProvider::class, + Illuminate\Validation\ValidationServiceProvider::class, + Illuminate\View\ViewServiceProvider::class, + ], + + 'aliases' => [ + 'App' => Illuminate\Support\Facades\App::class, + 'Auth' => Illuminate\Support\Facades\Auth::class, + 'Blade' => Illuminate\Support\Facades\Blade::class, + 'Cache' => Illuminate\Support\Facades\Cache::class, + 'Config' => Illuminate\Support\Facades\Config::class, + 'DB' => Illuminate\Support\Facades\DB::class, + 'Hash' => Illuminate\Support\Facades\Hash::class, + 'Request' => Illuminate\Support\Facades\Request::class, + 'Route' => Illuminate\Support\Facades\Route::class, + 'Schema' => Illuminate\Support\Facades\Schema::class, + 'Session' => Illuminate\Support\Facades\Session::class, + 'View' => Illuminate\Support\Facades\View::class, + ], +]; + +``` + +--- + +## config/auth.php + +```php + [ + 'guard' => 'web', + 'passwords' => 'users', + ], + + 'guards' => [ + 'web' => [ + 'driver' => 'session', + 'provider' => 'users', + ], + ], + + 'providers' => [ + 'users' => [ + 'driver' => 'eloquent', + 'model' => App\Models\User::class, + ], + ], + + 'passwords' => [ + 'users' => [ + 'provider' => 'users', + 'table' => 'password_reset_tokens', + 'expire' => 60, + 'throttle' => 60, + ], + ], + + 'password_timeout' => 10800, +]; + +``` + +--- + +## config/database.php + +```php + env('DB_CONNECTION', 'sqlite'), + + 'connections' => [ + 'sqlite' => [ + 'driver' => 'sqlite', + 'url' => env('DATABASE_URL'), + 'database' => env('DB_DATABASE', database_path('database.sqlite')), + 'prefix' => '', + 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), + ], + + 'mysql' => [ + 'driver' => 'mysql', + 'url' => env('DATABASE_URL'), + 'host' => env('DB_HOST', '127.0.0.1'), + 'port' => env('DB_PORT', '3306'), + 'database' => env('DB_DATABASE', 'forge'), + 'username' => env('DB_USERNAME', 'forge'), + 'password' => env('DB_PASSWORD', ''), + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => '', + 'strict' => true, + 'engine' => null, + ], + ], + + 'migrations' => 'migrations', +]; + +``` + +--- + +## config/posthog.php + +```php + env('POSTHOG_API_KEY', ''), + 'host' => env('POSTHOG_HOST', 'https://us.i.posthog.com'), + 'disabled' => env('POSTHOG_DISABLED', false), + 'debug' => env('APP_DEBUG', false), +]; + +``` + +--- + +## config/session.php + +```php + env('SESSION_DRIVER', 'file'), + 'lifetime' => env('SESSION_LIFETIME', 120), + 'expire_on_close' => false, + 'encrypt' => false, + 'files' => storage_path('framework/sessions'), + 'connection' => null, + 'table' => 'sessions', + 'store' => null, + 'lottery' => [2, 100], + 'cookie' => env('SESSION_COOKIE', 'laravel_session'), + 'path' => '/', + 'domain' => env('SESSION_DOMAIN'), + 'secure' => env('SESSION_SECURE_COOKIE'), + 'http_only' => true, + 'same_site' => 'lax', +]; + +``` + +--- + +## database/migrations/2024_01_01_000000_create_users_table.php + +```php +id(); + $table->string('email')->unique(); + $table->string('password'); + $table->boolean('is_staff')->default(false); + $table->rememberToken(); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('users'); + } +}; + +``` + +--- + +## database/seeders/DatabaseSeeder.php + +```php + 'admin@example.com'], + [ + 'password' => bcrypt('admin'), + 'is_staff' => true, + ] + ); + } +} + +``` + +--- + +## IMPLEMENTATION.md + +# Laravel PostHog Example - Implementation Summary + +This document summarizes the implementation of the Laravel PostHog example application, ported from the Flask version. + +## ✅ Completed Implementation + +### Core Application Structure + +**Models & Database** +- ✅ User model with PostHog properties helper method +- ✅ User migration with `is_staff` field +- ✅ Database seeder for default admin user +- ✅ SQLite database configuration + +**PostHog Integration** +- ✅ PostHog configuration file (`config/posthog.php`) +- ✅ PostHogService class with all core methods: + - `identify()` - User identification + - `capture()` - Event tracking + - `captureException()` - Error tracking + - `isFeatureEnabled()` - Feature flag checking + - `getFeatureFlagPayload()` - Feature flag payload retrieval + +**Authentication (Livewire Components)** +- ✅ Login component with PostHog tracking +- ✅ Register component with PostHog tracking +- ✅ Logout route with PostHog tracking + +**Core Features (Livewire Components)** +- ✅ Dashboard - Feature flag demonstration +- ✅ Burrito Tracker - Custom event tracking +- ✅ Profile - Error tracking demonstration + +**API Controllers** +- ✅ BurritoController - API endpoint for burrito tracking +- ✅ ErrorTestController - Manual error capture demonstration + +**Views & Layouts** +- ✅ App layout (authenticated users) +- ✅ Guest layout (unauthenticated users) +- ✅ All Livewire view files with inline styling +- ✅ Error pages (404, 500) + +**Routes** +- ✅ Web routes (authentication, dashboard, burrito, profile, logout) +- ✅ API routes (burrito tracking, error testing) + +**Configuration** +- ✅ Environment example file +- ✅ Composer.json with dependencies +- ✅ Laravel config files (app, auth, database, session) +- ✅ .gitignore + +**Documentation** +- ✅ Comprehensive README +- ✅ Implementation plan (php-plan.md) + +## 📋 Features Implemented + +### 1. User Authentication +- Login with PostHog identification +- Registration with PostHog tracking +- Logout with event capture +- Session management + +### 2. PostHog Analytics +- User identification on login/signup +- Person properties (email, is_staff, date_joined) +- Custom event tracking (burrito considerations) +- Dashboard views tracking + +### 3. Feature Flags +- Feature flag checking (`new-dashboard-feature`) +- Feature flag payload retrieval +- Conditional UI rendering based on flags + +### 4. Error Tracking +- Manual exception capture +- Error ID generation +- Test endpoint with optional capture (`?capture=true/false`) + +### 5. UI/UX +- Responsive layouts +- Flash messages for user feedback +- Livewire reactivity for burrito counter +- Loading states on buttons + +## 🎯 PostHog Integration Points + +| Feature | Location | PostHog Method | +|---------|----------|----------------| +| User Login | `Login.php:23-27` | `identify()` + `capture()` | +| User Signup | `Register.php:29-32` | `identify()` + `capture()` | +| User Logout | `web.php:25` | `capture()` | +| Dashboard View | `Dashboard.php:18` | `capture()` | +| Feature Flag Check | `Dashboard.php:21-25` | `isFeatureEnabled()` | +| Feature Flag Payload | `Dashboard.php:28-31` | `getFeatureFlagPayload()` | +| Burrito Tracking | `BurritoTracker.php:22-24` | `identify()` + `capture()` | +| Profile View | `Profile.php:14` | `capture()` | +| Error Capture | `ErrorTestController.php:22-24` | `identify()` + `captureException()` | + +## 📁 File Structure + +``` +basics/laravel/ +├── app/ +│ ├── Http/ +│ │ ├── Controllers/ +│ │ │ ├── Controller.php +│ │ │ └── Api/ +│ │ │ ├── BurritoController.php +│ │ │ └── ErrorTestController.php +│ │ └── Livewire/ +│ │ ├── Auth/ +│ │ │ ├── Login.php +│ │ │ └── Register.php +│ │ ├── Dashboard.php +│ │ ├── BurritoTracker.php +│ │ └── Profile.php +│ ├── Models/ +│ │ └── User.php +│ └── Services/ +│ └── PostHogService.php +├── config/ +│ ├── app.php +│ ├── auth.php +│ ├── database.php +│ ├── posthog.php +│ └── session.php +├── database/ +│ ├── migrations/ +│ │ └── 2024_01_01_000000_create_users_table.php +│ └── seeders/ +│ └── DatabaseSeeder.php +├── resources/ +│ └── views/ +│ ├── components/ +│ │ └── layouts/ +│ │ ├── app.blade.php +│ │ └── guest.blade.php +│ ├── livewire/ +│ │ ├── auth/ +│ │ │ ├── login.blade.php +│ │ │ └── register.blade.php +│ │ ├── dashboard.blade.php +│ │ ├── burrito-tracker.blade.php +│ │ └── profile.blade.php +│ └── errors/ +│ ├── 404.blade.php +│ └── 500.blade.php +├── routes/ +│ ├── api.php +│ └── web.php +├── .env.example +├── .gitignore +├── composer.json +├── IMPLEMENTATION.md +└── README.md +``` + +## 🔄 Flask to Laravel Mapping + +| Flask Component | Laravel Equivalent | +|----------------|-------------------| +| Flask-Login | Laravel Auth + Livewire | +| Flask-SQLAlchemy | Eloquent ORM | +| Jinja2 Templates | Blade Templates + Livewire | +| Blueprint routes | Route definitions | +| @app.route decorators | Route::get/post | +| session | session() helper | +| flash() | session()->flash() | +| @login_required | Route::middleware('auth') | +| request.form | Livewire properties | +| render_template() | view() or Livewire render() | +| jsonify() | response()->json() | +| SQLAlchemy models | Eloquent models | + +## 🚀 Next Steps for Production + +To make this a production-ready application: + +1. **Install via Composer**: Run full Laravel installation +2. **Environment**: Generate APP_KEY with `php artisan key:generate` +3. **Database**: Run migrations with `php artisan migrate --seed` +4. **Assets**: Set up Vite for asset compilation +5. **Middleware**: Add CSRF protection middleware +6. **Validation**: Add form request classes +7. **Testing**: Implement PHPUnit tests +8. **Caching**: Configure Redis/Memcached +9. **Queue**: Set up queue workers for PostHog events +10. **Deployment**: Configure for production server + +## 📝 Notes + +- This implementation uses inline CSS (matching Flask example) instead of Tailwind compilation +- Livewire provides reactivity without separate JavaScript files +- PostHog service is dependency-injected into components/controllers +- Manual error capture pattern matches Flask implementation +- Session-based burrito counter (same as Flask) +- Default admin account: admin@example.com / admin + +## 🎓 Learning Resources + +- [Laravel Documentation](https://laravel.com/docs) +- [Livewire Documentation](https://livewire.laravel.com) +- [PostHog PHP SDK](https://github.com/PostHog/posthog-php) +- [Eloquent ORM](https://laravel.com/docs/eloquent) + +--- + +**Implementation Date**: January 2026 +**Laravel Version**: 11.x +**Livewire Version**: 3.x +**PostHog PHP SDK**: 3.x + +--- + +## public/index.php + +```php +handleRequest(Request::capture()); + +``` + +--- + +## resources/views/components/layouts/app.blade.php + +```php + + + + + + + + {{ $title ?? 'PostHog Laravel Example' }} + + + @livewireStyles + + + @auth + + @endauth + +
+ @if (session('success')) +
+ {{ session('success') }} +
+ @endif + + @if (session('error')) +
+ {{ session('error') }} +
+ @endif + + {{ $slot }} +
+ + @livewireScripts + + + +``` + +--- + +## resources/views/components/layouts/guest.blade.php + +```php + + + + + + + + {{ $title ?? 'PostHog Laravel Example' }} + + + @livewireStyles + + +
+ {{ $slot }} +
+ + @livewireScripts + + + +``` + +--- + +## resources/views/errors/404.blade.php + +```php + + + + + + Page Not Found - PostHog Laravel Example + + + +
+
+

404

+

Page Not Found

+

The page you're looking for doesn't exist.

+ Go Home +
+
+ + + +``` + +--- + +## resources/views/errors/500.blade.php + +```php + + + + + + Server Error - PostHog Laravel Example + + + +
+
+

500

+

Internal Server Error

+

Something went wrong on our end.

+ Go Home +
+
+ + + +``` + +--- + +## resources/views/livewire/auth/login.blade.php + +```php +
+
+

Welcome to PostHog Laravel Example

+

This example demonstrates how to integrate PostHog with a Laravel application.

+ +
+ + + @error('email')
{{ $message }}
@enderror + + + + @error('password')
{{ $message }}
@enderror + +
+ +
+ + +
+ +

+ Don't have an account? Sign up here +

+

+ Tip: Default credentials are admin@example.com/admin +

+
+ +
+

Features Demonstrated

+ +
+
+ +``` + +--- + +## resources/views/livewire/auth/register.blade.php + +```php +
+
+

Create an Account

+

Sign up to explore the PostHog Laravel integration example.

+ +
+ + + @error('email')
{{ $message }}
@enderror + + + + @error('password')
{{ $message }}
@enderror + + + + + +
+ +

+ Already have an account? Login here +

+
+ +
+

PostHog Integration

+

When you sign up, the following PostHog events are captured:

+ + +

Code Example

+
// After creating the user
+$posthog->identify($user->email, $user->getPostHogProperties());
+$posthog->capture($user->email, 'user_signed_up', [
+    'signup_method' => 'form'
+]);
+
+
+ +``` + +--- + +## resources/views/livewire/burrito-tracker.blade.php + +```php +
+
+

Burrito Consideration Tracker

+

This page demonstrates custom event tracking with PostHog.

+ +
{{ $burritoCount }}
+

Times you've considered a burrito

+ +
+ +
+
+ +
+

Code Example

+
// Livewire component method
+public function considerBurrito(PostHogService $posthog)
+{
+    $this->burritoCount++;
+    session(['burrito_count' => $this->burritoCount]);
+
+    $user = Auth::user();
+    $posthog->identify($user->email, $user->getPostHogProperties());
+    $posthog->capture($user->email, 'burrito_considered', [
+        'total_considerations' => $this->burritoCount,
+    ]);
+}
+
+
+ +``` + +--- + +## resources/views/livewire/dashboard.blade.php + +```php +
+
+

Dashboard

+

Welcome back, {{ auth()->user()->email }}!

+
+ +
+

Error Tracking Demo

+

Test manual exception capture in PostHog. These buttons trigger errors in the context of your logged-in user.

+ + @if($successMessage) +
+ {{ $successMessage }} +
+ @endif + + @if($errorMessage) +
+ {{ $errorMessage }} +
+ @endif + +
+ + +
+ +

Code Example

+
try {
+    // Critical operation that might fail
+    processPayment();
+} catch (\Throwable $e) {
+    // Manually capture this specific exception
+    $errorId = $posthog->captureException($e, $user->email);
+
+    return response()->json([
+        'error' => 'Operation failed',
+        'error_id' => $errorId
+    ], 500);
+}
+

This demonstrates manual exception capture where you have control over whether errors are sent to PostHog.

+
+ +
+

Feature Flags

+ + @if($showNewFeature) +
+ New Feature Enabled! +

You're seeing this because the new-dashboard-feature flag is enabled for you.

+ + @if($featureConfig) +

Feature Configuration:

+
{{ json_encode($featureConfig, JSON_PRETTY_PRINT) }}
+ @endif +
+ @else +

The new-dashboard-feature flag is not enabled for your account.

+ @endif + +

Code Example

+
// Check if feature flag is enabled
+$showNewFeature = $posthog->isFeatureEnabled(
+    'new-dashboard-feature',
+    $user->email,
+    $user->getPostHogProperties()
+);
+
+// Get feature flag payload
+$featureConfig = $posthog->getFeatureFlagPayload(
+    'new-dashboard-feature',
+    $user->email
+);
+
+ +
+ +``` + +--- + +## resources/views/livewire/profile.blade.php + +```php +
+
+

Your Profile

+

This page demonstrates error tracking with PostHog.

+ + + + + + + + + + + + + + +
Email{{ auth()->user()->email }}
Date Joined{{ auth()->user()->created_at->format('Y-m-d H:i') }}
Staff Status{{ auth()->user()->is_staff ? 'Yes' : 'No' }}
+
+ +
+

Error Tracking Demo

+

Test manual exception capture in PostHog. These buttons trigger errors in the context of your logged-in user.

+ + @if($successMessage) +
+ {{ $successMessage }} +
+ @endif + + @if($errorMessage) +
+ {{ $errorMessage }} +
+ @endif + +
+ + +
+ +

+ This demonstrates manual exception capture where you have control over whether errors are sent to PostHog. +

+
+ +
+

Code Example

+
try {
+    throw new \Exception('Test exception from critical operation');
+} catch (\Throwable $e) {
+    // Capture exception with user context
+    $posthog->identify($user->email, $user->getPostHogProperties());
+    $eventId = $posthog->captureException($e, $user->email);
+
+    return response()->json([
+        'error' => 'Operation failed',
+        'error_id' => $eventId,
+        'message' => "Error captured in PostHog. Reference ID: {$eventId}"
+    ], 500);
+}
+
+
+ +``` + +--- + +## routes/api.php + +```php +group(function () { + Route::post('/burrito/consider', [BurritoController::class, 'consider']); + Route::post('/test-error', [ErrorTestController::class, 'test']); +}); + +``` + +--- + +## routes/web.php + +```php +group(function () { + Route::get('/', Login::class)->name('login'); + Route::get('/register', Register::class)->name('register'); +}); + +// Authenticated routes +Route::middleware('auth')->group(function () { + Route::get('/dashboard', Dashboard::class)->name('dashboard'); + Route::get('/burrito', BurritoTracker::class)->name('burrito'); + Route::get('/profile', Profile::class)->name('profile'); + + Route::post('/logout', function (PostHogService $posthog) { + $user = Auth::user(); + + // PostHog: Track logout + $posthog->capture($user->email, 'user_logged_out'); + + Auth::logout(); + request()->session()->invalidate(); + request()->session()->regenerateToken(); + + return redirect('/'); + })->name('logout'); +}); + +``` + +--- + diff --git a/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.0-begin.md b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.0-begin.md new file mode 100644 index 00000000..953ead2d --- /dev/null +++ b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.0-begin.md @@ -0,0 +1,43 @@ +--- +title: PostHog Setup - Begin +description: Start the event tracking setup process by analyzing the project and creating an event tracking plan +--- + +We're making an event tracking plan for this project. + +Before proceeding, find any existing `posthog.capture()` code. Make note of event name formatting. + +From the project's file list, select between 10 and 15 files that might have interesting business value for event tracking, especially conversion and churn events. Also look for additional files related to login that could be used for identifying users, along with error handling. Read the files. If a file is already well-covered by PostHog events, replace it with another option. + +Look for opportunities to track client-side events. + +**IMPORTANT: Server-side events are REQUIRED** if the project includes any instrumentable server-side code. If the project has API routes (e.g., `app/api/**/route.ts`) or Server Actions, you MUST include server-side events for critical business operations like: + + - Payment/checkout completion + - Webhook handlers + - Authentication endpoints + +Do not skip server-side events - they capture actions that cannot be tracked client-side. + +Create a new file with a JSON array at the root of the project: .posthog-events.json. It should include one object for each event we want to add: event name, event description, and the file path we want to place the event in. If events already exist, don't duplicate them; supplement them. + +Track actions only, not pageviews. These can be captured automatically. Exceptions can be made for "viewed"-type events that correspond to the top of a conversion funnel. + +As you review files, make an internal note of opportunities to identify users and catch errors. We'll need them for the next step. + +## Status + +Before beginning a phase of the setup, you will send a status message with the exact prefix '[STATUS]', as in: + +[STATUS] Checking project structure. + +Status to report in this phase: + +- Checking project structure +- Verifying PostHog dependencies +- Generating events based on project + + +--- + +**Upon completion, continue with:** [basic-integration-1.1-edit.md](basic-integration-1.1-edit.md) \ No newline at end of file diff --git a/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.1-edit.md b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.1-edit.md new file mode 100644 index 00000000..44c1a4e9 --- /dev/null +++ b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.1-edit.md @@ -0,0 +1,37 @@ +--- +title: PostHog Setup - Edit +description: Implement PostHog event tracking in the identified files, following best practices and the example project +--- + +For each of the files and events noted in .posthog-events.json, make edits to capture events using PostHog. Make sure to set up any helper files needed. Carefully examine the included example project code: your implementation should match it as closely as possible. + +Use environment variables for PostHog keys. Do not hardcode PostHog keys. + +If a file already has existing integration code for other tools or services, don't overwrite or remove that code. Place PostHog code below it. + +For each event, add useful properties, and use your access to the PostHog source code to ensure correctness. You also have access to documentation about creating new events with PostHog. Consider this documentation carefully and follow it closely before adding events. Your integration should be based on documented best practices. Carefully consider how the user project's framework version may impact the correct PostHog integration approach. + +Remember that you can find the source code for any dependency in the node_modules directory. This may be necessary to properly populate property names. There are also example project code files available via the PostHog MCP; use these for reference. + +Where possible, add calls for PostHog's identify() function on the client side upon events like logins and signups. Use the contents of login and signup forms to identify users on submit. If there is server-side code, pass the client-side session and distinct ID to the server-side code to identify the user. On the server side, make sure events have a matching distinct ID where relevant. + +It's essential to do this in both client code and server code, so that user behavior from both domains is easy to correlate. + +You should also add PostHog exception capture error tracking to these files where relevant. + +Remember: Do not alter the fundamental architecture of existing files. Make your additions minimal and targeted. + +Remember the documentation and example project resources you were provided at the beginning. Read them now. + +## Status + +Status to report in this phase: + +- Inserting PostHog capture code +- A status message for each file whose edits you are planning, including a high level summary of changes +- A status message for each file you have edited + + +--- + +**Upon completion, continue with:** [basic-integration-1.2-revise.md](basic-integration-1.2-revise.md) \ No newline at end of file diff --git a/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.2-revise.md b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.2-revise.md new file mode 100644 index 00000000..26f3a60e --- /dev/null +++ b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.2-revise.md @@ -0,0 +1,22 @@ +--- +title: PostHog Setup - Revise +description: Review and fix any errors in the PostHog integration implementation +--- + +Check the project for errors. Read the package.json file for any type checking or build scripts that may provide input about what to fix. Remember that you can find the source code for any dependency in the node_modules directory. + +Ensure that any components created were actually used. + +Once all other tasks are complete, run any linter or prettier-like scripts found in the package.json. + +## Status + +Status to report in this phase: + +- Finding and correcting errors +- Report details of any errors you fix +- Linting, building and prettying + +--- + +**Upon completion, continue with:** [basic-integration-1.3-conclude.md](basic-integration-1.3-conclude.md) \ No newline at end of file diff --git a/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.3-conclude.md b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.3-conclude.md new file mode 100644 index 00000000..552118fb --- /dev/null +++ b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/basic-integration-1.3-conclude.md @@ -0,0 +1,36 @@ +--- +title: PostHog Setup - Conclusion +description: Review and fix any errors in the PostHog integration implementation +--- + +Use the PostHog MCP to create a new dashboard named "Analytics basics" based on the events created here. Make sure to use the exact same event names as implemented in the code. Populate it with up to five insights, with special emphasis on things like conversion funnels, churn events, and other business critical insights. + +Create the file posthog-setup-report.md. It should include a summary of the integration edits, a table with the event names, event descriptions, and files where events were added, along with a list of links for the dashboard and insights created. Follow this format: + + +# PostHog post-wizard report + +The wizard has completed a deep integration of your project. [Detailed summary of changes] + +[table of events/descriptions/files] + +## Next steps + +We've built some insights and a dashboard for you to keep an eye on user behavior, based on the events we just instrumented: + +[links] + +### Agent skill + +We've left an agent skill folder in your project. You can use this context for further agent development when using Claude Code. This will help ensure the model provides the most up-to-date approaches for integrating PostHog. + + + +Upon completion, remove .posthog-events.json. + +## Status + +Status to report in this phase: + +- Configured dashboard: [insert PostHog dashboard URL] +- Created setup report: [insert full local file path] \ No newline at end of file diff --git a/apps/laravel/laravel12-saas/.claude/skills/laravel/references/identify-users.md b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/identify-users.md new file mode 100644 index 00000000..ce165453 --- /dev/null +++ b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/identify-users.md @@ -0,0 +1,202 @@ +# Identify users - Docs + +Linking events to specific users enables you to build a full picture of how they're using your product across different sessions, devices, and platforms. + +This is straightforward to do when [capturing backend events](/docs/product-analytics/capture-events?tab=Node.js.md), as you associate events to a specific user using a `distinct_id`, which is a required argument. + +However, in the frontend of a [web](/docs/libraries/js/features#capturing-events.md) or [mobile app](/docs/libraries/ios#capturing-events.md), a `distinct_id` is not a required argument — PostHog's SDKs will generate an anonymous `distinct_id` for you automatically and you can capture events anonymously, provided you use the appropriate [configuration](/docs/libraries/js/features#capturing-anonymous-events.md). + +To link events to specific users, call `identify`: + +PostHog AI + +### Web + +```javascript +posthog.identify( + 'distinct_id', // Replace 'distinct_id' with your user's unique identifier + { email: 'max@hedgehogmail.com', name: 'Max Hedgehog' } // optional: set additional person properties +); +``` + +### Android + +```kotlin +PostHog.identify( + distinctId = distinctID, // Replace 'distinctID' with your user's unique identifier + // optional: set additional person properties + userProperties = mapOf( + "name" to "Max Hedgehog", + "email" to "max@hedgehogmail.com" + ) +) +``` + +### iOS + +```swift +PostHogSDK.shared.identify("distinct_id", // Replace "distinct_id" with your user's unique identifier + userProperties: ["name": "Max Hedgehog", "email": "max@hedgehogmail.com"]) // optional: set additional person properties +``` + +### React Native + +```jsx +posthog.identify('distinct_id', { // Replace "distinct_id" with your user's unique identifier + email: 'max@hedgehogmail.com', // optional: set additional person properties + name: 'Max Hedgehog' +}) +``` + +### Dart + +```dart +await Posthog().identify( + userId: 'distinct_id', // Replace "distinct_id" with your user's unique identifier + userProperties: { + email: "max@hedgehogmail.com", // optional: set additional person properties + name: "Max Hedgehog" +}); +``` + +Events captured after calling `identify` are identified events and this creates a person profile if one doesn't exist already. + +Due to the cost of processing them, anonymous events can be up to 4x cheaper than identified events, so it's recommended you only capture identified events when needed. + +## How identify works + +When a user starts browsing your website or app, PostHog automatically assigns them an **anonymous ID**, which is stored locally. + +Provided you've [configured persistence](/docs/libraries/js/persistence.md) to use cookies or `localStorage`, this enables us to track anonymous users – even across different sessions. + +By calling `identify` with a `distinct_id` of your choice (usually the user's ID in your database, or their email), you link the anonymous ID and distinct ID together. + +Thus, all past and future events made with that anonymous ID are now associated with the distinct ID. + +This enables you to do things like associate events with a user from before they log in for the first time, or associate their events across different devices or platforms. + +Using identify in the backend + +Although you can call `identify` using our backend SDKs, it is used most in frontends. This is because there is no concept of anonymous sessions in the backend SDKs, so calling `identify` only updates person profiles. + +## Best practices when using `identify` + +### 1\. Call `identify` as soon as you're able to + +In your frontend, you should call `identify` as soon as you're able to. + +Typically, this is every time your **app loads** for the first time, and directly after your **users log in**. + +This ensures that events sent during your users' sessions are correctly associated with them. + +You only need to call `identify` once per session, and you should avoid calling it multiple times unnecessarily. + +If you call `identify` multiple times with the same data without reloading the page in between, PostHog will ignore the subsequent calls. + +### 2\. Use unique strings for distinct IDs + +If two users have the same distinct ID, their data is merged and they are considered one user in PostHog. Two common ways this can happen are: + +- Your logic for generating IDs does not generate sufficiently strong IDs and you can end up with a clash where 2 users have the same ID. +- There's a bug, typo, or mistake in your code leading to most or all users being identified with generic IDs like `null`, `true`, or `distinctId`. + +PostHog also has built-in protections to stop the most common distinct ID mistakes. + +### 3\. Reset after logout + +If a user logs out on your frontend, you should call `reset()` to unlink any future events made on that device with that user. + +This is important if your users are sharing a computer, as otherwise all of those users are grouped together into a single user due to shared cookies between sessions. + +**We strongly recommend you call `reset` on logout even if you don't expect users to share a computer.** + +You can do that like so: + +PostHog AI + +### Web + +```javascript +posthog.reset() +``` + +### iOS + +```swift +PostHogSDK.shared.reset() +``` + +### Android + +```kotlin +PostHog.reset() +``` + +### React Native + +```jsx +posthog.reset() +``` + +### Dart + +```dart +Posthog().reset() +``` + +If you *also* want to reset the `device_id` so that the device will be considered a new device in future events, you can pass `true` as an argument: + +Web + +PostHog AI + +```javascript +posthog.reset(true) +``` + +### 4\. Person profiles and properties + +You'll notice that one of the parameters in the `identify` method is a `properties` object. + +This enables you to set [person properties](/docs/product-analytics/person-properties.md). + +Whenever possible, we recommend passing in all person properties you have available each time you call identify, as this ensures their person profile on PostHog is up to date. + +Person properties can also be set being adding a `$set` property to a event `capture` call. + +See our [person properties docs](/docs/product-analytics/person-properties.md) for more details on how to work with them and best practices. + +### 5\. Use deep links between platforms + +We recommend you call `identify` [as soon as you're able](#1-call-identify-as-soon-as-youre-able), typically when a user signs up or logs in. + +This doesn't work if one or both platforms are unauthenticated. Some examples of such cases are: + +- Onboarding and signup flows before authentication. +- Unauthenticated web pages redirecting to authenticated mobile apps. +- Authenticated web apps prompting an app download. + +In these cases, you can use a [deep link](https://developer.android.com/training/app-links/deep-linking) on Android and [universal links](https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app) on iOS to identify users. + +1. Use `posthog.get_distinct_id()` to get the current distinct ID. Even if you cannot call identify because the user is unauthenticated, this will return an anonymous distinct ID generated by PostHog. +2. Add the distinct ID to the deep link as query parameters, along with other properties like UTM parameters. +3. When the user is redirected to the app, parse the deep link and handle the following cases: + +- The user is already authenticated on the mobile app. In this case, call [`posthog.alias()`](/docs/libraries/js/features#alias.md) with the distinct ID from the web. This associates the two distinct IDs as a single person. +- The user is unauthenticated. In this case, call [`posthog.identify()`](/docs/libraries/js/features#identifying-users.md) with the distinct ID from the web. Events will be associated with this distinct ID. + +As long as you associate the distinct IDs with `posthog.identify()` or `posthog.alias()`, you can track events generated across platforms. + +## Further reading + +- [Identifying users docs](/docs/product-analytics/identify.md) +- [How person processing works](/docs/how-posthog-works/ingestion-pipeline#2-person-processing.md) +- [An introductory guide to identifying users in PostHog](/tutorials/identifying-users-guide.md) + +### Community questions + +Ask a question + +### Was this page useful? + +HelpfulCould be better \ No newline at end of file diff --git a/apps/laravel/laravel12-saas/.claude/skills/laravel/references/laravel.md b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/laravel.md new file mode 100644 index 00000000..c5442fc2 --- /dev/null +++ b/apps/laravel/laravel12-saas/.claude/skills/laravel/references/laravel.md @@ -0,0 +1,75 @@ +# Laravel - Docs + +PostHog makes it easy to get data about traffic and usage of your Laravel app. Integrating PostHog enables analytics, custom events capture, feature flags, and more. + +This guide walks you through integrating PostHog into your Laravel app using the [PHP SDK](/docs/libraries/php.md). + +## Installation + +First, ensure [Composer](https://getcomposer.org/) is installed. Then run `composer require posthog/posthog-php` to install PostHog’s PHP SDK. + +Next, initialize PostHog in the `boot` method of `app/Providers/AppServiceProvider.php`: + +app/Providers/AppServiceProvider.php + +PostHog AI + +```php +', + [ + 'host' => 'https://us.i.posthog.com' + ] + ); + } +} +``` + +You can find your project API key and instance address in [your project settings](https://us.posthog.com/project/settings). + +## Usage + +To access your PostHog client anywhere in your app, import `use PostHog\PostHog;` and call `PostHog::method_name`. For example, below is how to capture an event in a simple route: + +routes/web.php + +PostHog AI + +```php + 'distinct_id_of_your_user', + 'event' => 'route_called' + ]); + return view('welcome'); +}); +``` + +## Next steps + +For any technical questions for how to integrate specific PostHog features into Laravel (such as analytics, feature flags, A/B testing, etc.), have a look at our [PHP SDK docs](/docs/libraries/php.md). + +Alternatively, the following tutorials can help you get started: + +- [How to set up analytics in Laravel](/tutorials/laravel-analytics.md) +- [How to set up feature flags in Laravel](/tutorials/laravel-feature-flags.md) +- [How to set up A/B tests in Laravel](/tutorials/laravel-ab-tests.md) + +### Community questions + +Ask a question + +### Was this page useful? + +HelpfulCould be better \ No newline at end of file diff --git a/apps/laravel/laravel12-saas/.env.example b/apps/laravel/laravel12-saas/.env.example index d6998f6c..3434ad9b 100644 --- a/apps/laravel/laravel12-saas/.env.example +++ b/apps/laravel/laravel12-saas/.env.example @@ -79,3 +79,7 @@ AWS_BUCKET= AWS_USE_PATH_STYLE_ENDPOINT=false VITE_APP_NAME="${APP_NAME}" + +POSTHOG_API_KEY= +POSTHOG_HOST=https://us.i.posthog.com +POSTHOG_DISABLED=false diff --git a/apps/laravel/laravel12-saas/app/Http/Controllers/Auth/SocialiteController.php b/apps/laravel/laravel12-saas/app/Http/Controllers/Auth/SocialiteController.php index 1345f8f1..dba37672 100644 --- a/apps/laravel/laravel12-saas/app/Http/Controllers/Auth/SocialiteController.php +++ b/apps/laravel/laravel12-saas/app/Http/Controllers/Auth/SocialiteController.php @@ -4,6 +4,7 @@ use App\Http\Controllers\Controller; use App\Models\User; +use App\Services\PostHogService; use Exception; use Illuminate\Support\Facades\Auth; use Laravel\Socialite\Facades\Socialite; @@ -15,11 +16,14 @@ public function redirect($provider) return Socialite::driver($provider)->redirect(); } - public function callback($provider) + public function callback($provider, PostHogService $posthog) { try { $socialUser = Socialite::driver($provider)->user(); } catch (Exception $e) { + // PostHog: Capture OAuth error + $posthog->captureException($e); + return redirect('/login')->withErrors(['error' => 'Unable to login using '.$provider]); } @@ -28,7 +32,9 @@ public function callback($provider) 'provider_id' => $socialUser->getId(), ])->first(); + $isNewUser = false; if (! $user) { + $isNewUser = true; $user = User::create([ 'name' => $socialUser->getName(), 'email' => $socialUser->getEmail(), @@ -40,6 +46,17 @@ public function callback($provider) Auth::login($user); + // PostHog: Identify and track login/signup + $posthog->identify($user->email, $user->getPostHogProperties()); + if ($isNewUser) { + $posthog->capture($user->email, 'user_signed_up', [ + 'signup_method' => $provider, + ]); + } + $posthog->capture($user->email, 'user_logged_in', [ + 'login_method' => $provider, + ]); + return redirect('/dashboard'); } } diff --git a/apps/laravel/laravel12-saas/app/Http/Controllers/SubscriptionController.php b/apps/laravel/laravel12-saas/app/Http/Controllers/SubscriptionController.php index 373b681d..3787f825 100644 --- a/apps/laravel/laravel12-saas/app/Http/Controllers/SubscriptionController.php +++ b/apps/laravel/laravel12-saas/app/Http/Controllers/SubscriptionController.php @@ -7,11 +7,16 @@ use App\Actions\Billing\RedirectToBillingPortal; use App\Actions\Billing\SwapPlan; use App\Domains\Billing\PlanCatalog; +use App\Services\PostHogService; use Exception; use Illuminate\Http\Request; class SubscriptionController extends Controller { + public function __construct( + protected PostHogService $posthog + ) {} + public function index(Request $request, PlanCatalog $catalog, GetSubscriptionSummary $summary) { $plans = $catalog->all(); @@ -28,8 +33,14 @@ public function checkout(Request $request, PlanCatalog $catalog, CheckoutPlan $c $plan = $catalog->findOrFail($request->plan); $user = $request->user(); + // PostHog: Track checkout initiated + $this->posthog->capture($user->email, 'subscription_checkout_started', [ + 'plan_name' => $plan->name, + 'plan_price' => $plan->price, + ]); + // Stub out subscription if Stripe isn't configured (for demo/development) - if (!CheckoutPlan::isStripeConfigured()) { + if (! CheckoutPlan::isStripeConfigured()) { return $this->createStubSubscription($user, $plan); } @@ -49,16 +60,16 @@ protected function createStubSubscription($user, $plan) // Create a fake subscription $user->subscriptions()->create([ 'type' => 'default', - 'stripe_id' => 'sub_demo_' . uniqid(), + 'stripe_id' => 'sub_demo_'.uniqid(), 'stripe_status' => 'active', - 'stripe_price' => $plan->stripe_plan_id ?? 'price_demo_' . uniqid(), + 'stripe_price' => $plan->stripe_plan_id ?? 'price_demo_'.uniqid(), 'quantity' => 1, 'trial_ends_at' => null, 'ends_at' => null, 'amount' => $plan->price ?? 0, ]); - return redirect()->route('dashboard')->with('success', 'Demo subscription created for ' . $plan->name . '. (Stripe not configured)'); + return redirect()->route('dashboard')->with('success', 'Demo subscription created for '.$plan->name.'. (Stripe not configured)'); } public function swap(Request $request, PlanCatalog $catalog, SwapPlan $swapPlan) @@ -70,8 +81,17 @@ public function swap(Request $request, PlanCatalog $catalog, SwapPlan $swapPlan) try { $swapPlan($user, $plan); + // PostHog: Track plan swap + $this->posthog->capture($user->email, 'subscription_plan_swapped', [ + 'new_plan_name' => $plan->name, + 'new_plan_price' => $plan->price, + ]); + return redirect()->route('subscribe')->with('success', 'Your subscription has been updated to '.$plan->name.'.'); } catch (Exception $e) { + // PostHog: Capture plan swap error + $this->posthog->captureException($e, $user->email); + return redirect()->route('subscribe')->with('error', 'There was an error updating your subscription: '.$e->getMessage()); } } diff --git a/apps/laravel/laravel12-saas/app/Livewire/Actions/Logout.php b/apps/laravel/laravel12-saas/app/Livewire/Actions/Logout.php index 3ef481d1..0eebead6 100644 --- a/apps/laravel/laravel12-saas/app/Livewire/Actions/Logout.php +++ b/apps/laravel/laravel12-saas/app/Livewire/Actions/Logout.php @@ -2,6 +2,7 @@ namespace App\Livewire\Actions; +use App\Services\PostHogService; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; @@ -10,8 +11,15 @@ class Logout /** * Log the current user out of the application. */ - public function __invoke(): void + public function __invoke(PostHogService $posthog): void { + $user = Auth::user(); + + // PostHog: Track logout before invalidating session + if ($user) { + $posthog->capture($user->email, 'user_logged_out'); + } + Auth::guard('web')->logout(); Session::invalidate(); diff --git a/apps/laravel/laravel12-saas/app/Models/User.php b/apps/laravel/laravel12-saas/app/Models/User.php index b1b9a44b..1854b900 100644 --- a/apps/laravel/laravel12-saas/app/Models/User.php +++ b/apps/laravel/laravel12-saas/app/Models/User.php @@ -65,4 +65,16 @@ public function canAccessPanel(Panel $panel): bool { return str_ends_with($this->email, '@mvpable.com'); } + + /** + * Get PostHog person properties for this user. + */ + public function getPostHogProperties(): array + { + return [ + 'email' => $this->email, + 'name' => $this->name, + 'date_joined' => $this->created_at?->toISOString(), + ]; + } } diff --git a/apps/laravel/laravel12-saas/app/Services/PostHogService.php b/apps/laravel/laravel12-saas/app/Services/PostHogService.php new file mode 100644 index 00000000..156a7589 --- /dev/null +++ b/apps/laravel/laravel12-saas/app/Services/PostHogService.php @@ -0,0 +1,95 @@ + config('posthog.host'), + 'debug' => config('posthog.debug'), + ] + ); + self::$initialized = true; + } + } + + public function identify(string $distinctId, array $properties = []): void + { + if (config('posthog.disabled')) { + return; + } + + PostHog::identify([ + 'distinctId' => $distinctId, + 'properties' => $properties, + ]); + } + + public function capture(string $distinctId, string $event, array $properties = []): void + { + if (config('posthog.disabled')) { + return; + } + + PostHog::capture([ + 'distinctId' => $distinctId, + 'event' => $event, + 'properties' => $properties, + ]); + } + + public function captureException(\Throwable $exception, ?string $distinctId = null): ?string + { + if (config('posthog.disabled')) { + return null; + } + + $distinctId = $distinctId ?? Auth::user()?->email ?? 'anonymous'; + + $eventId = uniqid('error_', true); + + $this->capture($distinctId, '$exception', [ + 'error_id' => $eventId, + 'exception_type' => get_class($exception), + 'exception_message' => $exception->getMessage(), + 'exception_file' => $exception->getFile(), + 'exception_line' => $exception->getLine(), + 'stack_trace' => $exception->getTraceAsString(), + ]); + + return $eventId; + } + + public function isFeatureEnabled(string $key, string $distinctId, array $properties = []): ?bool + { + if (config('posthog.disabled')) { + return false; + } + + return PostHog::isFeatureEnabled($key, $distinctId, $properties); + } + + public function getFeatureFlagPayload(string $key, string $distinctId) + { + if (config('posthog.disabled')) { + return null; + } + + return PostHog::getFeatureFlagPayload($key, $distinctId); + } +} diff --git a/apps/laravel/laravel12-saas/composer.json b/apps/laravel/laravel12-saas/composer.json index 114f06b1..04d5f48e 100644 --- a/apps/laravel/laravel12-saas/composer.json +++ b/apps/laravel/laravel12-saas/composer.json @@ -18,6 +18,7 @@ "laravel/tinker": "^2.9", "livewire/livewire": "^3.5", "livewire/volt": "^1.7.1", + "posthog/posthog-php": "*", "propaganistas/laravel-disposable-email": "^2.4", "pxlrbt/filament-activity-log": "^2.0", "pxlrbt/filament-environment-indicator": "^3.0", diff --git a/apps/laravel/laravel12-saas/composer.lock b/apps/laravel/laravel12-saas/composer.lock index 5cc626e1..05f329d2 100644 --- a/apps/laravel/laravel12-saas/composer.lock +++ b/apps/laravel/laravel12-saas/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0275185749fcbdf020b24729821224d6", + "content-hash": "f3e53709500d2b94efa13cb988217af9", "packages": [ { "name": "anourvalar/eloquent-serialize", @@ -5060,6 +5060,60 @@ ], "time": "2025-12-15T11:51:42+00:00" }, + { + "name": "posthog/posthog-php", + "version": "3.7.3", + "source": { + "type": "git", + "url": "https://github.com/PostHog/posthog-php.git", + "reference": "bb7e317da1c35eaa523d9e80c76d4d86bf5067df" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PostHog/posthog-php/zipball/bb7e317da1c35eaa523d9e80c76d4d86bf5067df", + "reference": "bb7e317da1c35eaa523d9e80c76d4d86bf5067df", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": ">=8.0" + }, + "require-dev": { + "overtrue/phplint": "^3.0", + "phpunit/phpunit": "^9.0", + "slope-it/clock-mock": "^0.4.0", + "squizlabs/php_codesniffer": "^3.7" + }, + "bin": [ + "bin/posthog" + ], + "type": "library", + "autoload": { + "psr-4": { + "PostHog\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PostHog ", + "homepage": "https://posthog.com/" + } + ], + "description": "PostHog PHP Library", + "homepage": "https://github.com/PostHog/posthog-php", + "keywords": [ + "posthog" + ], + "support": { + "issues": "https://github.com/PostHog/posthog-php/issues", + "source": "https://github.com/PostHog/posthog-php/tree/3.7.3" + }, + "time": "2025-12-04T19:45:37+00:00" + }, { "name": "pragmarx/google2fa", "version": "v9.0.0", diff --git a/apps/laravel/laravel12-saas/config/posthog.php b/apps/laravel/laravel12-saas/config/posthog.php new file mode 100644 index 00000000..df6551b4 --- /dev/null +++ b/apps/laravel/laravel12-saas/config/posthog.php @@ -0,0 +1,8 @@ + env('POSTHOG_API_KEY', ''), + 'host' => env('POSTHOG_HOST', 'https://us.i.posthog.com'), + 'disabled' => env('POSTHOG_DISABLED', false), + 'debug' => env('APP_DEBUG', false), +]; diff --git a/apps/laravel/laravel12-saas/posthog-setup-report.md b/apps/laravel/laravel12-saas/posthog-setup-report.md new file mode 100644 index 00000000..670905cd --- /dev/null +++ b/apps/laravel/laravel12-saas/posthog-setup-report.md @@ -0,0 +1,68 @@ +# PostHog post-wizard report + +The wizard has completed a deep integration of PostHog analytics into your Laravel SaaS application. The integration adds comprehensive event tracking for user authentication, subscription management, and profile updates using a centralized `PostHogService` class that wraps the PostHog PHP SDK. + +## Configuration Files Created + +| File | Description | +|------|-------------| +| `config/posthog.php` | PostHog configuration using environment variables | +| `app/Services/PostHogService.php` | Service class wrapping PostHog SDK with identify, capture, and error tracking methods | + +## Events Implemented + +| Event Name | Description | File | +|------------|-------------|------| +| `user_signed_up` | User completed registration (form or OAuth) | `resources/views/livewire/pages/auth/register.blade.php`, `app/Http/Controllers/Auth/SocialiteController.php` | +| `user_logged_in` | User successfully logged in (password or OAuth) | `resources/views/livewire/pages/auth/login.blade.php`, `app/Http/Controllers/Auth/SocialiteController.php` | +| `user_logged_out` | User logged out of the application | `app/Livewire/Actions/Logout.php` | +| `subscription_checkout_started` | User initiated checkout for a subscription plan | `app/Http/Controllers/SubscriptionController.php` | +| `subscription_plan_swapped` | User changed their subscription plan | `app/Http/Controllers/SubscriptionController.php` | +| `profile_updated` | User updated their profile information | `resources/views/livewire/profile/update-profile-information-form.blade.php` | +| `password_updated` | User changed their password | `resources/views/livewire/profile/update-password-form.blade.php` | +| `account_deleted` | User deleted their account (churn event) | `resources/views/livewire/profile/delete-user-form.blade.php` | +| `password_reset_requested` | User requested a password reset | `resources/views/livewire/pages/auth/forgot-password.blade.php` | + +## User Identification + +Users are identified with the following properties: +- `email` - User's email address +- `name` - User's display name +- `date_joined` - Account creation timestamp + +## Error Tracking + +Exception capture is integrated into: +- OAuth login failures (`app/Http/Controllers/Auth/SocialiteController.php`) +- Subscription plan swap failures (`app/Http/Controllers/SubscriptionController.php`) + +## Environment Variables + +Add the following to your `.env` file: + +```env +POSTHOG_API_KEY=your_posthog_api_key +POSTHOG_HOST=https://us.i.posthog.com +POSTHOG_DISABLED=false +``` + +## Next steps + +### Create Recommended Dashboard & Insights + +We recommend creating a dashboard named "Analytics Basics" with these insights: + +1. **User Signup Funnel**: Track conversion from signup page view to `user_signed_up` +2. **Subscription Conversion Funnel**: `user_signed_up` → `subscription_checkout_started` → subscription success +3. **Churn Analysis**: Track `account_deleted` events over time with `days_since_signup` breakdown +4. **Authentication Methods**: Breakdown of `user_logged_in` by `login_method` property +5. **User Engagement**: Track active users based on login frequency + +### Access Your PostHog Dashboard + +Visit your PostHog instance to create these insights: +- PostHog US: https://us.i.posthog.com + +### Agent skill + +We've left an agent skill folder in your project at `.claude/skills/laravel/`. You can use this context for further agent development when using Claude Code. This will help ensure the model provides the most up-to-date approaches for integrating PostHog. diff --git a/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/forgot-password.blade.php b/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/forgot-password.blade.php index 6cc0ab57..53bfc9da 100644 --- a/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/forgot-password.blade.php +++ b/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/forgot-password.blade.php @@ -1,5 +1,6 @@ validate([ 'email' => ['required', 'string', 'email'], @@ -24,6 +25,11 @@ public function sendPasswordResetLink(): void $this->only('email') ); + // PostHog: Track password reset request + $posthog->capture($this->email, 'password_reset_requested', [ + 'success' => $status === Password::RESET_LINK_SENT, + ]); + if ($status != Password::RESET_LINK_SENT) { $this->addError('email', __($status)); diff --git a/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/login.blade.php b/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/login.blade.php index b4ee555d..3bc2756c 100644 --- a/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/login.blade.php +++ b/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/login.blade.php @@ -1,6 +1,8 @@ validate(); @@ -20,6 +22,13 @@ public function login(): void Session::regenerate(); + // PostHog: Identify and track login + $user = Auth::user(); + $posthog->identify($user->email, $user->getPostHogProperties()); + $posthog->capture($user->email, 'user_logged_in', [ + 'login_method' => 'password', + ]); + $this->redirectIntended(default: route('dashboard', absolute: false), navigate: false); } diff --git a/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/register.blade.php b/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/register.blade.php index 30ed2b0e..68a3ac18 100644 --- a/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/register.blade.php +++ b/apps/laravel/laravel12-saas/resources/views/livewire/pages/auth/register.blade.php @@ -1,6 +1,7 @@ validate([ 'name' => ['required', 'string', 'max:255'], @@ -30,6 +31,12 @@ public function register(): void event(new Registered($user = User::create($validated))); + // PostHog: Identify new user and track signup + $posthog->identify($user->email, $user->getPostHogProperties()); + $posthog->capture($user->email, 'user_signed_up', [ + 'signup_method' => 'form', + ]); + Auth::login($user); $this->redirect(route('dashboard', absolute: false), navigate: false); diff --git a/apps/laravel/laravel12-saas/resources/views/livewire/profile/delete-user-form.blade.php b/apps/laravel/laravel12-saas/resources/views/livewire/profile/delete-user-form.blade.php index 5e2bb574..98a853cd 100644 --- a/apps/laravel/laravel12-saas/resources/views/livewire/profile/delete-user-form.blade.php +++ b/apps/laravel/laravel12-saas/resources/views/livewire/profile/delete-user-form.blade.php @@ -1,6 +1,7 @@ validate([ 'password' => ['required', 'string', 'current_password'], ]); - tap(Auth::user(), $logout(...))->delete(); + $user = Auth::user(); + + // PostHog: Track account deletion (churn event) before deleting + $posthog->capture($user->email, 'account_deleted', [ + 'days_since_signup' => $user->created_at?->diffInDays(now()), + ]); + + tap($user, $logout(...))->delete(); $this->redirect('/', navigate: true); } diff --git a/apps/laravel/laravel12-saas/resources/views/livewire/profile/update-password-form.blade.php b/apps/laravel/laravel12-saas/resources/views/livewire/profile/update-password-form.blade.php index 95df55c9..2afe27cf 100644 --- a/apps/laravel/laravel12-saas/resources/views/livewire/profile/update-password-form.blade.php +++ b/apps/laravel/laravel12-saas/resources/views/livewire/profile/update-password-form.blade.php @@ -1,5 +1,6 @@ validate([ @@ -28,10 +29,15 @@ public function updatePassword(): void throw $e; } - Auth::user()->update([ + $user = Auth::user(); + + $user->update([ 'password' => Hash::make($validated['password']), ]); + // PostHog: Track password update + $posthog->capture($user->email, 'password_updated'); + $this->reset('current_password', 'password', 'password_confirmation'); $this->dispatch('password-updated'); diff --git a/apps/laravel/laravel12-saas/resources/views/livewire/profile/update-profile-information-form.blade.php b/apps/laravel/laravel12-saas/resources/views/livewire/profile/update-profile-information-form.blade.php index 6d1bdcda..1afded3c 100644 --- a/apps/laravel/laravel12-saas/resources/views/livewire/profile/update-profile-information-form.blade.php +++ b/apps/laravel/laravel12-saas/resources/views/livewire/profile/update-profile-information-form.blade.php @@ -1,6 +1,7 @@ ['required', 'string', 'indisposable', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($user->id)], ]); + $emailChanged = $user->email !== $validated['email']; + $nameChanged = $user->name !== $validated['name']; + $user->fill($validated); if ($user->isDirty('email')) { @@ -40,6 +44,15 @@ public function updateProfileInformation(): void $user->save(); + // PostHog: Track profile update + $posthog->capture($user->email, 'profile_updated', [ + 'email_changed' => $emailChanged, + 'name_changed' => $nameChanged, + ]); + + // PostHog: Update user properties + $posthog->identify($user->email, $user->getPostHogProperties()); + $this->dispatch('profile-updated', name: $user->name); }