@dreamdata/analytics-next

中文文档
2.0.4发布于 2024/09/16MITTypeScriptESM + CJS近 30 天下载 7.4万

Analytics Next (aka Analytics 2.0) is the latest version of Dreamdata's JavaScript SDK.

原始使用文档
文档目录8 节
  1. Quickstart
  2. Using with Dreamdata
  3. Using as an npm package
  4. Lazy / Delayed Loading
  5. Usage in Common Frameworks and SPAs
  6. FAQ
  7. Adding Typescript support when using script tag
  8. Handling initialization errors

@dreamdata/analytics-next

Analytics Next (aka Analytics 2.0) is the latest version of Dreamdata's JavaScript SDK.

Quickstart

The easiest and quickest way to get started with Analytics 2.0 is to use it through Dreamdata. Alternatively, you can install it through NPM and do the instrumentation yourself.

Using with Dreamdata

Navigate to Dreamdata and choose JavaScript v2.0. Click "Reconfigure script" to go through a wizard on how to set up the script.

Using as an npm package

  1. Install the package
# npm
npm install @dreamdata/analytics-next

# yarn
yarn add @dreamdata/analytics-next

# pnpm
pnpm add @dreamdata/analytics-next
  1. Import the package into your project and you're good to go (with working types)!
import { AnalyticsBrowser } from '@dreamdata/analytics-next'

const analytics = AnalyticsBrowser.load({ writeKey: '<YOUR_WRITE_KEY>' })

analytics.identify('hello world')

document.body?.addEventListener('click', () => {
  analytics.track('document body clicked!')
})

Note that you can find your write key on the sources pages in the Dreamdata app.

Lazy / Delayed Loading

You can load a buffered version of analytics that requires .load to be explicitly called before initiating any network activity. This can be useful if you want to wait for a user to consent before sending any tracking data.

  • ⚠️ ️.load should only be called once.
export const analytics = new AnalyticsBrowser()

analytics.identify('hello world')

if (userConsentsToBeingTracked) {
  analytics.load({ writeKey: '<YOUR_WRITE_KEY>' }) // destinations loaded, enqueued events are flushed
}

This strategy also comes in handy if you have some settings that are fetched asynchronously.

const analytics = new AnalyticsBrowser()
fetchWriteKey().then((writeKey) => analytics.load({ writeKey }))

analytics.identify('hello world')

Usage in Common Frameworks and SPAs

See or documentation on integrating with SPAs here.

FAQ

Adding Typescript support when using script tag

  1. Install npm package @dreamdata/analytics-next as a dev dependency.

  2. Create ./typings/analytics.d.ts

// ./typings/analytics.d.ts
import type { AnalyticsSnippet } from '@dreamdata/analytics-next'

declare global {
  interface Window {
    analytics: AnalyticsSnippet
  }
}
  1. Configure typescript to read from the custom ./typings folder
// tsconfig.json
{
  ...
  "compilerOptions": {
    ....
    "typeRoots": [
      "./node_modules/@types",
      "./typings"
    ]
  }
  ....
}

Handling initialization errors

Initialization errors get logged by default, but if you also want to catch these errors, you can do the following:

export const analytics = new AnalyticsBrowser();
analytics
  .load({ writeKey: "MY_WRITE_KEY" })
  .catch((err) => ...);

版本与发布记录

@dreamdata/analytics-next 共发布 7 个版本,首次发布于 2024/05/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.4latest2024/09/162 年前
2.0.32024/08/292 年前
2.0.22024/07/192 年前
2.0.12024/06/282 年前
2.0.02024/06/032 年前
1.65.02024/05/312 年前

关于 @dreamdata/analytics-next 的常见问题

@dreamdata/analytics-next 是什么?

Analytics Next (aka Analytics 2.0) is the latest version of Dreamdata's JavaScript SDK.

如何安装 @dreamdata/analytics-next?

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

@dreamdata/analytics-next 支持 TypeScript 吗?

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

@dreamdata/analytics-next 支持 ESM 还是 CommonJS?

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

@dreamdata/analytics-next 可以商用吗?

@dreamdata/analytics-next 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dreamdata/analytics-next 的下载量有多少?

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

@dreamdata/analytics-next 还在维护吗?

最新版本 2.0.4 发布于 2024/09/16(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/05/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。