openreply icon indicating copy to clipboard operation
openreply copied to clipboard

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

Open MrFluffen opened this issue 2 months ago • 0 comments

First of all, thank you for building this and making it open source. I'm really happy this project exists.

Some context on why this matters to me: I run a coaching business with a lead magnet that people get through comment-to-DM. Once you pass about 1,000 leads a month, ManyChat gets expensive fast, which is why I'm moving to OpenReply on my own server. For me, tracking is a big part of the business side. I decide which text to use for better CTR and also offers to double down on. I need that number to be accurate.

Problem

Clicks and CTR count every request to /r/[slug]. When one person taps the DM link three times, it shows up as three clicks. The comment on calculateCtr in lib/tracking/analytics.ts already points this out. Because of that, CTR is capped at 100%, and below 100% it still overstates how many people actually clicked. For a creator, the question is usually "how many of the people I messaged opened the link?", and the current number can't answer it.

Proposed Solution

Main proposal: a per-recipient token on the tracked link

Every DM goes to a known recipient, so the worker can tag the link with that person:

  1. Worker. In buildLinkButtons and buildInlineLinkFallback (lib/queue/dm-worker.ts), add a short token to the tracked URL: /r/<slug>?r=<token>. The token is a keyed hash of the recipient's Instagram-scoped ID, for example a truncated HMAC with an existing server secret. All call sites already have userId, so no extra lookups are needed. Because it's a hash, the URL doesn't expose the raw ID.
  2. Click route. app/r/[slug]/route.ts reads r and stores it in a new nullable LinkClick.recipientHash column. Everything else stays the same, and the redirect works with or without the parameter.
  3. Counting. Wherever clicks are counted (app/api/dashboard/stats/route.ts, app/api/automations/route.ts, lib/reports/data.ts), count distinct recipients per campaign. CTR is based on that number.

This gives an exact count, and it doesn't depend on the network. A person who opens the link once on Wi-Fi and again on mobile data still counts once.

The cost is one migration that adds a nullable column. No backfill is needed: older clicks have no recipientHash, so they fall back to the IP-based count described below.

Privacy note: a click becomes linkable to a person through the server secret. DmLog already stores commenterId, commenterName and the comment text for each recipient, so this adds one fact (opened the link) to data OpenReply already holds. Operators should still mention it in their privacy policy, and it might be worth a line in the docs.

Alternative: count distinct ipHash, no migration

If you'd rather avoid a schema change, the smaller version is to count distinct ipHash per campaign instead of rows. The data is already stored, so only the counting changes.

This version isn't robust, though, because an IP isn't a person:

  • Phones switch between Wi-Fi and mobile data, and mobile carriers rotate IPs. One person can therefore count two or three times.
  • Carrier-grade NAT puts many mobile users behind one shared IP. Different people can therefore count as one.
  • Most Instagram users are on phones, which is exactly where both effects are strongest.

It's still much better than raw clicks, but the number stays an estimate. I'd use it only as the fallback for rows without a token.

Target User

  • Creator
  • Self-hoster

Acceptance Criteria

  • [ ] New DMs carry a per-recipient token on every tracked link, in both the button template and the inline fallback.
  • [ ] Several clicks by the same recipient on the same campaign count once in CTR and in the clicks figure on the dashboard, the campaign list and the shared report.
  • [ ] Older clicks without a token are counted by distinct ipHash, and rows with neither are counted individually.
  • [ ] Links without the r parameter, or with an unknown one, still redirect normally.
  • [ ] Tests for token generation and the counting helper.

Notes

I've opened a PR with the main proposal. If you'd rather have the IP-only version, I'm happy to cut it down.

MrFluffen avatar Oct 01 '26 09:10 MrFluffen