Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

feat: capture ad click IDs for ad-platform conversion attribution (#276)

Paulo Castellanoca1e34622761
diff --git a/app/Actions/User/CreateUser.php b/app/Actions/User/CreateUser.php
index ead5eca04..d54e5a4f7 100644
--- a/app/Actions/User/CreateUser.php
+++ b/app/Actions/User/CreateUser.php
@@ -17,11 +17,11 @@ class CreateUser
{
/**
* @param array{name: string, email: string, password?: string, google_id?: string, github_id?: string, email_verified_at?: \DateTimeInterface|null, is_invite?: bool, registration_ip?: string|null} $data
- * @param array<string, string> $utmParameters
+ * @param array<string, string> $attributionParameters UTM parameters and ad click IDs (gclid, fbclid, etc.) captured before signup
*/
- public static function execute(array $data, array $utmParameters = []): User
+ public static function execute(array $data, array $attributionParameters = []): User
{
- $user = DB::transaction(function () use ($data, $utmParameters): User {
+ $user = DB::transaction(function () use ($data, $attributionParameters): User {
$isInviteRegistration = data_get($data, 'is_invite', false);
$requiresCardForTrial = (bool) config('trypost.billing.require_card_for_trial', true);
$accountAttributes = [
@@ -45,7 +45,7 @@ class CreateUser
'email_verified_at' => data_get($data, 'email_verified_at', $isInviteRegistration ? now() : null),
'account_id' => $account->id,
'registration_ip' => data_get($data, 'registration_ip'),
- ], $utmParameters));
+ ], $attributionParameters));
$account->update(['owner_id' => $user->id]);
diff --git a/app/Http/Controllers/Auth/Concerns/PreservesAttributionParameters.php b/app/Http/Controllers/Auth/Concerns/PreservesAttributionParameters.php
new file mode 100644
index 000000000..27e2f1cfb
--- /dev/null
+++ b/app/Http/Controllers/Auth/Concerns/PreservesAttributionParameters.php
@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Http\Controllers\Auth\Concerns;
+
+use App\Support\AttributionKeys;
+use Illuminate\Http\Request;
+use Illuminate\Support\Str;
+
+trait PreservesAttributionParameters
+{
+ /**
+ * UTM values are ours (our own campaign URLs), so truncating them to a
+ * safe length is fine. Click IDs are opaque tokens assigned by the ad
+ * platform — Google explicitly warns never to truncate or assume a
+ * fixed max length for gclid, since it has already grown over time. The
+ * `gclid`/etc. columns are `text`, so there's no need to cap them here.
+ *
+ * A key present with an empty value (e.g. `?utm_source=&gclid=`, which
+ * some ad/email templates always append) is filtered out along with
+ * absent keys, so it never overwrites a null column with `''`.
+ *
+ * @return array<string, string>
+ */
+ private function extractAttributionParameters(Request $request): array
+ {
+ $utm = collect($request->only(AttributionKeys::UTM))
+ ->filter(fn ($value) => is_string($value))
+ ->map(fn (string $value) => Str::limit($value, 255, ''));
+
+ $clickIds = collect($request->only(AttributionKeys::CLICK_ID))
+ ->filter(fn ($value) => is_string($value));
+
+ return $utm->merge($clickIds)->filter()->all();
+ }
+
+ private function storeAttributionParameters(Request $request): void
+ {
+ $request->session()->put('attribution_parameters', $this->extractAttributionParameters($request));
+ }
+
+ /**
+ * @return array<string, string>
+ */
+ private function retrieveAttributionParameters(): array
+ {
+ return session()->pull('attribution_parameters', []);
+ }
+}
diff --git a/app/Http/Controllers/Auth/Concerns/PreservesUtmParameters.php b/app/Http/Controllers/Auth/Concerns/PreservesUtmParameters.php
deleted file mode 100644
index 90b174c4a..000000000
--- a/app/Http/Controllers/Auth/Concerns/PreservesUtmParameters.php
+++ /dev/null
@@ -1,48 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace App\Http\Controllers\Auth\Concerns;
-
-use Illuminate\Http\Request;
-
-trait PreservesUtmParameters
-{
- private const array UTM_KEYS = [
- 'utm_source',
- 'utm_medium',
- 'utm_campaign',
- 'utm_term',
- 'utm_content',
- ];
-
- /**
- * @return array<string, string>
- */
- private function extractUtmParameters(Request $request): array
- {
- return array_filter(
- array_map(
- fn (string $value) => mb_substr($value, 0, 255),
- array_filter($request->only(self::UTM_KEYS), 'is_string'),
- ),
- );
- }
-
- private function storeUtmParameters(Request $request): void
- {
- $utms = $this->extractUtmParameters($request);
-
- if ($utms !== []) {
- $request->session()->put('utm_parameters', $utms);
- }
- }
-
- /**
- * @return array<string, string>
- */
- private function retrieveUtmParameters(): array
- {
- return session()->pull('utm_parameters', []);
- }
-}
diff --git a/app/Http/Controllers/Auth/GitHubController.php b/app/Http/Controllers/Auth/GitHubController.php
index 3b3f09ba7..5b11104d9 100644
--- a/app/Http/Controllers/Auth/GitHubController.php
+++ b/app/Http/Controllers/Auth/GitHubController.php
@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace App\Http\Controllers\Auth;
use App\Actions\User\CreateUser;
-use App\Http\Controllers\Auth\Concerns\PreservesUtmParameters;
+use App\Http\Controllers\Auth\Concerns\PreservesAttributionParameters;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
@@ -16,11 +16,11 @@ use Laravel\Socialite\Facades\Socialite;
class GitHubController extends Controller
{
- use PreservesUtmParameters;
+ use PreservesAttributionParameters;
public function redirect(Request $request): RedirectResponse
{
- $this->storeUtmParameters($request);
+ $this->storeAttributionParameters($request);
return Socialite::driver('github')
->scopes(['read:user', 'user:email'])
@@ -90,14 +90,14 @@ class GitHubController extends Controller
Auth::login($user, remember: true);
- $this->retrieveUtmParameters();
+ $this->retrieveAttributionParameters();
return redirect()->route('app.home');
}
private function registerNewUser(\Laravel\Socialite\Contracts\User $githubUser): RedirectResponse
{
- $utmParameters = $this->retrieveUtmParameters();
+ $attributionParameters = $this->retrieveAttributionParameters();
$user = CreateUser::execute([
'name' => $githubUser->getName() ?? $githubUser->getNickname() ?? explode('@', $githubUser->getEmail())[0],
@@ -105,7 +105,7 @@ class GitHubController extends Controller
'github_id' => (string) $githubUser->getId(),
'email_verified_at' => now(),
'registration_ip' => request()->ip(),
- ], $utmParameters);
+ ], $attributionParameters);
event(new Registered($user));
@@ -113,6 +113,6 @@ class GitHubController extends Controller
session()->flash('auth_provider', 'github');
- return redirect()->route('register.success', $utmParameters);
+ return redirect()->route('register.success', $attributionParameters);
}
}
diff --git a/app/Http/Controllers/Auth/GoogleController.php b/app/Http/Controllers/Auth/GoogleController.php
index f7e1b5da4..378ab9b9c 100644
--- a/app/Http/Controllers/Auth/GoogleController.php
+++ b/app/Http/Controllers/Auth/GoogleController.php
@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace App\Http\Controllers\Auth;
use App\Actions\User\CreateUser;
-use App\Http\Controllers\Auth\Concerns\PreservesUtmParameters;
+use App\Http\Controllers\Auth\Concerns\PreservesAttributionParameters;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
@@ -16,11 +16,11 @@ use Laravel\Socialite\Facades\Socialite;
class GoogleController extends Controller
{
- use PreservesUtmParameters;
+ use PreservesAttributionParameters;
public function redirect(Request $request): RedirectResponse
{
- $this->storeUtmParameters($request);
+ $this->storeAttributionParameters($request);
return Socialite::driver('google-auth')->redirect();
}
@@ -82,14 +82,14 @@ class GoogleController extends Controller
Auth::login($user, remember: true);
- $this->retrieveUtmParameters();
+ $this->retrieveAttributionParameters();
return redirect()->route('app.home');
}
private function registerNewUser(\Laravel\Socialite\Contracts\User $googleUser): RedirectResponse
{
- $utmParameters = $this->retrieveUtmParameters();
+ $attributionParameters = $this->retrieveAttributionParameters();
$user = CreateUser::execute([
'name' => $googleUser->getName(),
@@ -97,7 +97,7 @@ class GoogleController extends Controller
'google_id' => $googleUser->getId(),
'email_verified_at' => now(),
'registration_ip' => request()->ip(),
- ], $utmParameters);
+ ], $attributionParameters);
event(new Registered($user));
@@ -105,6 +105,6 @@ class GoogleController extends Controller
session()->flash('auth_provider', 'google');
- return redirect()->route('register.success', $utmParameters);
+ return redirect()->route('register.success', $attributionParameters);
}
}
diff --git a/app/Http/Controllers/Auth/RegisteredUserController.php b/app/Http/Controllers/Auth/RegisteredUserController.php
index 8b860a908..273f9f560 100644
--- a/app/Http/Controllers/Auth/RegisteredUserController.php
+++ b/app/Http/Controllers/Auth/RegisteredUserController.php
@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace App\Http\Controllers\Auth;
use App\Actions\User\CreateUser;
-use App\Http\Controllers\Auth\Concerns\PreservesUtmParameters;
+use App\Http\Controllers\Auth\Concerns\PreservesAttributionParameters;
use App\Http\Controllers\Controller;
use App\Http\Requests\App\Auth\RegisterRequest;
use Illuminate\Auth\Events\Registered;
@@ -17,11 +17,11 @@ use Inertia\Response;
class RegisteredUserController extends Controller
{
- use PreservesUtmParameters;
+ use PreservesAttributionParameters;
public function create(Request $request): Response
{
- $this->storeUtmParameters($request);
+ $this->storeAttributionParameters($request);
return Inertia::render('auth/Register', [
'email' => $request->query('email'),
@@ -32,7 +32,7 @@ class RegisteredUserController extends Controller
public function store(RegisterRequest $request): RedirectResponse
{
- $utmParameters = $this->retrieveUtmParameters();
+ $attributionParameters = $this->retrieveAttributionParameters();
$user = CreateUser::execute([
'name' => $request->validated('name'),
@@ -40,7 +40,7 @@ class RegisteredUserController extends Controller
'password' => $request->validated('password'),
'is_invite' => $request->isInviteRegistration(),
'registration_ip' => $request->ip(),
- ], $utmParameters);
+ ], $attributionParameters);
event(new Registered($user));
@@ -56,6 +56,6 @@ class RegisteredUserController extends Controller
session()->flash('auth_provider', 'email');
- return redirect()->route('register.success', $utmParameters);
+ return redirect()->route('register.success', $attributionParameters);
}
}
diff --git a/app/Jobs/PostHog/SyncUser.php b/app/Jobs/PostHog/SyncUser.php
index c1d355c36..648168077 100644
--- a/app/Jobs/PostHog/SyncUser.php
+++ b/app/Jobs/PostHog/SyncUser.php
@@ -6,6 +6,7 @@ namespace App\Jobs\PostHog;
use App\Models\User;
use App\Services\PostHogService;
+use App\Support\AttributionKeys;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -37,10 +38,18 @@ class SyncUser implements ShouldQueue
return;
}
+ $attribution = collect(AttributionKeys::all())
+ ->mapWithKeys(fn (string $key) => [$key => $user->{$key}])
+ ->filter()
+ ->all();
+
$postHog->identify($user->id, [
'$email' => $user->email,
'$name' => $user->name,
- '$set_once' => ['signed_up_at' => $user->created_at?->toIso8601String()],
+ '$set_once' => [
+ 'signed_up_at' => $user->created_at?->toIso8601String(),
+ ...$attribution,
+ ],
]);
if ($user->account_id) {
diff --git a/app/Models/User.php b/app/Models/User.php
index dd9795dbb..95788b565 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -44,6 +44,12 @@ class User extends Authenticatable implements MustVerifyEmail, OAuthenticatable
'utm_campaign',
'utm_term',
'utm_content',
+ 'gclid',
+ 'fbclid',
+ 'li_fat_id',
+ 'ttclid',
+ 'rdt_cid',
+ 'epik',
'registration_ip',
'persona',
'goals',
diff --git a/app/Support/AttributionKeys.php b/app/Support/AttributionKeys.php
new file mode 100644
index 000000000..c7e2e8547
--- /dev/null
+++ b/app/Support/AttributionKeys.php
@@ -0,0 +1,44 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Support;
+
+/**
+ * UTM parameters and ad click IDs captured before signup (query string ->
+ * session -> persisted on the users row), shared by
+ * App\Http\Controllers\Auth\Concerns\PreservesAttributionParameters (capture)
+ * and App\Jobs\PostHog\SyncUser (forwarding to PostHog).
+ */
+final class AttributionKeys
+{
+ public const array UTM = [
+ 'utm_source',
+ 'utm_medium',
+ 'utm_campaign',
+ 'utm_term',
+ 'utm_content',
+ ];
+
+ /**
+ * Per-platform ad click IDs (Google, Meta, LinkedIn, TikTok, Reddit,
+ * Pinterest). Adding a new ad network's click ID is just one more key
+ * here.
+ */
+ public const array CLICK_ID = [
+ 'gclid',
+ 'fbclid',
+ 'li_fat_id',
+ 'ttclid',
+ 'rdt_cid',
+ 'epik',
+ ];
+
+ /**
+ * @return list<string>
+ */
+ public static function all(): array
+ {
+ return [...self::UTM, ...self::CLICK_ID];
+ }
+}
diff --git a/database/migrations/2026_08_11_171700_add_click_id_columns_to_users_table.php b/database/migrations/2026_08_11_171700_add_click_id_columns_to_users_table.php
new file mode 100644
index 000000000..ff97aa229
--- /dev/null
+++ b/database/migrations/2026_08_11_171700_add_click_id_columns_to_users_table.php
@@ -0,0 +1,29 @@
+<?php
+
+declare(strict_types=1);
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ public function up(): void
+ {
+ Schema::table('users', function (Blueprint $table): void {
+ $table->text('gclid')->nullable()->after('utm_content');
+ $table->text('fbclid')->nullable()->after('gclid');
+ $table->text('li_fat_id')->nullable()->after('fbclid');
+ $table->text('ttclid')->nullable()->after('li_fat_id');
+ $table->text('rdt_cid')->nullable()->after('ttclid');
+ $table->text('epik')->nullable()->after('rdt_cid');
+ });
+ }
+
+ public function down(): void
+ {
+ Schema::table('users', function (Blueprint $table): void {
+ $table->dropColumn(['gclid', 'fbclid', 'li_fat_id', 'ttclid', 'rdt_cid', 'epik']);
+ });
+ }
+};
diff --git a/tests/Feature/Auth/SignupClickIdTrackingTest.php b/tests/Feature/Auth/SignupClickIdTrackingTest.php
new file mode 100644
index 000000000..e6fa55410
--- /dev/null
+++ b/tests/Feature/Auth/SignupClickIdTrackingTest.php
@@ -0,0 +1,215 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Models\User;
+use Laravel\Socialite\Facades\Socialite;
+use Laravel\Socialite\Two\User as SocialiteUser;
+
+beforeEach(fn () => config()->set('trypost.self_hosted', false));
+
+test('email registration saves ad click ids from the register page query string', function () {
+ $clickIds = [
+ 'gclid' => 'gclid-value',
+ 'fbclid' => 'fbclid-value',
+ 'li_fat_id' => 'li-fat-id-value',
+ 'ttclid' => 'ttclid-value',
+ 'rdt_cid' => 'rdt-cid-value',
+ 'epik' => 'epik-value',
+ ];
+
+ $this->get(route('register', $clickIds));
+
+ $this->post(route('register.store'), [
+ 'name' => 'Click User',
+ 'email' => '[email protected]',
+ 'password' => 'Password123!',
+ ])
+ ->assertRedirect(route('register.success', $clickIds, absolute: false));
+
+ $this->assertDatabaseHas('users', [
+ 'email' => '[email protected]',
+ ...$clickIds,
+ ]);
+});
+
+test('email registration without click ids saves null columns', function () {
+ $this->post(route('register.store'), [
+ 'name' => 'No Click User',
+ 'email' => '[email protected]',
+ 'password' => 'Password123!',
+ ]);
+
+ $this->assertDatabaseHas('users', [
+ 'email' => '[email protected]',
+ 'gclid' => null,
+ 'fbclid' => null,
+ 'li_fat_id' => null,
+ 'ttclid' => null,
+ 'rdt_cid' => null,
+ 'epik' => null,
+ ]);
+});
+
+test('click id values longer than 255 characters are stored in full, unlike utm parameters', function () {
+ $longValue = str_repeat('a', 300);
+
+ $this->get(route('register', ['gclid' => $longValue]));
+
+ $this->post(route('register.store'), [
+ 'name' => 'Long Click User',
+ 'email' => '[email protected]',
+ 'password' => 'Password123!',
+ ]);
+
+ $user = User::where('email', '[email protected]')->first();
+
+ expect($user->gclid)->toBe($longValue);
+});
+
+test('google registration saves ad click ids captured before the oauth round-trip', function () {
+ $clickIds = ['gclid' => 'g-click', 'fbclid' => 'fb-click'];
+
+ $this->get(route('auth.google.redirect', $clickIds));
+
+ $socialiteUser = new SocialiteUser;
+ $socialiteUser->id = 'g-click-user';
+ $socialiteUser->name = 'Google Click';
+ $socialiteUser->email = '[email protected]';
+
+ Socialite::shouldReceive('driver')
+ ->with('google-auth')
+ ->andReturn($driver = Mockery::mock());
+
+ $driver->shouldReceive('user')
+ ->andReturn($socialiteUser);
+
+ $this->get(route('auth.google.callback'))
+ ->assertRedirect(route('register.success', $clickIds, absolute: false));
+
+ $this->assertDatabaseHas('users', [
+ 'email' => '[email protected]',
+ ...$clickIds,
+ ]);
+});
+
+test('github registration saves ad click ids captured before the oauth round-trip', function () {
+ $clickIds = ['li_fat_id' => 'li-click', 'ttclid' => 'tt-click'];
+
+ $this->get(route('auth.github.redirect', $clickIds));
+
+ $socialiteUser = new SocialiteUser;
+ $socialiteUser->id = 'gh-click-user';
+ $socialiteUser->name = 'GitHub Click';
+ $socialiteUser->email = '[email protected]';
+
+ Socialite::shouldReceive('driver')
+ ->with('github')
+ ->andReturn($driver = Mockery::mock());
+
+ $driver->shouldReceive('scopes')
+ ->andReturnSelf();
+
+ $driver->shouldReceive('user')
+ ->andReturn($socialiteUser);
+
+ $this->get(route('auth.github.callback'))
+ ->assertRedirect(route('register.success', $clickIds, absolute: false));
+
+ $this->assertDatabaseHas('users', [
+ 'email' => '[email protected]',
+ ...$clickIds,
+ ]);
+});
+
+test('existing google user login consumes the click id session so it does not leak to a later signup', function () {
+ User::factory()->create([
+ 'email' => '[email protected]',
+ 'google_id' => 'g-existing-click',
+ ]);
+
+ $this->get(route('auth.google.redirect', ['gclid' => 'stale-click']));
+
+ $socialiteUser = new SocialiteUser;
+ $socialiteUser->id = 'g-existing-click';
+ $socialiteUser->name = 'Existing Click User';
+ $socialiteUser->email = '[email protected]';
+
+ Socialite::shouldReceive('driver')
+ ->with('google-auth')
+ ->andReturn($driver = Mockery::mock());
+
+ $driver->shouldReceive('user')
+ ->andReturn($socialiteUser);
+
+ $this->get(route('auth.google.callback'))
+ ->assertRedirect(route('app.home'));
+
+ expect(session()->get('attribution_parameters'))->toBeNull();
+});
+
+test('existing github user login consumes the click id session so it does not leak to a later signup', function () {
+ User::factory()->create([
+ 'email' => '[email protected]',
+ 'github_id' => 'gh-existing-click',
+ ]);
+
+ $this->get(route('auth.github.redirect', ['gclid' => 'stale-gh-click']));
+
+ $socialiteUser = new SocialiteUser;
+ $socialiteUser->id = 'gh-existing-click';
+ $socialiteUser->name = 'Existing GitHub Click User';
+ $socialiteUser->email = '[email protected]';
+
+ Socialite::shouldReceive('driver')
+ ->with('github')
+ ->andReturn($driver = Mockery::mock());
+
+ $driver->shouldReceive('scopes')
+ ->andReturnSelf();
+
+ $driver->shouldReceive('user')
+ ->andReturn($socialiteUser);
+
+ $this->get(route('auth.github.callback'))
+ ->assertRedirect(route('app.home'));
+
+ expect(session()->get('attribution_parameters'))->toBeNull();
+});
+
+test('an empty query string value is treated the same as an absent one, not stored as an empty string', function () {
+ // Some ad/email templates always append every tracking key, leaving
+ // unfilled slots blank (?utm_source=&gclid=) instead of omitting them.
+ $this->get(route('register', ['utm_source' => '', 'gclid' => '']));
+
+ $this->post(route('register.store'), [
+ 'name' => 'Empty Param User',
+ 'email' => '[email protected]',
+ 'password' => 'Password123!',
+ ]);
+
+ $this->assertDatabaseHas('users', [
+ 'email' => '[email protected]',
+ 'utm_source' => null,
+ 'gclid' => null,
+ ]);
+});
+
+test('click ids and utm parameters are both saved when present together', function () {
+ $this->get(route('register', [
+ 'utm_source' => 'google',
+ 'gclid' => 'mixed-click',
+ ]));
+
+ $this->post(route('register.store'), [
+ 'name' => 'Mixed User',
+ 'email' => '[email protected]',
+ 'password' => 'Password123!',
+ ]);
+
+ $this->assertDatabaseHas('users', [
+ 'email' => '[email protected]',
+ 'utm_source' => 'google',
+ 'gclid' => 'mixed-click',
+ ]);
+});
diff --git a/tests/Feature/Auth/SignupUtmTrackingTest.php b/tests/Feature/Auth/SignupUtmTrackingTest.php
index 9a80e10e3..075099af3 100644
--- a/tests/Feature/Auth/SignupUtmTrackingTest.php
+++ b/tests/Feature/Auth/SignupUtmTrackingTest.php
@@ -146,7 +146,7 @@ test('existing google user login consumes the utm session so utms do not leak to
$this->get(route('auth.google.callback'))
->assertRedirect(route('app.home'));
- expect(session()->get('utm_parameters'))->toBeNull();
+ expect(session()->get('attribution_parameters'))->toBeNull();
});
test('invitation registration does not include utm parameters in its redirect', function () {
@@ -317,5 +317,5 @@ test('existing github user login consumes the utm session and skips signup succe
$this->get(route('auth.github.callback'))
->assertRedirect(route('app.home'));
- expect(session()->get('utm_parameters'))->toBeNull();
+ expect(session()->get('attribution_parameters'))->toBeNull();
});
diff --git a/tests/Feature/Jobs/PostHog/SyncUserTest.php b/tests/Feature/Jobs/PostHog/SyncUserTest.php
index 9e5dc2520..075ea845b 100644
--- a/tests/Feature/Jobs/PostHog/SyncUserTest.php
+++ b/tests/Feature/Jobs/PostHog/SyncUserTest.php
@@ -54,6 +54,53 @@ test('handle identifies the user with email, name and signed_up_at', function ()
});
});
+test('handle forwards ad click ids as first-touch person properties when present', function () {
+ $this->user->update(['gclid' => 'g-123', 'fbclid' => 'fb-456']);
+ Queue::fake();
+
+ (new SyncUser((string) $this->user->id))->handle(app(PostHogService::class));
+
+ Queue::assertPushed(SendEvent::class, function ($job) {
+ return $job->payload['properties']['$set_once']['gclid'] === 'g-123'
+ && $job->payload['properties']['$set_once']['fbclid'] === 'fb-456';
+ });
+});
+
+test('handle forwards utm parameters as first-touch person properties when present', function () {
+ $this->user->update(['utm_source' => 'google', 'utm_medium' => 'cpc', 'utm_campaign' => 'spring-launch']);
+ Queue::fake();
+
+ (new SyncUser((string) $this->user->id))->handle(app(PostHogService::class));
+
+ Queue::assertPushed(SendEvent::class, function ($job) {
+ return $job->payload['properties']['$set_once']['utm_source'] === 'google'
+ && $job->payload['properties']['$set_once']['utm_medium'] === 'cpc'
+ && $job->payload['properties']['$set_once']['utm_campaign'] === 'spring-launch';
+ });
+});
+
+test('handle omits attribution properties when none are set', function () {
+ Queue::fake();
+
+ (new SyncUser((string) $this->user->id))->handle(app(PostHogService::class));
+
+ Queue::assertPushed(SendEvent::class, function ($job) {
+ $setOnce = $job->payload['properties']['$set_once'];
+
+ return ! array_key_exists('utm_source', $setOnce)
+ && ! array_key_exists('utm_medium', $setOnce)
+ && ! array_key_exists('utm_campaign', $setOnce)
+ && ! array_key_exists('utm_term', $setOnce)
+ && ! array_key_exists('utm_content', $setOnce)
+ && ! array_key_exists('gclid', $setOnce)
+ && ! array_key_exists('fbclid', $setOnce)
+ && ! array_key_exists('li_fat_id', $setOnce)
+ && ! array_key_exists('ttclid', $setOnce)
+ && ! array_key_exists('rdt_cid', $setOnce)
+ && ! array_key_exists('epik', $setOnce);
+ });
+});
+
test('handle dispatches SyncAccountUsage with the user account id', function () {
Queue::fake();