2.0.0 • Published 1 year ago

@builder.io/personalization-utils v2.0.0

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

Builder Personalization Utils

A collection of handy utilities when working with delivering personalized Builder content at the edge.

npm install @builder.io/personalization-utils

How to start with personalized rewrites?

PersonalizedURL identifies the current personalization target based on attributes in cookies, headers, query, and origin URL path, it should be used in middleware in combination with a page path handler defined in pages/builder/[hash].jsx:

// in pages/builder/[hash].jsx
export async function getStaticProps({ params }) {
  const personlizedURL = PersonalizedURL.fromHash(params.hash);
  const page =
    (await builder
      .get('page', {
        apiKey: builderConfig.apiKey,
        userAttributes: personlizedURL.attributes,
        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 5 seconds
    revalidate: 5,
  }
}

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

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

Now that we have a path for rendering builder content ready, let's route to it in the middleware:

import { NextResponse } from 'next/server'
import { PersonalizedURL } from '@builder.io/personalization-utils'

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

export default function middleware(
  request: NextFetchEvent
) {
  const url = request.nextUrl
  let response = NextResponse.next();
  if (!excludededPrefixes.find(path => url.pathname?.startsWith(path))) {
    const query = Object.fromEntries(url.searchParams);
    const personlizedURL = new PersonalizedURL({
      pathname: url.pathname,
      attributes: {
        // optionally add geo information to target by city/country in builder
        'builder.userAttributes.city': request.geo?.city || '',
        'builder.userAttributes.country': request.geo?.country || '',
        ...request.cookies,
        // passing query is to add support for studio tab navigation [viewing pages per targeting attributes]
        ...query
      }
    })

    url.pathname = personlizedURL.toHash();
    response = NextResponse.rewrite(url);
  }
  return response
}

Great now that we have the personalized routes ready all we need to do is set the corresponding cookie for any of the targeting attributes we have in builder:

  const audience = await myCDP.identifyAudience(userID);
  setCookie(`builder.userAttributes.audience`, audience)

Once the cookie is set, all builder content matching from now on will weigh in the current audience segment.

2.0.1-0

1 year ago

2.0.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.1-9

2 years ago

1.1.1-8

2 years ago

1.1.1-7

2 years ago

1.1.1-6

2 years ago

1.0.5-7

2 years ago

1.0.5-9

2 years ago

1.0.5-4

2 years ago

1.0.5-3

2 years ago

1.0.5-6

2 years ago

1.0.5-5

2 years ago

1.0.5-0

2 years ago

1.0.5-12

2 years ago

1.0.5-2

2 years ago

1.0.5-1

2 years ago

1.0.5-10

2 years ago

1.0.5-11

2 years ago

1.1.1-16

2 years ago

1.1.1-15

2 years ago

1.1.1-14

2 years ago

1.1.1-13

2 years ago

1.1.1-19

2 years ago

1.1.1-18

2 years ago

1.1.1-17

2 years ago

1.1.1-5

2 years ago

1.1.1-4

2 years ago

1.1.1-3

2 years ago

1.1.1-2

2 years ago

1.1.1-12

2 years ago

1.1.1-1

2 years ago

1.1.1-11

2 years ago

1.1.1-0

2 years ago

1.1.1-10

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.2.1-3

2 years ago

0.2.1-2

2 years ago

0.2.1-1

2 years ago

0.2.1-0

2 years ago

0.2.0

2 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.5-10

3 years ago

0.0.5-9

3 years ago

0.0.5-8

3 years ago

0.0.5-7

3 years ago

0.0.5-6

3 years ago

0.0.5-4

3 years ago

0.0.5-3

3 years ago

0.0.5-2

3 years ago

0.0.5-1

3 years ago

0.0.5-0

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago