构建器个性化 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 :
- 它首先根据所提供的A/B测试变体适用
abTests对象. - 然后它根据所提供的用户属性来评价个性化容器.
- 最后,它返回一个带有适当的个性化内容和A/B测试变体的新HTML字符串.
这种方法允许您提供个性化内容和A/B测试变体,同时仍能利用边缘缓存或静态站点生成,因为个人化和A/B测试逻辑在初始HTML生成后被应用.