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
6
7
8
            'group' => $newSegmentGroup,
        ]);

        $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
        $segment = $this->segmentationService->loadSegmentByIdentifier('segment_1');

        $this->segmentationService->assignUserToSegment($user, $segment);

Assigning users

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

1
            $output->writeln('Segment identifier: ' . $segment->getIdentifier() . ', name: ' . $segment->getName());

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
7
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $user = $this->userService->loadUserByLogin('admin');
        $this->permissionResolver->setCurrentUserReference($user);

        $segmentGroupCreateStruct = new SegmentGroupCreateStruct([
            'name' => 'Custom Group',

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
7
            'createSegments' => [],
        ]);

        $newSegmentGroup = $this->segmentationService->createSegmentGroup($segmentGroupCreateStruct);

        $segmentCreateStruct = new SegmentCreateStruct([
            'name' => 'Segment 1',

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);