diff --git a/app/Jobs/PostHog/SyncUser.php b/app/Jobs/PostHog/SyncUser.php index 2a9a8c19..f2529333 100644 --- a/app/Jobs/PostHog/SyncUser.php +++ b/app/Jobs/PostHog/SyncUser.php @@ -57,8 +57,8 @@ public function handle(PostHogService $postHog): void } $postHog->identify((string) $user->id, [ - 'email' => $user->email, - 'name' => $user->name, + '$email' => $user->email, + '$name' => $user->name, '$set_once' => ['signed_up_at' => $user->created_at?->toIso8601String()], ]); diff --git a/app/Listeners/StripeEventListener.php b/app/Listeners/StripeEventListener.php index e2634b90..3aecd923 100644 --- a/app/Listeners/StripeEventListener.php +++ b/app/Listeners/StripeEventListener.php @@ -91,6 +91,14 @@ protected function handleSubscriptionUpdated(Account $account, array $payload): */ protected function handleSubscriptionDeleted(Account $account, array $payload): void { + // Stripe re-delivers webhooks on transient failures; if the plan is + // already cleared we skip the rest so the cancellation event isn't + // captured twice and the (idempotent but pointless) cache flush + // doesn't run again. + if ($account->plan_id === null) { + return; + } + $previousPlan = $account->plan?->name; $account->update(['plan_id' => null]); diff --git a/resources/js/composables/useTracking.ts b/resources/js/composables/useTracking.ts index da363f02..c9fb4a78 100644 --- a/resources/js/composables/useTracking.ts +++ b/resources/js/composables/useTracking.ts @@ -1,4 +1,4 @@ -import posthog from '@/posthog'; +import { captureEvent } from '@/posthog'; const push = (data: Record) => { window.dataLayer = window.dataLayer || []; @@ -7,7 +7,7 @@ const push = (data: Record) => { export const useTracking = () => ({ trackSignUp: (authProvider: string) => { - posthog.capture('user.signed_up', { + captureEvent('user.signed_up', { auth_provider: authProvider, }); @@ -18,7 +18,7 @@ export const useTracking = () => ({ }, trackBeginCheckout: (plan: { name: string; interval: string }) => { - posthog.capture('checkout.started', { + captureEvent('checkout.started', { plan_name: plan.name, interval: plan.interval, }); @@ -31,7 +31,7 @@ export const useTracking = () => ({ }, trackPurchase: (plan: { name: string; interval: string }) => { - posthog.capture('checkout.completed', { + captureEvent('checkout.completed', { plan_name: plan.name, interval: plan.interval, }); diff --git a/resources/js/posthog.ts b/resources/js/posthog.ts index f6532b25..6662195d 100644 --- a/resources/js/posthog.ts +++ b/resources/js/posthog.ts @@ -38,7 +38,7 @@ export const initializePostHog = (): void => { * - Workspace switches re-attach the right `workspace` group (the * navigation event carries the fresh `auth.currentWorkspace`). * - * Hierarchy mirrors the backend `app/Jobs/SyncUserToPostHog.php`: + * Hierarchy mirrors the backend `app/Jobs/PostHog/SyncUser.php`: * person → User, group `account` → billing/plan parent, * group `workspace` → collaboration child (carries `account_id`). */ @@ -90,4 +90,17 @@ export const capturePageview = (): void => { posthog.capture('$pageview', { $current_url: window.location.href }); }; +/** + * Gated wrapper around `posthog.capture` for arbitrary domain events. Use + * this from composables/components instead of calling `posthog.capture` + * directly so self-hosted (or otherwise disabled) installs never queue + * events into the SDK buffer. + */ +export const captureEvent = (event: string, properties?: Record): void => { + if (!enabled) { + return; + } + posthog.capture(event, properties); +}; + export default posthog; diff --git a/tests/Feature/Jobs/PostHog/SyncUserTest.php b/tests/Feature/Jobs/PostHog/SyncUserTest.php index ec4fcd41..ad1ef06a 100644 --- a/tests/Feature/Jobs/PostHog/SyncUserTest.php +++ b/tests/Feature/Jobs/PostHog/SyncUserTest.php @@ -49,8 +49,8 @@ Queue::assertPushed(SendEvent::class, function ($job) { return $job->method === 'identify' && $job->payload['distinctId'] === (string) $this->user->id - && $job->payload['properties']['email'] === $this->user->email - && $job->payload['properties']['name'] === $this->user->name + && $job->payload['properties']['$email'] === $this->user->email + && $job->payload['properties']['$name'] === $this->user->name && isset($job->payload['properties']['$set_once']['signed_up_at']); }); }); diff --git a/tests/Feature/Jobs/PostHog/TrackBillingTest.php b/tests/Feature/Jobs/PostHog/TrackBillingTest.php index 793043f7..5e0e941b 100644 --- a/tests/Feature/Jobs/PostHog/TrackBillingTest.php +++ b/tests/Feature/Jobs/PostHog/TrackBillingTest.php @@ -103,7 +103,7 @@ (new TrackBilling((string) $this->account->id, BillingEvent::Created, $this->payload)) ->handle(app(PostHogService::class)); - // SyncUser can still be queued but it would itself no-op when handled. - // The contract of this job is: no PostHog network call without a key. + // The contract of this job is: when PostHog is disabled, handle short- + // circuits before any DB query and no SendEvent reaches the queue. Queue::assertNotPushed(SendEvent::class); }); diff --git a/tests/Feature/Listeners/StripeEventListenerTest.php b/tests/Feature/Listeners/StripeEventListenerTest.php index 92b4c7cb..9caceea9 100644 --- a/tests/Feature/Listeners/StripeEventListenerTest.php +++ b/tests/Feature/Listeners/StripeEventListenerTest.php @@ -183,6 +183,9 @@ }); test('subscription deleted dispatches TrackBilling with subscription.cancelled event', function () { + $starter = Plan::query()->where('slug', 'starter')->firstOrFail(); + $this->account->update(['plan_id' => $starter->id]); + Bus::fake([TrackBilling::class]); $this->listener->handle(new WebhookReceived([ @@ -330,12 +333,18 @@ test('subscription deleted is idempotent when plan_id is already null', function () { $this->account->update(['plan_id' => null]); + Bus::fake([TrackBilling::class]); + $this->listener->handle(new WebhookReceived([ 'type' => 'customer.subscription.deleted', 'data' => ['object' => ['customer' => 'cus_test123']], ])); expect($this->account->fresh()->plan_id)->toBeNull(); + + // Re-delivery of a cancellation must not produce a duplicate + // 'subscription.cancelled' event downstream. + Bus::assertNotDispatched(TrackBilling::class); }); // ======================================== diff --git a/tests/Feature/Models/AccountModelTest.php b/tests/Feature/Models/AccountModelTest.php index 100751df..f0c3a4e0 100644 --- a/tests/Feature/Models/AccountModelTest.php +++ b/tests/Feature/Models/AccountModelTest.php @@ -39,24 +39,3 @@ expect(Feature::for($account)->value(MemberLimit::class))->toBe($plus->member_limit); expect(Feature::for($account)->value(MonthlyCreditsLimit::class))->toBe($plus->monthly_credits_limit); }); - -test('updating non-plan fields does not flush the pennant cache', function () { - $plan = Plan::where('slug', 'plus')->first(); - $account = Account::factory()->create(['plan_id' => $plan->id]); - - Feature::for($account)->value(WorkspaceLimit::class); - - $cachedRow = DB::table('features') - ->where('scope', 'account|'.$account->id) - ->first(); - - expect($cachedRow)->not->toBeNull(); - - $account->update(['name' => 'Updated Name']); - - $stillCachedRow = DB::table('features') - ->where('scope', 'account|'.$account->id) - ->first(); - - expect($stillCachedRow->id)->toBe($cachedRow->id); -});