@dub/analytics

中文文档
0.0.33发布于 2026/11/12MPL-2.0TypeScriptESM + CJS近 30 天下载 75.2万

@dub/analytics allows you to track leads and sales conversions for Dub.

原始使用文档
文档目录11 节
  1. Overview
  2. Quick start
  3. Available Props
  4. apiHost
  5. domainsConfig
  6. shortDomain
  7. attributionModel
  8. cookieOptions
  9. queryParam
  10. trackIdentityPaths
  11. scriptProps

Overview

@dub/analytics allows you to track leads and sales conversions for Dub.

Quick start

  1. Enable conversion tracking for your Dub link.
  2. Install the @dub/analytics package to your project
npm install @dub/analytics
  1. Inject the Analytics script to your app
import { Analytics as DubAnalytics } from '@dub/analytics/react';

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body>{children}</body>
      <DubAnalytics />
    </html>
  );
}

You can all use the inject() function to add the tracking script to other frameworks.

Available Props

You can pass the following props to the Analytics component to customize the tracking script.

apiHost

The API host to use for tracking. This is useful for setting up reverse proxies to avoid adblockers. The default is https://api.dub.co.

domainsConfig

This is a JSON object that configures the domains that Dub will track.

  • refer: The Dub short domain for referral program client-side click tracking (previously shortDomain).
  • site: The Dub short domain for tracking site visits.
  • outbound: An array of domains for cross-domain tracking. When configured, the existing dub_id cookie will be automatically appended to all outbound links targeting these domains to enable cross-domain tracking across different applications.

shortDomain

[DEPRECATED: use domainsConfig.refer instead] The custom short domain you're using on Dub for your short links (for client-side click tracking).

attributionModel

Decide the attribution model to use for tracking. The default is last-click.

  • 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.

cookieOptions

The cookieOptions prop accepts the following keys:

KeyDefaultDescriptionExample
domainnullSpecifies the value for the Domain Set-Cookie attribute.example.com
expires90 days from nowSpecifies the Date object to be the value for the Expires Set-Cookie attribute.new Date('2024-12-31')
expiresInDays90Specifies the number (in days) to be the value for the Expires Set-Cookie attribute.90
path/Specifies the value for the Path Set-Cookie attribute. By default, the path is considered the "default path"./

For example, to set a 60-day cookie window, you can use the following code:

import { Analytics as DubAnalytics } from "@dub/analytics"

<DubAnalytics
   cookieOptions={{
      expiresInDays: 60,
   }}
/>

queryParam

The query parameter to listen to for client-side click-tracking (e.g. ?via=john, ?ref=jane). The default is via.

trackIdentityPaths

Identifies the visitor's device on the given paths and links it to their Dub click, for partner risk monitoring.

A path matches exactly (a trailing slash is ignored). A trailing /* also matches everything under the path, so /checkout/* matches /checkout and /checkout/plan. It runs on the first page load and on client-side navigation to a matching path.

It only runs for client-side partner clicks, tracked with ?via= or the parameter you set in queryParam (so set domainsConfig.refer). That click sets the dub_partner_data cookie, and the script identifies only while that cookie belongs to the click in dub_id. Ad and site-visit clicks are skipped, so they never use Fingerprint.

It runs once per click: after identifying, it appends the device's visitor ID to the cookie (dub_id=<clickId>_<visitorId>). A new click replaces the cookie and gets identified again. trackLead and trackSale send the full value, so Dub can link the customer to the device; pass the cookie as is when you track conversions from your server too. The script strips the suffix for outbound links, and anywhere else you read the dub_id cookie yourself, the click ID is the part before _. The device identification agent is loaded only when identification runs, and failures never throw or block the page.

Identification needs Sealed Client Results turned off in the Fingerprint workspace, so the agent returns the visitor ID.

<DubAnalytics trackIdentityPaths={['/signup', '/checkout/*']} />

To identify conditionally instead, call trackIdentity yourself. It follows the same dub_id rules, and needs the trackIdentityPaths prop to load the identity variant (trackIdentityPaths={[]} turns off automatic identification).

Where the law requires consent for device fingerprinting, use trackIdentityPaths={[]} and call trackIdentity() only after the visitor gives consent. The script has no consent or opt-out setting of its own. A call first waits for a pending ?via= click, and it can't be canceled, so identification can still start if consent is withdrawn during that wait.

const { trackIdentity } = useAnalytics();

await trackIdentity();

Without React, use the identity script variant (script.identity.js, or combined like script.site-visit.identity.js). Set the paths with a data-track-identity-paths attribute, and call dubAnalytics.trackIdentity(). To queue calls made before the script loads, add trackIdentity to the snippet's method list.

<script
  src="https://www.dubcdn.com/analytics/script.identity.js"
  data-track-identity-paths='["/signup", "/checkout/*"]'
  defer
></script>

The device identification agent loads from identity-agent.js next to the script. If you serve the script from your own domain or a proxy, serve identity-agent.js from the same folder.

scriptProps

Custom properties to pass to the script tag. Refer to MDN for all available options.

版本与发布记录

@dub/analytics 共发布 30 个版本,首次发布于 2024/02/14;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.0.33latest2026/11/121 天前
0.0.322025/10/081.1 年前
0.0.312025/09/301.1 年前
0.0.302025/08/221.2 年前
0.0.292025/07/291.3 年前
0.0.282025/04/241.6 年前

其他发布通道:beta → 0.0.18-beta.4。

关于 @dub/analytics 的常见问题

@dub/analytics 是什么?

@dub/analytics allows you to track leads and sales conversions for Dub.

如何安装 @dub/analytics?

在项目目录运行 npm install @dub/analytics,或使用 pnpm add / yarn add / bun add。

@dub/analytics 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dub/analytics 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dub/analytics 可以商用吗?

@dub/analytics 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@dub/analytics 的下载量有多少?

根据 npm 官方下载统计,@dub/analytics 在 2026/10/13 – 2026/11/11 这 30 天内被下载 751,730 次,日均约 25,058 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dub/analytics 还在维护吗?

最新版本 0.0.33 发布于 2026/11/12(1 天前);至今共发布 30 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/02/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。