@happyvertical/smrt-marketing

中文文档
0.62.1发布于 2026/11/17MITTypeScriptES Module近 30 天下载 1.4万

Cross-channel campaign coordination, immutable performance evidence, budget pacing, and reusable Svelte marketing surfaces for SMRT

原始使用文档

@happyvertical/smrt-marketing

Cross-channel Campaign models, immutable performance snapshots, computed budget pacing, and reusable Svelte marketing surfaces for s-m-r-t.

pnpm add @happyvertical/smrt-marketing
import {
  BudgetPacingService,
  CampaignChannelCollection,
  CampaignCollection,
  MetricIngestionService,
} from '@happyvertical/smrt-marketing';

const campaigns = await CampaignCollection.create({ db });
const channels = await CampaignChannelCollection.create({ db });
const campaign = await campaigns.create({
  tenantId,
  customerId,
  campaignKey: 'summer-demand-2026',
  name: 'Summer demand 2026',
  objective: 'demand_generation',
  budgetCents: 200_000,
  currency: 'CAD',
});
if (!campaign.id) throw new Error('Campaign did not persist');

const adGroup = await channels.create({
  tenantId,
  campaignId: campaign.id,
  channelKind: 'ad_group',
  channelRef: 'ad-group-42',
  allocatedBudgetCents: 150_000,
});
if (!adGroup.id) throw new Error('Campaign channel did not persist');

const ingestion = await MetricIngestionService.create({ db });
await ingestion.ingest({
  tenantId,
  campaignId: campaign.id,
  campaignChannelId: adGroup.id,
  periodStart: new Date('2026-07-01T00:00:00Z'),
  periodEnd: new Date('2026-07-01T23:59:59Z'),
  spendCents: 12_500,
  impressions: 25_000,
  clicks: 800,
  conversions: 35,
  leads: 20,
  source: 'ad-platform',
  // Unique per tenant: (tenant_id, dedupe_key). A replay by the same owner
  // returns the original snapshot unchanged.
  dedupeKey: 'summer-demand-2026:ad-group-42:2026-07-01',
});

// Channel-scoped evidence is accepted only when the channel belongs to the
// supplied campaign. Reporting periods are required valid date-like values.

const pacing = await BudgetPacingService.create({ db });
console.log(await pacing.getCampaignPacing(campaign.id));

Customer-scoped campaign reads

Campaign.customerId is the native UUID relationship to the canonical @happyvertical/smrt-commerce:Customer. A campaign and its Customer must have exactly the same tenant, and customer-scoped reads require that tenant explicitly (null selects the global/global scope). Associated Campaign saves validate and persist in one transaction; customer-scoped reads validate and query in one transaction. Missing and cross-tenant Customers fail with CampaignCustomerScopeError without disclosing which condition occurred.

const firstPage = await campaigns.listByCustomer(tenantId, customerId, {
  limit: 50,
});
const secondPage = firstPage.nextCursor
  ? await campaigns.listByCustomer(tenantId, customerId, {
      limit: 50,
      after: firstPage.nextCursor,
    })
  : null;

const summaries = await campaigns.summarizeByCustomers(tenantId, customerIds);
// [{ customerId, totalCount, activeCount, latestStartAt }]

const reporting = await campaigns.listReportingByCustomer(
  tenantId,
  customerId,
  {
    limit: 50,
    after: firstPage.nextCursor ?? undefined,
    at: new Date('2026-08-15T00:00:00Z'),
  },
);
// reporting.items keeps the same newest-first page order. Every item contains:
// { campaign, channelCount, channelMix, metricTotals, pacing }

Pages, reporting pages, and summary batches are capped at 100 items and reject larger inputs. Pagination is newest-first by startAt, then UUID; campaigns without a start time follow scheduled campaigns. listReportingByCustomer() validates Customer scope and reads the page in one transaction, then performs one grouped channel read and one grouped immutable-evidence read regardless of page size. metricTotals use the same evidence selection as BudgetPacingService: for each exact period, a campaign rollup replaces its channel snapshots while channel-only periods remain. pacing is therefore equivalent to getCampaignPacing() without per-campaign callbacks or lazy loads. Summary resolution likewise uses a bounded grouped query rather than loading tenant campaigns or issuing one query per Customer.

Migrating metadata-backed associations

  1. Apply the generated schema migration that adds nullable native-UUID campaigns.customer_id and the (tenant_id, customer_id, start_at, id) index.
  2. In an operator-owned data migration, extract the old metadata Customer id, validate that it exists in commerce and has the exact same tenant_id, then write customer_id. Stop on missing, malformed, or mismatched values.
  3. Verify every expected association through listByCustomer() or summarizeByCustomers(), then update consumers to use these APIs.
  4. Remove the old metadata key after verification. Marketing never reads it as a compatibility fallback, so there is no tenant-wide JSON or raw-SQL path to keep in sync.

Svelte components are exported from @happyvertical/smrt-marketing/svelte. They are presentational and accept plain view models; consumers remain in control of fetching and mutations.

See AGENTS.md for lifecycle, evidence, and package-boundary invariants.

版本与发布记录

@happyvertical/smrt-marketing 共发布 279 个版本,首次发布于 2026/07/15;过去 12 个月发布了 279 个版本。

版本发布日期距今
0.62.1latest2026/11/17今天
0.62.02026/11/17今天
0.61.62026/11/161 天前
0.61.52026/11/161 天前
0.61.42026/11/161 天前
0.61.32026/11/152 天前

关于 @happyvertical/smrt-marketing 的常见问题

@happyvertical/smrt-marketing 是什么?

Cross-channel campaign coordination, immutable performance evidence, budget pacing, and reusable Svelte marketing surfaces for SMRT

如何安装 @happyvertical/smrt-marketing?

在项目目录运行 npm install @happyvertical/smrt-marketing,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:svelte(^5.57.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@happyvertical/smrt-marketing 支持 TypeScript 吗?

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

@happyvertical/smrt-marketing 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@happyvertical/smrt-marketing 可以商用吗?

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

@happyvertical/smrt-marketing 的下载量有多少?

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

@happyvertical/smrt-marketing 还在维护吗?

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