VariantGroup
VariantGroup
Variant Groups is a feature of Personalize in Contentstack. It allows you to link content types and create entry variants. This setup helps tailor content to different audiences or contexts based on specific grouping logic.
Note
- The Variants feature is currently part of our Early Access Program and may not be available in all stacks. To request access, contact our support team.
- You cannot edit variant group details directly. They are managed through Personalize.
Get All Variant Groups
The variantGroup.find() method retrieves all variant groups linked to your stack. This helps list available groups for content personalization.
import Contentstack;
// Replace AUTHTOKEN and API_KEY with your actual credentials
Contentstack contentstack = new
Contentstack.Builder().setAuthToken(AUTHTOKEN).build();
Stack stack = contentstack.stack(API_KEY);
VariantGroup variantGroup = stack.variantGroup();
Response<ResponseBody> response = variantGroup.find().execute();
if (response.isSuccessful() && response.body() != null) {
System.out.println("Fetched Variant Groups: " + response.body().string());
} else {
System.out.println("Error: " + response.errorBody().string());
}addParam
The addParam() methodsets a single query parameter for the request using a key-value pair..
| Name | Type | Description |
|---|---|---|
| key | String | Query parameter key |
| value | Object | Query parameter value |
import Contentstack;
// Replace AUTHTOKEN and API_KEY with your actual credentials
Contentstack contentstack = new Contentstack.Builder().setAuthToken(AUTHTOKEN).build();
Stack stack = contentstack.stack(API_KEY);
VariantGroup variantGroup = stack.variantGroup();
// Add query parameters to include count and variant info in the response
variantGroup.addParam("include_count", true);
variantGroup.addParam("include_variant_info", true);
Response<ResponseBody> response = variantGroup.find().execute();
if (response.isSuccessful() && response.body() != null) {
System.out.println("Fetched variant groups: " + response.body().string());
} else {
System.out.println("Error: " + response.errorBody().string());
}The following keys are accepted by the addParam method:
| Key | Value | Description |
|---|---|---|
| skip | int | Specifies the number of records to skip in the response. This is useful for pagination when fetching variant groups. |
| limit | int | Specifies the maximum number of records to return in a single API response. This parameter helps control the size of the response when fetching variant groups. |
| include_count | boolean | When set to true, the API response will include the total count of variant groups. |
| include_variant_info | boolean | When set to true, the API response will include detailed information about the variants associated with each variant group. |
| include_variant_count | boolean | When set to true, the API response will include the count of variants within each variant group. |
| asc | String | Specifies the field name by which to sort the results in ascending order. |
| desc | String | Specifies the field name by which to sort the results in descending order. |
| content_type | String | Filters the variant groups based on a specific content type UID. |
addParams
The addParams() method sets multiple query parameters for the request. Pass a HashMap of key-value pairs to customize the API response.
| Name | Type | Description |
|---|---|---|
| params | HashMap<String,Object> | Maps key–value pairs for API request customization such as include_count, limit, or content_type. |
// Create a map of query parameters to include in the API request
HashMap<String, Object> params = new HashMap<>();
params.put("include_count", true);
params.put("include_variant_info", true);
variantGroup.addParams(params);clearParams
The clearParams method removes a specific query parameter from the request using its key.
| Name | Type | Description |
|---|---|---|
| key (required) | String | Removes the query parameter that matches the specified key. |
import Contentstack;
// Replace AUTHTOKEN and API_KEY with your actual credentials
Contentstack contentstack = new Contentstack.Builder().setAuthToken(AUTHTOKEN).build();
Stack stack = contentstack.stack(API_KEY);
VariantGroup variantgroup = stack.variantGroup();
// Remove a specific query parameter (e.g., 'include_count') using its key
variantGroup.clearParams("key");addHeader
The addHeader method sets a custom header for the request. Use this to specify a key–value pair in the request header.
| Name | Type | Description |
|---|---|---|
| key (required) | String | The name of the header to include in the request. Common headers include authorization, authtoken, or api_key. |
| value (required) | Object | The value assigned to the header. This can be a string, such as a token or API key. Appears in the final request header. |
import Contentstack;
// Replace AUTHTOKEN and API_KEY with your actual credentials
Contentstack contentstack = new Contentstack.Builder().setAuthToken(AUTHTOKEN).build();
Stack stack = contentstack.stack(API_KEY);
// Add a custom header to the request using a key-value pair.
// For example: key = "authorization", value = "Bearer your_auth_token"
VariantGroup variantgroup = stack.variantGroup();
variantGroup.addHeader("key", "value");
addHeaders
The addHeaders method sets multiple headers for the request. Pass a HashMap containing key–value pairs of header names and values.
| Name | Type | Description |
|---|---|---|
| headers | HashMap<String,String> | A HashMap that maps String keys to String values. Common headers include authorization, authtoken, and api_key. |
HashMap<String, String> headers = new HashMap<>();
// Replace AUTHTOKEN and API_KEY with your actual credentials
headers.put("authtoken", "authToken");
headers.put("api_key", "apiKey");
variantGroup.addHeaders(headers);linkContentTypes
The linkContentTypes method links one or more content types to a variant group. Use it to enable personalization by associating content types with variant logic.
| Name | Type | Description |
|---|---|---|
| contentTypeUids | String… | Links one or more content types to a variant group. Accepts a variable number of String arguments. |
import Contentstack;
// Set an Authorization header using a Bearer token
Contentstack contentstack = new Contentstack.Builder().setAuthToken(AUTHTOKEN).build();
// Get the stack instance using the API key
Stack stack = contentstack.stack(API_KEY);
VariantGroup variantgroup = stack.variantGroup();
// Link multiple content types to the variant group using their UIDs
Response<ResponseBody> response = variantGroup.linkContentTypes("contentType1", "contentType2").execute();
if (response.isSuccessful() && response.body() != null) {
System.out.println("Successfully linked: " + response.body().string());
} else {
System.out.println("Error: " + response.errorBody().string());
}unlinkContentTypes
The unlinkContentTypes method removes one or more content types from a variant group. Use this method to detach content types previously linked for personalization.
| Name | Type | Description |
|---|---|---|
| contentTypeUids | String… | Unlinks one or more content types to a variant group. Accepts variable-length String arguments. |
import Contentstack;
// Initialize the Contentstack client using an auth token
Contentstack contentstack = new Contentstack.Builder().setAuthToken(AUTHTOKEN).build();
// Get the stack instance using the API key
Stack stack = contentstack.stack(API_KEY);
// Get the VariantGroup instance
VariantGroup variantgroup = stack.variantGroup();
// Unlink one or more content types from the variant group using their UIDs
Response<ResponseBody> response = variantGroup.unlinkContentTypes("contentType1").execute();
if (response.isSuccessful() && response.body() != null) {
System.out.println("Successfully unlinked: " + response.body().string());
} else {
System.out.println("Error: " + response.errorBody().string());
}setBranches
The setBranches method defines the branches used in the request body when linking or unlinking content types from a variant group. By default, only the main branch is included.
| Name | Type | Description |
|---|---|---|
| branches | List<String> or String… | A list of branch names to apply to the request. Accepts either a List<String> or a variable number of String arguments. |
import Contentstack;
// Initialize the Contentstack client using an auth token
Contentstack contentstack = new Contentstack.Builder().setAuthToken(AUTHTOKEN).build();
// Get the stack instance using the API key
Stack stack = contentstack.stack(API_KEY);
// Get the VariantGroup instance
VariantGroup variantgroup = stack.variantGroup();
// Set the branches used in the request (e.g., main and dev branches)
variantgroup.setBranches("main", "dev");
// Link content types (by UID) with the selected branches
Response<ResponseBody> response = variantGroup.linkContentTypes("contentType").execute();
if (response.isSuccessful() && response.body() != null) {
System.out.println("Successfully linked: " + response.body().string());
} else {
System.out.println("Error: " + response.errorBody().string());
}