Repository navigation
feat(tracking): count unique clicks per recipient - #81
Merged
Merged
Conversation
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.
|
@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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.?r=<token>. The token is a 22-character HMAC of the recipient's Instagram-scoped ID (hashRecipientIdinlib/tracking/server.ts), keyed withNEXTAUTH_SECRETthe same wayhashClickIpis. The raw ID never appears in the URL./r/[slug]stores a well-formed token in the new nullableLinkClick.recipientHashcolumn. A missing or malformedris ignored, and the redirect behaves exactly as before.countUniqueClicks/countUniqueClicksByinlib/tracking/analytics.ts. A click counts by its token, otherwise byipHash, 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.ADD COLUMN IF NOT EXISTS, identical to whatprisma migrate diffgenerates.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
id, automationId, recipientHash, ipHashper click and dedupe in memory, rather than runningcount(). That follows the existingcontactsCountpattern (findMany+distinct) and stays small per row. If you'd rather keep it in the database, aCOUNT(DISTINCT COALESCE(...))raw query would also work, and I can switch to 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 onlib/queue/dm-worker.ts:868(operationId: ... : nullpassed wheresendPostbackOnceexpects astring). That error is already onmainsince fix: stop repeated DMs after ambiguous Meta delivery errors #68, and CI onmainfails 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.