@fluid-app/fairshare

中文文档
0.2.13发布于 2025/07/22MITTypeScriptESM + CJS近 30 天下载 2.1万

Fairshare functionality for Fluid Commerce applications

原始使用文档
文档目录15 节
  1. Important: Integration with Fluid SDK
  2. Features
  3. Installation
  4. Quick Start
  5. Attribution Tracking
  6. Server-side Attribution Benefits
  7. How Server-side Attribution Works
  8. Attribution Models
  9. API Reference
  10. Core Functions
  11. Event Tracking Examples
  12. Add to Cart
  13. Checkout Started
  14. Debugging
  15. License

FairShare SDK

A lightweight, privacy-focused tracking SDK for Fluid Commerce that seamlessly tracks user events, attribution, and conversions while prioritizing performance and user privacy.

Important: Integration with Fluid SDK

For most use cases, you don't need to install or initialize FairShare directly.

FairShare is automatically included and initialized when you use the Fluid SDK. We recommend using the Fluid SDK directly for the simplest integration:

import { initializeFluid } from "@fluid-app/fluid";

// This initializes both Fluid SDK and FairShare
// Attribution is now handled server-side via the settings API
initializeFluid({
  fluidShop: "your-shop-id",
});

The remainder of this documentation is for advanced use cases where you need to use FairShare independently from the Fluid SDK.

Features

  • Server-side Attribution: Attribution is automatically handled by the Fluid Commerce backend via the settings API
  • Session Management: Uses localStorage instead of cookies to maintain sessions
  • Page Visit Tracking: Automatic tracking of page views with metadata
  • Event Tracking: Easy-to-use API for tracking custom events
  • Browser Fingerprinting: Privacy-focused device identification for cross-session attribution
  • Event Batching: Efficiently batches events to minimize network requests

Installation

npm install @fluid-app/fairshare
# or
yarn add @fluid-app/fairshare
# or
pnpm add @fluid-app/fairshare

Quick Start

import { initializeFairshare, trackFairshareEvent } from "@fluid-app/fairshare";

// Initialize the SDK
initializeFairshare({
  fluidShop: "your-shop-id",
  debug: true, // Enable debug logging (optional)
  // Attribution is now handled server-side via the settings API
});

// Track a custom event (e.g., Add to Cart)
trackFairshareEvent({
  eventName: "ADD_TO_CART",
  data: {
    product_id: 123,
    variant_id: 456,
    quantity: 1,
  },
});

Attribution Tracking

Attribution is now handled automatically by the Fluid Commerce backend via the settings API. The server parses URLs and provides attribution data, eliminating the need for client-side pattern matching.

Server-side Attribution Benefits

  • Automatic URL parsing: The server automatically extracts attribution from various URL formats
  • No client configuration needed: Attribution patterns are managed server-side
  • Consistent attribution: Centralized logic ensures consistent attribution across all touchpoints
  • Better performance: No client-side regex processing required

How Server-side Attribution Works

  1. When a user visits your site, the URL is sent to the Fluid Commerce backend
  2. The server parses the URL using centralized attribution logic
  3. Attribution data is returned via the settings API and stored locally
  4. All tracked events automatically include the server-determined attribution
  5. Conversions are credited to the correct source based on server-side attribution rules

Attribution Models

FairShare uses a last-touch attribution model by default. This means that if a user visits multiple attribution URLs, the most recent one will be used for tracking.

API Reference

Core Functions

initializeFairshare(config)

Initializes the FairShare SDK.

interface FairshareConfig {
  // Required: Your Fluid shop identifier
  fluidShop: string;

  // Optional: Enable debug mode for console logging
  debug?: boolean;

  // Optional: Custom API endpoint for all Fluid API requests
  apiEndpoint?: string;

  // Optional: Attribution override that overrides URL-based attribution
  attributionOverride?:
    | {
        email: string;
      }
    | {
        username: string;
      }
    | {
        share_guid: string;
      }
    | {
        fluid_rep_id: number;
      }
    | {
        external_id: string;
      };

  // Optional: Configure tracking behavior
  trackingSettings?: {
    // Disable automatic page visit tracking on initialization (default: false)
    disableAutoPageTracking?: boolean;
  };

  // Optional: Configure event batching
  batchSize?: number; // Maximum events per batch (default: 10)
  batchInterval?: number; // Milliseconds between batch sends (default: 1000)
  maxQueueSize?: number; // Maximum events to store if offline (default: 100)
  flushIntervalMs?: number; // Auto-flush interval (default: 10000)
}

trackFairshareEvent(event)

Tracks a custom event with data.

trackFairshareEvent({
  eventName: "CHECKOUT_STARTED",
  data: {
    cart_token: "cart_123456",
    metadata: {
      // Optional additional data
    },
  },
});

getSessionToken()

Gets the current session token.

const sessionToken = getSessionToken();

getAttribution()

Gets the current attribution data.

const attribution = getAttribution();
console.log(attribution?.username); // e.g., "influencer-name"
console.log(attribution?.email); // e.g., "[email protected]"
console.log(attribution?.share_guid); // e.g., "abc123-def456-ghi789"
console.log(attribution?.fluid_rep_id); // e.g., 12345
console.log(attribution?.external_id); // e.g., "external-affiliate-id"

reset()

Clears all stored data and resets the SDK state.

reset();

Event Tracking Examples

Add to Cart

trackFairshareEvent({
  eventName: "ADD_TO_CART",
  data: {
    product_id: 123,
    variant_id: 456,
    quantity: 1,
    price: "19.99",
    metadata: {
      // Optional additional data
      product_name: "Blue Sweater",
      category: "Apparel",
    },
  },
});

Checkout Started

trackFairshareEvent({
  eventName: "CHECKOUT_STARTED",
  data: {
    cart_token: "cart_123456",
    metadata: {
      total_amount: "49.99",
      currency: "USD",
      items_count: 3,
    },
  },
});

Debugging

Enable debug mode to see detailed logging in the browser console:

initializeFairshare({
  fluidShop: "your-shop-id",
  debug: true,
  // Attribution is now handled server-side via the settings API
  // Optional: Use attribution override for specific scenarios
  // You can use any of these attribution data formats:
  attributionOverride: {
    email: "[email protected]",
  },
  // OR
  // attributionOverride: { username: "influencer-username" },
  // OR
  // attributionOverride: { share_guid: "abc123-def456-ghi789" },
  // OR
  // attributionOverride: { fluid_rep_id: 12345 },
  // OR
  // attributionOverride: { external_id: "external-affiliate-id" },
});

You can also check localStorage directly to see what data is being stored:

  • fs_attribution - Current attribution data
  • fs_session - Session token
  • fs_fingerprint - Browser fingerprint
  • fs_event_queue - Queued events waiting to be sent

License

MIT

版本与发布记录

@fluid-app/fairshare 共发布 30 个版本,首次发布于 2025/03/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.13latest2025/07/221.3 年前
0.2.122025/07/181.3 年前
0.2.112025/07/161.3 年前
0.2.102025/07/161.3 年前
0.2.92025/07/161.3 年前
0.2.82025/07/071.4 年前

关于 @fluid-app/fairshare 的常见问题

@fluid-app/fairshare 是什么?

Fairshare functionality for Fluid Commerce applications

如何安装 @fluid-app/fairshare?

在项目目录运行 npm install @fluid-app/fairshare,或使用 pnpm add / yarn add / bun add。

@fluid-app/fairshare 支持 TypeScript 吗?

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

@fluid-app/fairshare 支持 ESM 还是 CommonJS?

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

@fluid-app/fairshare 可以商用吗?

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

@fluid-app/fairshare 的下载量有多少?

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

@fluid-app/fairshare 还在维护吗?

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