@dub/analytics is a lightweight (<1kb), open-source client-side script used for tracking conversions with Dub Conversions.

The script handles the detection of the dub_id query parameter and storing it as a first-party cookie, which will be used to attribute subsequent conversion events to the original link.

With the script, you can also track clicks on the client-side using query parameters (e.g. ?via=john). This gives you the flexibility to track clicks directly on your website or app, without needing to rely on link redirects.

Installation guides

Based on the framework or platform you’re using, you can install the script in different ways:

Concepts

You can pass the following props to the @dub/analytics script to customize its behavior:

data-api-host
url
default:
"https://api.dub.co"

The base URL for the Dub API. This is useful for setting up reverse proxies to avoid adblockers.

data-attribution-model
first-click | last-click
default:
"last-click"

The attribution model to use for the analytics event. The following attribution models are available:

  • first-click: The first click model gives all the credit to the first touchpoint in the customer journey.
  • last-click: The last click model gives all the credit to the last touchpoint in the customer journey.

Custom properties to pass to the cookie. Refer to MDN’s Set-Cookie documentation for all available options.

data-short-domain
string

The short domain you’re using on Dub for your short links. For security purposes, this must be a subdomain of your website’s domain. E.g. if your website domain is example.com, your short domain should be something like go.example.com.

data-query-param
string
default:
"via"

The query parameter to listen to for client-side click-tracking (e.g. ?via=abc123).

Examples

Here are some code examples to learn how to use the script:

Cross-domain tracking

By default, the script already sets the dub_id cookie on a cross-domain level. This means that if you have the script installed on your marketing site (e.g. example.com), the cookie will also be accessible when your user signs up for your app (e.g. app.example.com).

However, if you are installing the script on a subdomain (e.g. app.example.com), you will need to set the following option to make sure the cookie is accessible on the apex domain as well (e.g. example.com):

By default, the script sets the dub_id cookie to expire in 90 days. You can customize this behavior by passing the cookieOptions prop to the script:

Open-source examples

Here are some open-source code examples that you can referece: