Create an A/B test Amazon Pinpoint campaign with the AWS SDK for Java - Amazon Pinpoint

End of support notice: On October 30, 2026, AWS will end support for Amazon Pinpoint. After October 30, 2026, you will no longer be able to access the Amazon Pinpoint console or Amazon Pinpoint resources (endpoints, segments, campaigns, journeys, and analytics). For more information, see Amazon Pinpoint end of support. Note: APIs related to SMS, voice, mobile push, OTP, and phone number validate are not impacted by this change and are supported by AWS End User Messaging.

Create an A/B test Amazon Pinpoint campaign with the AWS SDK for Java

An A/B test campaign behaves like a standard campaign, but enables you to define different treatments for the campaign message or schedule. An A/B test includes two or more treatments of the message or schedule. Treatments are variations of your message or schedule. As your users respond to the campaign, you can view campaign analytics to compare the effectiveness of each treatment.

The following example demonstrates how to create an A/B test campaign with the AWS SDK for Java.

import com.amazonaws.services.pinpoint.AmazonPinpointClient; import com.amazonaws.services.pinpoint.model.Action; import com.amazonaws.services.pinpoint.model.CampaignResponse; import com.amazonaws.services.pinpoint.model.CreateCampaignRequest; import com.amazonaws.services.pinpoint.model.CreateCampaignResult; import com.amazonaws.services.pinpoint.model.Message; import com.amazonaws.services.pinpoint.model.MessageConfiguration; import com.amazonaws.services.pinpoint.model.Schedule; import com.amazonaws.services.pinpoint.model.WriteCampaignRequest; import com.amazonaws.services.pinpoint.model.WriteTreatmentResource; import java.util.ArrayList; import java.util.List; public class PinpointCampaignSample { public CampaignResponse createAbCampaign(AmazonPinpointClient client, String appId, String segmentId) { Schedule schedule = new Schedule() .withStartTime("IMMEDIATE"); // Default treatment. Message defaultMessage = new Message() .withAction(Action.OPEN_APP) .withBody("My message body.") .withTitle("My message title."); MessageConfiguration messageConfiguration = new MessageConfiguration() .withDefaultMessage(defaultMessage); // Additional treatments WriteTreatmentResource treatmentResource = new WriteTreatmentResource() .withMessageConfiguration(messageConfiguration) .withSchedule(schedule) .withSizePercent(40) .withTreatmentDescription("My treatment description.") .withTreatmentName("MyTreatment"); List<WriteTreatmentResource> additionalTreatments = new ArrayList<WriteTreatmentResource>(); additionalTreatments.add(treatmentResource); WriteCampaignRequest request = new WriteCampaignRequest() .withDescription("My description.") .withSchedule(schedule) .withSegmentId(segmentId) .withName("MyCampaign") .withMessageConfiguration(messageConfiguration) .withAdditionalTreatments(additionalTreatments) .withHoldoutPercent(10); // Hold out of A/B test CreateCampaignRequest createCampaignRequest = new CreateCampaignRequest() .withApplicationId(appId).withWriteCampaignRequest(request); CreateCampaignResult result = client.createCampaign(createCampaignRequest); System.out.println("Campaign ID: " + result.getCampaignResponse().getId()); return result.getCampaignResponse(); } }

When you run this example, the following is printed to the console window of your IDE:

Campaign ID: b1c3de717aea4408a75bb3287a906b46