Skip to content

Segment API

Segments enable you to profile the content displayed to specific users.

To manage segments, use the SegmentationService.

Getting segment information

To load a segment group, use SegmentationService::loadSegmentGroupByIdentifier(). Get all segments assigned to the group with SegmentationService::loadSegmentsAssignedToGroup():

1
2
3
4
5
        $newSegment = $this->segmentationService->createSegment($segmentCreateStruct);

        $segmentGroup = $this->segmentationService->loadSegmentGroupByIdentifier('custom_group');

        $segments = $this->segmentationService->loadSegmentsAssignedToGroup($segmentGroup);

Similarly, you can load a segment by using SegmentationService::loadSegmentByIdentifier():

1

Checking assignment

You can check whether a user is assigned to a segment with SegmentationService::isUserAssignedToSegment():

1
2
3
4
        $this->segmentationService->assignUserToSegment($user, $segment);

        $output->writeln((
            $this->segmentationService->isUserAssignedToSegment($user, $segment)

Assigning users

To assign a user to a segment, use SegmentationService::assignUserToSegment():

1

Creating segments

Each segment must be assigned to a segment group.

To create a segment group, use SegmentationService::createSegmentGroup() and provide it with a SegmentGroupCreateStruct:

1
2
3
4
5
6
        $this->permissionResolver->setCurrentUserReference($user);

        $segmentGroupCreateStruct = new SegmentGroupCreateStruct([
            'name' => 'Custom Group',
            'identifier' => 'custom_group',
            'createSegments' => [],

To add a segment, use SegmentationService::createSegment() and provide it with a SegmentCreateStruct, which takes an existing group as one of the parameters:

1
2
3
4
5
6
        $newSegmentGroup = $this->segmentationService->createSegmentGroup($segmentGroupCreateStruct);

        $segmentCreateStruct = new SegmentCreateStruct([
            'name' => 'Segment 1',
            'identifier' => 'segment_1',
            'group' => $newSegmentGroup,

Updating segments

To update a segment or a segment group, use SegmentationService::updateSegment() or SegmentationService::updateSegmentGroup() and provide it with SegmentUpdateStruct or SegmentGroupUpdateStruct.

Deleting segments

To delete a segment or a segment group, use SegmentationService::removeSegment() or SegmentationService::removeSegmentGroup():

1
$this->segmentationService->removeSegmentGroup($group);