With the @dub/analytics script, you can track clicks on the client-side using query parameters (e.g. ?via=john, ?ref=jane).

A few use cases include:

  • You’re using Dub Partners with dual-sided incentives and want to display a banner that says: “John referred you to Acme and gave you 25% off”
  • You have dynamically generated referral pages (e.g. Tesla) and want to track clicks using a trackClick() function inside your application code.
  • You are migrating from an existing affiliate management platform (e.g. Rewardful) that uses query parameters to track conversions.

Quickstart

Here’s how you can enable client-side click-tracking with the @dub/analytics script:

1

Add a custom domain to your Dub workspace

First, you’ll need to add a custom short link domain to your Dub workspace. You can use a domain you already own, or leverage our free .link domain offer to register a custom domain like yourcompany.link for free.

This is the domain that you’ll use for your short links on Dub.

2

Allowlist your site's domain

Then, you’ll need to allowlist your site’s domain to allow the client-side click events to be ingested by Dub. To do that, navigate to your workspace’s Analytics settings page and add your site’s domain to the Allowed Hostnames list.

When testing things out locally, you can add localhost to the Allowed Hostnames list temporarily. This will allow local events to be ingested by Dub. Don’t forget to remove it once you’re ready to go live!

3

Install the Dub client-side SDK

Next, install the Dub client-side SDK and initialize it with the domain you added in the previous step.

// install the package (e.g. npm install @dub/analytics)
import { Analytics as DubAnalytics } from "@dub/analytics/react";

export default function App() {
  return (
    <Layout>
      <DubAnalytics
        domainsConfig={{
          refer: "go.example.com", // the custom domain you're using on Dub for your short links
        }}
      />
      {/* Your app code here */}
    </Layout>
  );
}

Here’s the full list of parameters you can pass to the script:

4

(Optional, but recommended): Set up a reverse proxy

To avoid ad-blockers from blocking your click-tracking requests, we recommend setting up a reverse proxy.

Refer to the Reverse-proxy support guide to learn how to set one up.

5

Verify your setup

To verify that your click-tracking is working, run your website locally and append the URL with the unique key of your short link

For example, if your short link is https://go.example.com/abc123, you’ll need to append ?via=abc123 to the URL.

Once you’ve done that, check if the following is true:

  1. There is a successful /track/click request in your browser’s Network tab (and no errors in the Console tab).
  2. The dub_id cookie is being set upon a successful /track/click request.
  3. The click tracked correctly in the Events tab of your Dub workspace.

Automatically fetching partner and discount data

If you’re using Dub Partners with dual-sided incentives, the script will automatically fetch the partner and discount data for you when someone lands on your site via a valid referral link.

This data will be stored as a JSON-stringified object in the dub_partner_data cookie in the following format:

{
  "clickId": "xxx", // unique ID of the click event
  "partner": {
    "id": "pn_xxx", // unique ID of the partner on Dub
    "name": "John Doe", // name of the partner
    "image": "https://example.com/john.png" // avatar of the partner
  },
  "discount": {
    "id": "disc_xxx", // unique ID of the discount on Dub
    "amount": 25, // discount amount (either a percentage or a fixed amount)
    "type": "percentage", // type of the discount (either "percentage" or "fixed")
    "maxDuration": 3 // maximum duration of the discount in months
  }
}

To access this cookie data, we recommend using a cookie library like js-cookie – here’s an example:

import Cookies from "js-cookie";

const dubPartnerData = Cookies.get("dub_partner_data");
const { partner, discount } = dubPartnerData ? JSON.parse(dubPartnerData) : {};

// Display a banner that says: _"John referred you to Acme and gave you 25% off"_

return (
  <div className="flex items-center gap-2">
    <img src={image} alt={name} className="size-6 rounded-full" />
    <p>
      {name} referred you to Acme and gave you {amount} {type} off
    </p>
  </div>
);

Here’s an example of how the discount banner will look like:

Manually tracking clicks with the trackClick() function

This is helpful for tracking clicks on:

This feature is coming soon. If you’d like early access, please contact us.

Differences from server-side click-tracking

Server-side click-tracking is enabled by default for all Dub links and come with the following attributes:

AttributeTypeDescription
timestampstringThe timestamp of the click event
idstringThe unique ID of the click event
urlstringThe destination URL that the link resolved to – this can vary if geo/device-targeting is enabled
continentstringThe continent of the user who clicked the link
countrystringThe country of the user who clicked the link
citystringThe city of the user who clicked the link
devicestringThe device of the user who clicked the link
browserstringThe browser of the user who clicked the link
osstringThe operating system of the user who clicked the link
refererstringThe referrer of the user who clicked the link
refererUrlstringThe full referrer URL of the user who clicked the link
qrbooleanWhether the click event was triggered by a QR code scan
ipstringThe IP address of the user who clicked the link (non-EU users only)

These events happen on the server-side and cannot be blocked by browser extensions or ad-blockers, which improves the accuracy of your analytics data.