Skip to content

feat(tracking): count unique clicks per recipient - #81

Merged
diwenne merged 1 commit into
diwenne:mainfrom
MrFluffen:feat/unique-clicks-per-recipient
Oct 5, 2026
Merged

diwenne merged 1 commit into
diwenne:mainfrom
MrFluffen:feat/unique-clicks-per-recipient

Conversation

@MrFluffen

Copy link
Copy Markdown

Closes #80

What

Clicks and CTR counted every request to /r/[slug], so one person tapping a DM link three times counted as three clicks. They now count once per person per campaign.

  • Worker: links sent in a DM (buttons, inline fallback and plain-text DMs) get ?r=<token>. The token is a 22-character HMAC of the recipient's Instagram-scoped ID (hashRecipientId in lib/tracking/server.ts), keyed with NEXTAUTH_SECRET the same way hashClickIp is. The raw ID never appears in the URL.
  • Redirect: /r/[slug] stores a well-formed token in the new nullable LinkClick.recipientHash column. A missing or malformed r is ignored, and the redirect behaves exactly as before.
  • Counting: the dashboard, the campaign list and the shared report (total, per link and daily) count unique clickers through countUniqueClicks / countUniqueClicksBy in lib/tracking/analytics.ts. A click counts by its token, otherwise by ipHash, otherwise on its own. Older clicks and public-reply links have no token, so they fall back to the IP hash, and nothing needs backfilling.
  • Migration: one ADD COLUMN IF NOT EXISTS, identical to what prisma migrate diff generates.

The public reply is left untagged on purpose. It's visible to everyone, so a token there would credit every visitor to the commenter.

Trade-offs worth a look

  • The click counts now load id, automationId, recipientHash, ipHash per click and dedupe in memory, rather than running count(). That follows the existing contactsCount pattern (findMany + distinct) and stays small per row. If you'd rather keep it in the database, a COUNT(DISTINCT COALESCE(...)) raw query would also work, and I can switch to it.
  • If a recipient forwards the link, clicks by the new person count under the original recipient. That still counts at most once per campaign, so CTR can't be inflated by it.

Checks

  • npm test: 314 passed (new tests for token generation, parsing, the redirect and the counting helpers; existing worker and report tests updated for the tokenized URLs).
  • npm run lint: clean.
  • npm run typecheck / npm run build: both fail on lib/queue/dm-worker.ts:868 (operationId: ... : null passed where sendPostbackOnce expects a string). That error is already on main since fix: stop repeated DMs after ambiguous Meta delivery errors #68, and CI on main fails on it too, so it's unrelated to this PR. With that line bypassed locally, the build passes.

Notes

Feel free to change anything you see fit, or push changes straight to this branch. I hope the issue explains the problem and the idea well enough to give you the full context, and I'm happy to adjust things myself too.

Clicks and CTR counted every request to /r/[slug], so one person tapping a
DM link three times showed up as three clicks.

Links sent in a DM now carry a per-recipient token (?r=), a keyed hash of
the Instagram-scoped ID, stored on LinkClick.recipientHash. Clicks are
counted once per person per campaign on the dashboard, the campaign list
and the shared report. Clicks without a token (older links, public
replies) fall back to the IP hash, and clicks with neither count
individually.
@vercel

vercel Bot commented Oct 1, 2026

Copy link
Copy Markdown

@MrFluffen is attempting to deploy a commit to the diwenne's projects Team on Vercel.

A member of the Team first needs to authorize it.

@diwenne
diwenne merged commit 3b550c9 into diwenne:main Oct 5, 2026
1 check failed
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.

Count unique clicks per recipient instead of raw clicks for campaign CTR

2 participants