@builder.io/personalization-utils

中文文档
4.0.0发布于 2024/10/04TypeScriptCommonJS近 30 天下载 2698

边缘个性化用具

中文使用文档

构建器个性化 Utils

在边缘提供个性化的建设者内容时,收集一些有用的工具.

npm install @builder.io/personalization-utils

如何从个性化重写开始?

此工具库可帮助您将选择属性的编码/ 解码作为 URL 的一部分, 以便缓存( 静态生成) 生成结果, 它应该与一个页面路径处理器结合使用 pages/[[...path]].jsx:

import { parsePersonalizedURL } from '@builder.io/personalization-utils/next'
// in pages/[[...path]].jsx
export async function getStaticProps({ params }) {
  const { attributes } = parsePersonalizedURL(params?.path);
  const page =
    (await builder
      .get('page', {
        apiKey: builderConfig.apiKey,
        userAttributes: attributes,
        // cachebust is not advised outside static rendering contexts.
        cachebust: true,
      })
      .promise()) || null

  return {
    props: {
      page,
    },
    // Next.js will attempt to re-generate the page:
    // - When a request comes in
    // - At most once every 1 second
    revalidate: 1,
  }
}

export function getStaticPaths() {
  return {
    paths: [],
    fallback: true,
  }
}

export default function Path({ page }) {
  return  <BuilderComponent model="page" content={page} />
}

现在,我们已经有了一个路径 渲染构建器的内容准备, 让我们的路径到它的中间软件:

import { getPersonalizedURL } from '@builder.io/personalization-utils/next'

const excludededPrefixes = ['/favicon', '/api'];

export default function middleware(request) {
  const url = request.nextUrl
  if (shouldRewrite(url.pathname)) {
    const personalizedURL = getPersonalizedURL(request)
    return NextResponse.rewrite(personalizedURL)
  }
  return NextResponse.next();
}
const audience = await myCDP.identifyAudience(userID);
// this will include the `audience` in all api calls and save it in a cookie `builder.userAttributes`
builder.setUserAttributes({ audience })

饼干一被设定,从现在开始所有构建者内容相匹配都会被当前观众部分所重压.

在动态容器和A/B试验中使用三进制Html

这个 trimHtml 函数是处理您 HTML 内容中的动态个性化容器和 A/B 测试变体的一种工具。 在边缘或服务器侧渲染情景中处理个性化内容特别有用.

使用量

import { trimHtml } from '@builder.io/personalization-utils'

const fullHTML = '... your full HTML string with personalization containers and A/B test variants ...';
const userAttributes = {
  audience: 'segment-a',
  date: '2023-06-15T12:00:00Z'
};
const abTests = {
  'content-id-1': 'variant-a',
  'content-id-2': 'variant-b'
};

const { html } = trimHtml(fullHTML, { userAttributes, abTests });

为了得到 userAttributes你应该分析一下 builder.userAttributes 饼干 举个例子说明你可能这样做:

import { parse } from 'cookie'

function getUserAttributes(req) {
  const cookies = parse(req.headers.cookie || '');
  const builderAttributes = cookies['builder.userAttributes'];
  return builderAttributes ? JSON.parse(builderAttributes) : {};
}

// Then in your request handler:
const userAttributes = getUserAttributes(req);

为了得到 abTests 数据,您可以这样分析相关的饼干:

import { parse } from 'cookie'

function getAbTests(req) {
  const cookies = parse(req.headers.cookie || '');
  const abTests = Object.entries(cookies).reduce((acc, [cookieName, cookieValue]) => {
    if (cookieName.startsWith('builder.tests')) {
      return {
        ...acc,
        [cookieName.split('.').slice(-1)[0]]: cookieValue
      }
    }
    return acc;
  }, {});
  return abTests;
}

// Then in your request handler:
const abTests = getAbTests(req);
const { html } = trimHtml(fullHTML, { userAttributes, abTests });

这个 trimHtml 函数按以下顺序处理 HTML :

  1. 它首先根据所提供的A/B测试变体适用 abTests 对象.
  2. 然后它根据所提供的用户属性来评价个性化容器.
  3. 最后,它返回一个带有适当的个性化内容和A/B测试变体的新HTML字符串.

这种方法允许您提供个性化内容和A/B测试变体,同时仍能利用边缘缓存或静态站点生成,因为个人化和A/B测试逻辑在初始HTML生成后被应用.

版本与发布记录

@builder.io/personalization-utils 共发布 63 个版本,首次发布于 2021/10/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.0latest2024/10/042 年前
3.0.02024/09/202 年前
2.0.1-0dev2023/02/283 年前
2.0.02022/09/164 年前
1.1.12022/08/314 年前
1.1.1-192022/08/304 年前

关于 @builder.io/personalization-utils 的常见问题

@builder.io/personalization-utils 是什么?

边缘个性化用具

如何安装 @builder.io/personalization-utils?

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

@builder.io/personalization-utils 支持 TypeScript 吗?

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

@builder.io/personalization-utils 支持 ESM 还是 CommonJS?

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

@builder.io/personalization-utils 可以商用吗?

@builder.io/personalization-utils 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@builder.io/personalization-utils 的下载量有多少?

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

@builder.io/personalization-utils 还在维护吗?

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