Capture post.created to PostHog with created_via.
Dispatch TrackPost from PostCreated so we can chart which entry points create the most posts. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
227ee45c2b
commit
2c50a60790
5 changed files with 265 additions and 0 deletions
10
app/Enums/PostHog/PostEvent.php
Normal file
10
app/Enums/PostHog/PostEvent.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Enums\PostHog;
|
||||
|
||||
enum PostEvent: string
|
||||
{
|
||||
case Created = 'post.created';
|
||||
}
|
||||
67
app/Jobs/PostHog/TrackPost.php
Normal file
67
app/Jobs/PostHog/TrackPost.php
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Jobs\PostHog;
|
||||
|
||||
use App\Enums\PostHog\PostEvent;
|
||||
use App\Models\Post;
|
||||
use App\Services\PostHogService;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class TrackPost implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public int $tries = 3;
|
||||
|
||||
public int $timeout = 30;
|
||||
|
||||
public function __construct(public string $postId)
|
||||
{
|
||||
$this->onQueue('posthog');
|
||||
}
|
||||
|
||||
public function handle(PostHogService $postHog): void
|
||||
{
|
||||
if (! PostHogService::isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post = Post::query()
|
||||
->with(['workspace.account.plan', 'user'])
|
||||
->find($this->postId);
|
||||
|
||||
if (! $post) {
|
||||
return;
|
||||
}
|
||||
|
||||
$account = $post->workspace?->account;
|
||||
|
||||
if (! $account) {
|
||||
return;
|
||||
}
|
||||
|
||||
$distinctId = (string) ($post->user_id ?? $account->owner_id);
|
||||
|
||||
if ($distinctId === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$postHog->capture(
|
||||
$distinctId,
|
||||
PostEvent::Created->value,
|
||||
[
|
||||
'post_id' => (string) $post->id,
|
||||
'workspace_id' => (string) $post->workspace_id,
|
||||
'created_via' => $post->created_via?->value,
|
||||
'status' => $post->status->value,
|
||||
],
|
||||
$account,
|
||||
);
|
||||
}
|
||||
}
|
||||
21
app/Listeners/PostHog/TrackPostCreated.php
Normal file
21
app/Listeners/PostHog/TrackPostCreated.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Listeners\PostHog;
|
||||
|
||||
use App\Events\PostCreated;
|
||||
use App\Jobs\PostHog\TrackPost;
|
||||
use App\Services\PostHogService;
|
||||
|
||||
class TrackPostCreated
|
||||
{
|
||||
public function handle(PostCreated $event): void
|
||||
{
|
||||
if (! PostHogService::isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
TrackPost::dispatch((string) $event->post->id);
|
||||
}
|
||||
}
|
||||
96
tests/Feature/Jobs/PostHog/TrackPostTest.php
Normal file
96
tests/Feature/Jobs/PostHog/TrackPostTest.php
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Enums\Post\CreatedVia;
|
||||
use App\Enums\PostHog\PostEvent;
|
||||
use App\Jobs\PostHog\SendEvent;
|
||||
use App\Jobs\PostHog\TrackPost;
|
||||
use App\Models\Account;
|
||||
use App\Models\Post;
|
||||
use App\Models\User;
|
||||
use App\Models\Workspace;
|
||||
use App\Services\PostHogService;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
beforeEach(function () {
|
||||
config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);
|
||||
|
||||
$this->account = Account::factory()->create();
|
||||
$this->user = User::factory()->create(['account_id' => $this->account->id]);
|
||||
$this->account->update(['owner_id' => $this->user->id]);
|
||||
$this->workspace = Workspace::factory()->create([
|
||||
'account_id' => $this->account->id,
|
||||
'user_id' => $this->user->id,
|
||||
]);
|
||||
});
|
||||
|
||||
test('job is queued on the posthog queue', function () {
|
||||
$job = new TrackPost((string) Str::uuid());
|
||||
|
||||
expect($job->queue)->toBe('posthog');
|
||||
});
|
||||
|
||||
test('handle captures post.created with created_via and account group', function () {
|
||||
Queue::fake();
|
||||
|
||||
$post = Post::factory()->create([
|
||||
'workspace_id' => $this->workspace->id,
|
||||
'user_id' => $this->user->id,
|
||||
'created_via' => CreatedVia::Api,
|
||||
]);
|
||||
|
||||
(new TrackPost((string) $post->id))->handle(app(PostHogService::class));
|
||||
|
||||
Queue::assertPushed(SendEvent::class, function ($job) use ($post) {
|
||||
return $job->method === 'capture'
|
||||
&& $job->payload['event'] === PostEvent::Created->value
|
||||
&& $job->payload['distinctId'] === (string) $this->user->id
|
||||
&& $job->payload['properties']['post_id'] === (string) $post->id
|
||||
&& $job->payload['properties']['workspace_id'] === (string) $this->workspace->id
|
||||
&& $job->payload['properties']['created_via'] === CreatedVia::Api->value
|
||||
&& $job->payload['properties']['$groups']['account'] === (string) $this->account->id;
|
||||
});
|
||||
});
|
||||
|
||||
test('handle falls back to account owner when post has no user', function () {
|
||||
Queue::fake();
|
||||
|
||||
$post = Post::factory()->create([
|
||||
'workspace_id' => $this->workspace->id,
|
||||
'user_id' => null,
|
||||
'created_via' => CreatedVia::Mcp,
|
||||
]);
|
||||
|
||||
(new TrackPost((string) $post->id))->handle(app(PostHogService::class));
|
||||
|
||||
Queue::assertPushed(
|
||||
SendEvent::class,
|
||||
fn ($job) => $job->payload['distinctId'] === (string) $this->user->id
|
||||
&& $job->payload['properties']['created_via'] === CreatedVia::Mcp->value,
|
||||
);
|
||||
});
|
||||
|
||||
test('handle returns silently when post does not exist', function () {
|
||||
Queue::fake();
|
||||
|
||||
(new TrackPost((string) Str::uuid()))->handle(app(PostHogService::class));
|
||||
|
||||
Queue::assertNothingPushed();
|
||||
});
|
||||
|
||||
test('handle does not push when PostHog is disabled', function () {
|
||||
config(['services.posthog.api_key' => null]);
|
||||
Queue::fake();
|
||||
|
||||
$post = Post::factory()->create([
|
||||
'workspace_id' => $this->workspace->id,
|
||||
'user_id' => $this->user->id,
|
||||
'created_via' => CreatedVia::Web,
|
||||
]);
|
||||
|
||||
(new TrackPost((string) $post->id))->handle(app(PostHogService::class));
|
||||
|
||||
Queue::assertNotPushed(SendEvent::class);
|
||||
});
|
||||
71
tests/Feature/Listeners/PostHog/TrackPostCreatedTest.php
Normal file
71
tests/Feature/Listeners/PostHog/TrackPostCreatedTest.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Enums\Post\CreatedVia;
|
||||
use App\Events\PostCreated;
|
||||
use App\Jobs\PostHog\TrackPost;
|
||||
use App\Listeners\PostHog\TrackPostCreated;
|
||||
use App\Models\Account;
|
||||
use App\Models\Post;
|
||||
use App\Models\User;
|
||||
use App\Models\Workspace;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
|
||||
beforeEach(function () {
|
||||
config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);
|
||||
|
||||
$this->account = Account::factory()->create();
|
||||
$this->user = User::factory()->create(['account_id' => $this->account->id]);
|
||||
$this->account->update(['owner_id' => $this->user->id]);
|
||||
$this->workspace = Workspace::factory()->create([
|
||||
'account_id' => $this->account->id,
|
||||
'user_id' => $this->user->id,
|
||||
]);
|
||||
});
|
||||
|
||||
test('listener dispatches TrackPost with the post id', function () {
|
||||
$post = Post::factory()->create([
|
||||
'workspace_id' => $this->workspace->id,
|
||||
'user_id' => $this->user->id,
|
||||
'created_via' => CreatedVia::Web,
|
||||
]);
|
||||
|
||||
Bus::fake();
|
||||
|
||||
(new TrackPostCreated)->handle(new PostCreated($post));
|
||||
|
||||
Bus::assertDispatched(
|
||||
TrackPost::class,
|
||||
fn ($job) => $job->postId === (string) $post->id,
|
||||
);
|
||||
});
|
||||
|
||||
test('listener is wired to the PostCreated event via auto-discovery', function () {
|
||||
$post = Post::factory()->create([
|
||||
'workspace_id' => $this->workspace->id,
|
||||
'user_id' => $this->user->id,
|
||||
'created_via' => CreatedVia::Automation,
|
||||
]);
|
||||
|
||||
Bus::fake();
|
||||
|
||||
PostCreated::dispatch($post);
|
||||
|
||||
Bus::assertDispatched(TrackPost::class);
|
||||
});
|
||||
|
||||
test('listener does not dispatch when PostHog is disabled', function () {
|
||||
config(['services.posthog.enabled' => false]);
|
||||
|
||||
$post = Post::factory()->create([
|
||||
'workspace_id' => $this->workspace->id,
|
||||
'user_id' => $this->user->id,
|
||||
]);
|
||||
|
||||
Bus::fake();
|
||||
|
||||
(new TrackPostCreated)->handle(new PostCreated($post));
|
||||
|
||||
Bus::assertNotDispatched(TrackPost::class);
|
||||
});
|
||||
Loading…
Reference in a new issue