@barbapapazes/plausible-tracker

中文文档
0.5.6发布于 2024/11/20MITTypeScriptESM + CJS零依赖近 30 天下载 1.4万

Fork of frontend tracker to interact with Plausible Analytics

原始使用文档
文档目录8 节
  1. Why a rewrite?
  2. Usage
  3. Extensions
  4. Auto pageview tracking
  5. Outbound link click tracking
  6. File downloads tracking
  7. Contribute
  8. License

Plausible Analytics Tracker

npm version npm downloads

A rewrite of plausible-tracker with extensibility and future features in mind.

[!IMPORTANT] This is not a drop-in replacement.

Why a rewrite?

The original tracker from the Plausible team have not received updates for more than 2 years.

Since then, the script tracker have evolved but not the npm package. The file downlaods and revenue tracking are not supported, for example.

Also, some serious issues were opened and not addressed. The outbound tracking was breaking the target="_blank" attribute, for example.

Usage

First, install the package:

npm install @barbapapazes/plausible-tracker

Then, use it in your code:

import { createPlausibleTracker } from '@barbapapazes/plausible-tracker'

const plausible = createPlausibleTracker()

plausible.trackPageview() // Track a page view
plausible.trackEvent('signup') // Track a custom event

[!NOTE] The trackPageview is a wrapper around trackEvent with the pageview event name.

First argument of trackPageview and second of trackEvent is an object accepting props, data and a callback that is triggered after the event is sent.

Props are similar to the ones provided by the official Plausible script.

Data allows you to override some values like the url, referrer and deviceWidth.

plausible.trackEvent('signup', { props: { variation: 'button A' }, callback: () => console.log('sended') }) // Track a custom event with a custom prop with a callback

This core tracker is minimal and does not include any extensions. So you can easily compose it to avoid to ship code that you don't need.

Extensions

The extensions are the features that are not included in the core tracker.

[!NOTE] Extensions will help to maintain the package up to date with the official script.

Current extensions:

Auto pageview tracking

import { createPlausibleTracker } from '@barbapapazes/plausible-tracker'
import { useAutoPageviews } from '@barbapapazes/plausible-tracker/extensions'

const plausible = createPlausibleTracker()

const { install, cleanup, setEventOptions } = useAutoPageviews(plausible)

install()

// At any time, you can stop the auto pageview tracking
cleanup()

// You can also set the page options
setEventOptions({ props: { variation: 'button A' } })
// Now, every page view will include the prop `variation: 'button A'`
import { createPlausibleTracker } from '@barbapapazes/plausible-tracker'
import { useAutoOutboundTracking } from '@barbapapazes/plausible-tracker/extensions'

const plausible = createPlausibleTracker()

const { install, cleanup, setEventOptions } = useOutboundLinkTracking(plausible)

install()

// At any time, you can stop the outbound link tracking
cleanup()

// You can also set the page options
setEventOptions({ props: { variation: 'button A' } })

File downloads tracking

import { createPlausibleTracker } from '@barbapapazes/plausible-tracker'
import { useAutoFileDownloadsTracking } from '@barbapapazes/plausible-tracker/extensions'

const plausible = createPlausibleTracker()

const { install, cleanup, setEventOptions } = useFileDownloadsTracking(plausible, { fileTypes: [] }) // You can pass the file types to track. For example: ['pdf', 'zip']

install()

// At any time, you can stop the file downloads tracking
cleanup()

// You can also set the page options
setEventOptions({ props: { variation: 'button A' } })

[!IMPORTANT] By default, the file downloads does not track any file type. You need to pass the file types to track.

The package exports the defaultFileTypes that is an array with the most common file types which are the same as the official script.

import { defaultFileTypes } from '@barbapapazes/plausible-tracker/extensions'

const { install, cleanup, setEventOptions } = useFileDownloadsTracking(plausible, { fileTypes: defaultFileTypes })

This allows you to easily customize the file types to track:

import { defaultFileTypes } from '@barbapapazes/plausible-tracker/extensions'

const { install, cleanup, setEventOptions } = useFileDownloadsTracking(plausible, { fileTypes: [...defaultFileTypes, 'svg'] })

Contribute

Contributions are more than welcome. I'm not part of the Plausible team and I'm doing this in my free time. 💛

  • Fork the repository
  • Install the dependencies: pnpm install
  • Build the project: pnpm build
  • Use playground to test your changes: cd playground && pnpm dev

Thank you for your help!

License

MIT © Barbapapazes

This package is not affiliated with Plausible Analytics but inspired by their work. plausible-tracker Plausible Analytics Tracker

版本与发布记录

@barbapapazes/plausible-tracker 共发布 16 个版本,首次发布于 2024/02/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.5.6latest2024/11/202 年前
0.5.52024/11/202 年前
0.5.42024/11/202 年前
0.5.32024/10/032 年前
0.5.22024/07/182 年前
0.5.12024/07/182 年前

关于 @barbapapazes/plausible-tracker 的常见问题

@barbapapazes/plausible-tracker 是什么?

Fork of frontend tracker to interact with Plausible Analytics

如何安装 @barbapapazes/plausible-tracker?

在项目目录运行 npm install @barbapapazes/plausible-tracker,或使用 pnpm add / yarn add / bun add。

@barbapapazes/plausible-tracker 支持 TypeScript 吗?

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

@barbapapazes/plausible-tracker 支持 ESM 还是 CommonJS?

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

@barbapapazes/plausible-tracker 可以商用吗?

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

@barbapapazes/plausible-tracker 的下载量有多少?

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

@barbapapazes/plausible-tracker 还在维护吗?

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