Skip to content

fix(next): preserve existing referral cookie in trackReferralCode (fixes #3) - #6

Merged
ralyodio merged 1 commit into
profullstack:masterfrom
Echolonius:fix/track-referral-cookie-first-touch
Aug 1, 2026
Merged

fix(next): preserve existing referral cookie in trackReferralCode (fixes #3)#6
ralyodio merged 1 commit into
profullstack:masterfrom
Echolonius:fix/track-referral-cookie-first-touch

Conversation

@Echolonius

@Echolonius Echolonius commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #3 by checking whether a referral cookie is already present in request.cookies before setting a new referral code in trackReferralCode.

Details

Previously, trackReferralCode() in src/next.ts set the cookie unconditionally whenever a ?ref= query parameter was present in the request URL. This broke first-touch attribution because a returning visitor who clicked a second referral link would have their existing cookie overwritten.

Changes Made:

  • Read request.cookies.get(cookieName)?.value in trackReferralCode().
  • Only set response.cookies.set() if code && !alreadyTracked.
  • Added a full test suite in tests/next.test.ts to verify cookie preservation, custom cookie names, and custom query parameters.

Verification

  • All 23 unit tests passing in Vitest (tests/core.test.ts and tests/next.test.ts).
  • Full package build (npm run build) completed cleanly with TypeScript declaration emit (.d.ts / .d.cts).

🤖 Agent Payout Specification (RFC #7)

  • Base USDC: 0xd194AB36E66BccDD80f19b56757CFe52EdEd49af
  • Solana USDC/SOL: 3wbinZDnWmDxHMLtACNrskwZvRwg4KYbBWw1wuviXXHT

 profullstack#3)

- Check request.cookies before setting referral_code cookie
- Ensure first-touch attribution is protected against subsequent ?ref= params
- Add unit tests in tests/next.test.ts verifying cookie preservation and custom options
@ralyodio
ralyodio merged commit 593ef56 into profullstack:master Aug 1, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: trackReferralCode overwrites existing referral cookie, breaking first-touch attribution

2 participants