@cookie3/analytics

中文文档
0.6.0发布于 2023/09/29MITTypeScriptCommonJS零依赖近 30 天下载 3039

The Cookie3 Analytics plugin is a versatile tool designed to simplify the process of adding tracking to websites for clients of Cookie3. Whether you're working with a traditional codebase or a modern Single Page Application (SPA) built with frameworks lik

原始使用文档
文档目录11 节
  1. Features
  2. Installation
  3. Getting Started
  4. Registering a website in Cookie3
  5. Usage
  6. 1. Import the Library
  7. 2. Initialize the Library
  8. 3. Track Page Views
  9. 4. Track Events
  10. How it works
  11. Usage with Next.js / React

Overview

The Cookie3 Analytics plugin is a versatile tool designed to simplify the process of adding tracking to websites for clients of Cookie3. Whether you're working with a traditional codebase or a modern Single Page Application (SPA) built with frameworks like Next.js, React, or Nuxt, this library offers the flexibility and ease of integration you need.

Features

  • Cross-Framework Compatibility: Works seamlessly with various web development frameworks and libraries, including Next.js, React, Nuxt, and more.
  • Universal Tracking: Provides a unified API for tracking user interactions, events, and other metrics across different types of web applications.
  • Customizable Tracking: Easily configure tracking parameters and events to suit your specific analytics requirements.
  • Asynchronous Loading: Ensures minimal impact on your website's performance by loading tracking scripts asynchronously.
  • Real-Time Data: Access real-time analytics data through the Cookie3 dashboard for informed decision-making.

Installation

You can install the Cookie3 Analytics plugin via npm or yarn:

npm install @cookie3/analytics

or

yarn add @cookie3/analytics

Getting Started

Notice: The Cookie3 Analytics plugin requires a valid Cookie3 siteId to work. If you don't have a Cookie3 account, you can sign up for a free account here.

Registering a website in Cookie3

Note: skip this step if you already have a website registered in Cookie3.

Before you can start tracking your website's analytics, you'll need to register your website in Cookie3. To do this, follow these steps:

  1. Log in to your Cookie3 account.
  2. Go to the /data-sources page.
  3. Click on the Integrate Website button.
  4. On the next page, click on the "Add a new website" button in the top right corner.
  5. Enter your website's URL and name, then click on the "Add site" button.
  6. Copy the site ID from the script snippet and save it for later.

You can now start tracking your website's analytics using the Cookie3 Analytics plugin.

Getting the site id of an existing website

If you already have a website registered in Cookie3, you can get its site ID by following these steps:

On the /data-sources page, click on the Integrate Website button. You should see your website listed on the page. Click on the "View script" button next to your website's name. You should see a script snippet with your website's site ID. Copy the site ID and save it for later.

Usage

1. Import the Library

Import the library into your project to begin using it:

import { cookie3Analytics } from "@cookie3/analytics";

2. Initialize the Library

Initialize the library with your siteId from Cookie3 key:

const analytics = cookie3Analytics(options);

The options parameter is an object that accepts the following properties:

PropertyTypeDescription
siteIdnumberYour Cookie3 site ID.
disabledbooleanWhether to disable tracking. Defaults to false.
autoDetectWalletAddressesbooleanWhether to automatically detect wallet addresses in the page and track them. Defaults to true.
autoDetectWalletExtensionsbooleanWhether to automatically detect wallet extensions in the page and track them. Defaults to true.
heartBeatactive: boolean; seconds?: number;Whether to enable heart beat tracking and the interval in seconds. This option enables more accurate visit time tracking. The default interval is set to 10 seconds. Defaults to undefined.
linkTrackingbooleanWhether to enable link tracking. Defaults to true.
disableCookiesbooleanWhether to disable cookies. Defaults to false.
stagingbooleanWhether to use the staging environment. Defaults to false. For internal use only.

3. Track Page Views

Important information

When using the Cookie3 Analytics plugin all tracked pageviews are registered programmatically, so you need to call the trackPageView method on every page load.

Start tracking page views by calling the library's trackPageView method:

Tracking pageviews

analytics.trackPageView();

By default, the trackPageView method will automatically track the current page's URL and title. You can also pass in custom parameters to override the default values:

analytics.trackPageView({
  documentTitle: "Custom title",
  href: "https://example.com",
});

Note: The trackPageView method should be called on every page load.

4. Track Events

Start tracking events and user interactions by calling the library's tracking methods:

analytics.trackEvent({
  category: "Call to action",
  action: "Click",
  value: 200,
});

Note: The value parameter is optional and is used to track the financial value of the event. For example, if you're tracking a purchase event, you can pass in the total amount of the purchase as the value parameter.

How it works

The library sends a request to the Cookie3 API to track pageviews, events and user interactions. The API then processes the request and stores the data in the Cookie3 database. You can then access the data through the Cookie3 dashboard.

Whitelisting the Cookie3 API domain

The Cookie3 Analytics plugin sends requests to the Cookie3 API domain. If you're using a Content Security Policy (CSP) on your website, you'll need to whitelist the Cookie3 API domain to allow the plugin to work properly. The Cookie3 API domain is webanalytics.cookie3.co.

Usage with Next.js / React

  1. Create a context provider to make the instance available in your React app:
import { type Cookie3Analytics } from "@cookie3/analytics";
import { createContext, useContext } from "react";

export interface ProviderProps {
  children?: React.ReactNode;
  value: Cookie3Analytics;
}

const Cookie3Context = createContext<Cookie3Analytics | undefined>(undefined);

export const Cookie3Provider = ({ children, value }: ProviderProps) => {
  return (
    <Cookie3Context.Provider value={value}>{children}</Cookie3Context.Provider>
  );
};

export const useCookie3 = () => {
  const context = useContext(Cookie3Context);

  return context;
};
  1. Initialize the instance in your _app.tsx file:
import type { AppProps } from "next/app";

import { UserOptions, cookie3Analytics } from "@cookie3/analytics";
import { Cookie3Provider } from "@/hooks/useCookie3Analytics";

const config: UserOptions = {
  siteId: YOUR_SITE_ID,
};

const analytics = cookie3Analytics(config);

export default function App({ Component, pageProps }: AppProps) {
  return (
    <Cookie3Provider value={analytics}>
      <Component {...pageProps} />
    </Cookie3Provider>
  );
}
  1. Track events in your components:
const cookie3 = useCookie3();

useEffect(() => {
  cookie3?.trackPageView();
}, []);

版本与发布记录

@cookie3/analytics 共发布 14 个版本,首次发布于 2023/09/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.6.0latest2023/09/293 年前
0.5.02023/09/293 年前
0.4.82023/09/093 年前
0.4.72023/09/093 年前
0.4.62023/09/083 年前
0.4.52023/09/083 年前

关于 @cookie3/analytics 的常见问题

@cookie3/analytics 是什么?

The Cookie3 Analytics plugin is a versatile tool designed to simplify the process of adding tracking to websites for clients of Cookie3. Whether you're working with a traditional codebase or a modern Single Page Application (SPA) built with frameworks lik

如何安装 @cookie3/analytics?

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

@cookie3/analytics 支持 TypeScript 吗?

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

@cookie3/analytics 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cookie3/analytics 可以商用吗?

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

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

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

@cookie3/analytics 还在维护吗?

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