You can target content based on user interactions with your e-commerce platform's resources—such as products or collections—with e-commerce plugins.
To use e-commerce resource targeting, your app must pass the appropriate targeting attributes to Builder. For example, if you target an announcement bar to display when a user adds a certain item to their cart, your app must pass the product's id
when requesting the announcement bar's content.
You can pass targeting attributes either by using the JavaScript SDK or with query string parameters when requesting content using the Builder Content API and GraphQL API.
The e-commerce custom type that you use for your targeting condition determines which targeting attribute you use. For example, when using a request object type like ShopifyProduct
—displayed in the custom types list as Shopify Product when creating a new targeting condition—you must pass the Shopify ID for your product to the product
attribute.
You can implement the above with the following code when using the SDK:
const content = await builder.get('myTargetedContent', {
userAttributes: { product: product.id }
}).promise();
You can alternatively use setUserAttributes
to set the targeting attributes once across multiple content requests:
builder.setUserAttributes({ product: product.id });
const content = await builder.get('myTargetedContent').promise();
const otherContent = await builder.get('myOtherTargetedContent').promise();
You can also pass the targeting attributes using query strings when you send requests with the content or GraphQL APIs:
const response = await fetch(`https://cdn.builder.io/api/v2/content/my-model?apiKey=YOUR_API_KEY&userAttributes.product=${product.id}`)
If product.id
matches the ID of the product selected for your targeting condition, then your content renders.
After setting up your codebase to pass targeting attributes to Builder, you can target content using e-commerce targeting conditions. For example, you can show users an announcement bar when a certain product is in the cart.
To learn how to set up targeting within the Visual Editor, see Targeting with e-commerce plugins.
For more information on how to use e-commerce plugins, check out the following articles: