Menu
AWS SDK for Java
Developer Guide

Creating Segments in Amazon Pinpoint

A user segment represents a subset of your users that's based on shared characteristics, such as how recently a user opened your app or which device they use. The following example demonstrates how to define a segment of users.

Create a Segment

Create a new segment in Amazon Pinpoint by defining dimensions of the segment in a SegmentDimensions object.

Imports

import com.amazonaws.services.pinpoint.AmazonPinpoint; import com.amazonaws.services.pinpoint.AmazonPinpointClientBuilder; import com.amazonaws.services.pinpoint.model.CreateSegmentRequest; import com.amazonaws.services.pinpoint.model.CreateSegmentResult; import com.amazonaws.services.pinpoint.model.AttributeDimension; import com.amazonaws.services.pinpoint.model.AttributeType; import com.amazonaws.services.pinpoint.model.RecencyDimension; import com.amazonaws.services.pinpoint.model.SegmentBehaviors; import com.amazonaws.services.pinpoint.model.SegmentDemographics; import com.amazonaws.services.pinpoint.model.SegmentDimensions; import com.amazonaws.services.pinpoint.model.SegmentLocation; import com.amazonaws.services.pinpoint.model.SegmentResponse; import com.amazonaws.services.pinpoint.model.WriteSegmentRequest;

Code

Pinpoint pinpoint = AmazonPinpointClientBuilder.standard().withRegion(Regions.US_EAST_1).build(); Map<String, AttributeDimension> segmentAttributes = new HashMap<>(); segmentAttributes.put("Team", new AttributeDimension().withAttributeType(AttributeType.INCLUSIVE).withValues("Lakers")); SegmentBehaviors segmentBehaviors = new SegmentBehaviors(); SegmentDemographics segmentDemographics = new SegmentDemographics(); SegmentLocation segmentLocation = new SegmentLocation(); RecencyDimension recencyDimension = new RecencyDimension(); recencyDimension.withDuration("DAY_30").withRecencyType("ACTIVE"); segmentBehaviors.setRecency(recencyDimension); SegmentDimensions dimensions = new SegmentDimensions() .withAttributes(segmentAttributes) .withBehavior(segmentBehaviors) .withDemographic(segmentDemographics) .withLocation(segmentLocation);

Next set the SegmentDimensions object in a WriteSegmentRequest, which in turn is used to create a CreateSegmentRequest object. Then pass the CreateSegmentRequest object to the AmazonPinpointClient's createSegment method.

Code

WriteSegmentRequest writeSegmentRequest = new WriteSegmentRequest() .withName("MySegment").withDimensions(dimensions); CreateSegmentRequest createSegmentRequest = new CreateSegmentRequest() .withApplicationId(appId).withWriteSegmentRequest(writeSegmentRequest); CreateSegmentResult createSegmentResult = client.createSegment(createSegmentRequest);

See the complete example on GitHub.

More Information