dub icon indicating copy to clipboard operation
dub copied to clipboard

Affiliate tracking

Open zlwaterfield opened this issue 4 months ago • 2 comments

  • Added new Tinybird data source for conversions
  • Added tracking endpoints for clicks and conversions
    • Two API endpoints added /api/track/click and /api/track/conversion
    • Both are using API key authentication with a new withAuthEdge handler - note: need to test this + the rate-limiting logic
    • Both are using Zod for validation
    • The click endpoint takes in a URL, finds the link, records a click on that link with that Click ID and returns a Click ID to the user
    • The conversion endpoint takes in the event name, properties and click id and records the conversion in the new Tinybird data source
    • The Click ID will be recorded across the clicks and conversions so we can track the lifecycle of a user events
  • Added affiliates tables to Prisma.
    • A company will create affiliates in the dashboard (UI not done in this PR)
    • Affiliates will belong to a Project (and Campaign - though they aren't live yet) with an optional User
    • When a campaign is setup it will have a domain, url and affiliate param key associated, when the affiliate is created it will have a username. So each affiliate will have a URL generated either as a short link like https://${campaign.domain}/${affiliate.username} or as the full URL like https://${campaign.url}?{campaign.affiliate_param_key}=${affiliate.username}. Note: the campaign URL is really just the root domain of their website. This is a workaround when they don't want to use a short link. And if they don't want to use a short link, the domain will equal the URL.
      • When https://${campaign.url}?{campaign.affiliate_param_key}=${affiliate.username} is used the SDK will auto track the click since the campaign.affiliate_param_key is in the URL.
      • When https://${campaign.domain}/${affiliate.username} is used it will redirect to https://${campaign.url}?{campaign.affiliate_param_key}=${affiliate.username}. A click will be tracked on the redirect as well as the ending url visit with the SDK, but it will be deduced on the server. In this case, the SDK will not overwrite the click ID in the cookies.
  • Updated the analytics SDK to support all of the above.

Note: this is focused on the affiliate model. This doesn't include support for non-affiliate tracking for simple conversion tracking on all links.

This isn't foolproof, so call me out if I'm missing anything.

❓Questions ❓

  • For the conversion events API endpoint, how do we get the affiliate? We don't have a link or a project, and the API key is generic to the user.
  • Do we need to be passing the affiliate username in the click and conversion events? Is there another way to get it? Right now we pass affiliate param key on the click events and affiliate username on the conversion events.

🗒️ Things to do 🗒️

  • Add the Campaign model
  • Add the UI portion so companies can create/configure campaigns, create affiliates and view the analytics for campaigns and affiliates
  • Figure out a way to associate Affiliates with Users

zlwaterfield avatar Feb 28 '24 15:02 zlwaterfield