New guide: AI is in production. Is your governance?

Announcing Visual Copilot - Figma to production in half the time

Builder.io
Builder.io
Contact sales

New guide: AI is in production. Is your governance?

Announcing Visual Copilot - Figma to production in half the time

enterprise plans

Learn how to create custom targeting attributes in Builder to go beyond built-in targeting options.

Define custom attributes to tailor content delivery based on user roles, preferences, or other unique conditions for a more dynamic and personalized user experience.

  • Make sure you're familiar with the basics of Targeting Content.
  • You make custom targeting attributes in the Settings and apply them in the Visual Editor.
  • In your code, pass custom targeting attributes to Builder’s API or set them globally using setUserAttributes().
  1. Go to Space Settings.
  2. Under the Targeting section, click Edit button for Custom Targeting.
  3. Click + New Target Attribute button.
  4. Define targeting properties, such as Name, Type, and Enum in the Custom targeting attributes dialogue.
  5. Click Save.

The targeting properties:

  • Name specifies the name of the custom targeting attribute.
  • Type determines how values are entered. A String type provides a text box as an input, while a boolean type includes a toggle switch to select true or false.
  • Enum is available only for String types. This option replaces the text box with a dropdown menu, requiring users to select from predefined options when targeting content.

This video below shows how to create custom targeting attributes in Settings.

Target content using custom targeting attributes by setting conditions with the defined attributes.

The process is similar to targeting with built-in attributes, with custom targeting attributes included in the list of options for creating targeting conditions.

  1. Open the content entry you want to target.
  2. Click the Targeting icon at the top of the Visual Editor.
  3. Click + Add Target.
  4. Choose a targeting attribute from the dropdown and set its value based on the attribute type.

The video below covers setting up custom targeting attributes in the Visual Editor.

Targeting matches the values provided in a content API request through userAttributes with the targeting conditions set for your content in the Visual Editor. For built-in attributes like device or urlPath, Builder automatically extracts the necessary targeting data from your request.

For custom targeting attributes, include the required data in your request using the builder.get() or builder.getAll() methods.

const content = await builder.get('myModelName', {
  userAttributes: { 
    //user defined attribute
    user: ["admin", "designer"],
    paidUser: true,

    // Built in
    urlPath,
    device: ['mobile']
  }
}).promise();

You can alternatively use setUserAttributes to set the targeting attributes once across multiple content requests:

builder.setUserAttributes({
    user: ["admin", "designer"],
});

const model1Content = await builder.get('Model1').promise();
const model2Content = await builder.get('Model2').promise();

Alternatively, you can pass the attributes as query string parameters when making requests through the Content or GraphQL APIs.

const response = await fetch(`https://cdn.builder.io/api/v3/content/my-model?apiKey=YOUR_API_KEY&userAttributes.user=admin,designer`)

The following example explains how to define custom targeting attributes in a Page model and send them as query parameters to target content based on conditions set in the visual editor. You can also use a Section model.

For example, create two versions of the page: one that displays the premium version when isProUser is true and another that displays the basic version for non-paid users.

You can define as many custom targeting attributes as needed to suit your use case.

import { builder } from "@builder.io/sdk";
import { RenderBuilderContent } from "@/components/builder";

builder.init(/* ADD YOUR PUBLIC API KEY */);

export default async function Page({ params }: { params: { page: string[] } }) {
  const { page } = await params;
  const builderModelName = "YOUR_MODEL_NAME";

  const collectionHeroContent = await builder
    .get(builderModelName, {
      userAttributes: {

        // built in targeting attribute
        urlPath: "/" + (page?.join("/") || ""),

        // custom targeting attribute
        isProUser: true,
        userType: ["admin", "designer"]
      },
      prerender: false,
    })
    .toPromise();

  return (
    <>
      {/* Render the Builder page */}
      <BuilderContent
        model={builderModelName}
        content={collectionHeroContent}
      />
    </>
  );
}

For more examples, see Targeting Cheatsheet.

Targeting attributes store user input based on their type and determine how values are defined in content API requests. The API request must include the exact value to properly display the targeted content.

For example, userType with the value premium is passed to the Content API request.

In the Targeting section, the userType attribute is defined as a String type.

Screenshot of the Custom Targeting Attributes popup displaying the custom attribute called `userType` set as a string type from the available dropdown.

When making a request to the Content API, a String value is passed to userType, ensuring it aligns with the type declaration from Settings.

const content = await builder.get('myModelName', {
  userAttributes: {
    userType: "premium" // userType is defined as "String" type
  }
}).promise();

The targeting condition is set using the custom defined userType attribute, with premium as the expected value of type String.

Screenshot of the Targeting Editor in the Visual Editor, displaying the `userType` attribute set to "premium" as the targeting condition of type String, within the input field for string-type attributes.

You can create and use custom types with plugins like Shopify, Cloudinary, BigCommerce, Magento, and others. These plugins provide rich interfaces for inputs such as colors, forms, or products. The stored values depend on the editor and may not always be immediately clear.

For more details on stored values, see Extending the UI with custom types in the Making a Plugin.

Take your targeting a step further by scheduling your content and creating A/B tests to measure your Page or Section engagement.

Was this article helpful?