Onboarding/lifecycle workflows in PostHog (and downstream tools like SendKit) need to segment users by how many social accounts they've connected and how many posts they've created. The existing SyncUser job only re-emitted these counts on signup and billing changes, so the values went stale the moment a user did anything meaningful. This wires up two new paths that refresh the account group automatically: - SocialAccountObserver (#[ObservedBy] on the model) fires SyncAccountUsage on created/deleted, covering all 14 OAuth callback paths in one hook. - SyncUsageOnPostCreated / SyncUsageOnPostDeleted listeners (auto-discovered) fire SyncAccountUsage on the corresponding events dispatched by CreatePost and DeletePost. SyncAccountUsage is the new dedicated job for group properties only (groupIdentify account + workspace). SyncUser was slimmed to just identify the user and delegate the group sync, removing the duplicated property mapping between the two jobs. All entry points (observer + both listeners) short-circuit when PostHog is disabled, so self-hosted instances without PostHog configured see zero queued jobs and zero overhead. posts_count cache is invalidated before each sync so the job reads fresh counts from the database instead of stale cached values.
34 lines
745 B
PHP
34 lines
745 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Jobs\PostHog\SyncAccountUsage;
|
|
use App\Models\SocialAccount;
|
|
use App\Services\PostHogService;
|
|
|
|
class SocialAccountObserver
|
|
{
|
|
public function created(SocialAccount $socialAccount): void
|
|
{
|
|
$this->syncUsage($socialAccount);
|
|
}
|
|
|
|
public function deleted(SocialAccount $socialAccount): void
|
|
{
|
|
$this->syncUsage($socialAccount);
|
|
}
|
|
|
|
private function syncUsage(SocialAccount $socialAccount): void
|
|
{
|
|
if (! PostHogService::isEnabled()) {
|
|
return;
|
|
}
|
|
|
|
SyncAccountUsage::dispatch(
|
|
(string) $socialAccount->workspace->account_id,
|
|
(string) $socialAccount->workspace_id,
|
|
);
|
|
}
|
|
}
|