Build segments in Amazon Pinpoint - Amazon Pinpoint

Build segments in Amazon Pinpoint

To reach the intended audience for a campaign, build a segment based on the data reported by your app. For example, to reach users who haven’t used your app recently, you can define a segment for users who haven’t used your app in the last 30 days.

For more code examples, see Code examples.

Build segments with the AWS SDK for Java

The following example demonstrates how to build a segment with the AWS SDK for Java. The example creates a segment of users who's team is the Lakers and have been active in the past 30 days. Once the segment has been built you can use it as part of a campaign or journey. For an example of using a segment with a campaign, see Create Amazon Pinpoint campaigns programmatically.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.AttributeDimension; import software.amazon.awssdk.services.pinpoint.model.SegmentResponse; import software.amazon.awssdk.services.pinpoint.model.AttributeType; import software.amazon.awssdk.services.pinpoint.model.RecencyDimension; import software.amazon.awssdk.services.pinpoint.model.SegmentBehaviors; import software.amazon.awssdk.services.pinpoint.model.SegmentDemographics; import software.amazon.awssdk.services.pinpoint.model.SegmentLocation; import software.amazon.awssdk.services.pinpoint.model.SegmentDimensions; import software.amazon.awssdk.services.pinpoint.model.WriteSegmentRequest; import software.amazon.awssdk.services.pinpoint.model.CreateSegmentRequest; import software.amazon.awssdk.services.pinpoint.model.CreateSegmentResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import java.util.HashMap; import java.util.Map;
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.AttributeDimension; import software.amazon.awssdk.services.pinpoint.model.SegmentResponse; import software.amazon.awssdk.services.pinpoint.model.AttributeType; import software.amazon.awssdk.services.pinpoint.model.RecencyDimension; import software.amazon.awssdk.services.pinpoint.model.SegmentBehaviors; import software.amazon.awssdk.services.pinpoint.model.SegmentDemographics; import software.amazon.awssdk.services.pinpoint.model.SegmentLocation; import software.amazon.awssdk.services.pinpoint.model.SegmentDimensions; import software.amazon.awssdk.services.pinpoint.model.WriteSegmentRequest; import software.amazon.awssdk.services.pinpoint.model.CreateSegmentRequest; import software.amazon.awssdk.services.pinpoint.model.CreateSegmentResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import java.util.HashMap; import java.util.Map; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateSegment { public static void main(String[] args) { final String usage = """ Usage: <appId> Where: appId - The application ID to create a segment for. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String appId = args[0]; PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); SegmentResponse result = createSegment(pinpoint, appId); System.out.println("Segment " + result.name() + " created."); System.out.println(result.segmentType()); pinpoint.close(); } public static SegmentResponse createSegment(PinpointClient client, String appId) { try { Map<String, AttributeDimension> segmentAttributes = new HashMap<>(); segmentAttributes.put("Team", AttributeDimension.builder() .attributeType(AttributeType.INCLUSIVE) .values("Lakers") .build()); RecencyDimension recencyDimension = RecencyDimension.builder() .duration("DAY_30") .recencyType("ACTIVE") .build(); SegmentBehaviors segmentBehaviors = SegmentBehaviors.builder() .recency(recencyDimension) .build(); SegmentDemographics segmentDemographics = SegmentDemographics .builder() .build(); SegmentLocation segmentLocation = SegmentLocation .builder() .build(); SegmentDimensions dimensions = SegmentDimensions .builder() .attributes(segmentAttributes) .behavior(segmentBehaviors) .demographic(segmentDemographics) .location(segmentLocation) .build(); WriteSegmentRequest writeSegmentRequest = WriteSegmentRequest.builder() .name("MySegment") .dimensions(dimensions) .build(); CreateSegmentRequest createSegmentRequest = CreateSegmentRequest.builder() .applicationId(appId) .writeSegmentRequest(writeSegmentRequest) .build(); CreateSegmentResponse createSegmentResult = client.createSegment(createSegmentRequest); System.out.println("Segment ID: " + createSegmentResult.segmentResponse().id()); System.out.println("Done"); return createSegmentResult.segmentResponse(); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; } }

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

Segment ID: 09cb2967a82b4a2fbab38fead8d1f4c4

For the full SDK example, see CreateSegment.java on GitHub.