When you sign up, the following PostHog events are captured:
+
+
identify() - Associates your email with the user
+
capture() - Sets person properties (email, etc.)
+
user_signed_up event - Tracks the signup action
+
+
+
Code Example
+
// After creating the user
+$posthog->identify($user->email, $user->getPostHogProperties());
+$posthog->capture($user->email, 'user_signed_up', [
+ 'signup_method' => 'form'
+]);
+
+```
+
+---
+
+## 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);
}