Segments
Use this endpoint to obtain details on Mautic’s Contact Segments or to manipulate Contact memberships.
Using Mautic’s API Library
You can interact with this API through the Mautic API Library as follows, or use the various http endpoints as described in this document.
<?php
use Mautic\MauticApi;
use Mautic\Auth\ApiAuth;
// ...
$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings);
$apiUrl = "https://example.com";
$api = new MauticApi();
$segmentApi = $api->newApi("segments", $auth, $apiUrl);
Get Segment
<?php
//...
$segment = $segmentApi->get($id);
Get an individual Segment by ID.
HTTP Request
GET /segments/ID
Response
Expected Response Code: 200
"list": {
"id": 47,
"isPublished": 1,
"dateAdded": "2015-07-21T12:27:12-05:00",
"createdBy": 1,
"createdByUser": "Joe Smith",
"dateModified": "2015-07-21T14:12:03-05:00",
"modifiedBy": 1,
"modifiedByUser": "Joe Smith",
"category": null,
"name": "Segment A",
"alias": "segment-a",
"description": "This is my first Segment created via API.",
"filters": [
{
"glue": "and",
"field": "city",
"type": "text",
"filter": "Prague",
"display": null,
"operator": "=",
}
],
"isGlobal": true
}
Segment Properties
Name |
Type |
Description |
---|---|---|
|
int |
ID of the Segment |
|
boolean |
Published state |
|
|
Segment creation date/time |
|
int |
ID of the User that created the Segment |
|
string |
Name of the User that created the Segment |
|
datetime/null |
Date/time Segment was last modified |
|
int |
ID of the User that last modified the Segment |
|
string |
Name of the User that last modified the Segment |
|
object/null |
Object with the Category details |
|
string |
Segment name |
|
string |
Segment alias |
|
string |
Segment description |
|
array |
Smart filters for the Segment. See filter properties bellow |
|
boolean |
Whether the Segment is global. 0 means only the author can see it. |
Segment Filter Properties
Name |
Type |
Description |
---|---|---|
|
string |
How to glue the filters to others. Possible values: |
|
string |
Alias of the Contact or Company field to based the filter on |
|
string |
Object which have the field. Possible values: |
|
string |
Type of the field. Possible values: ‘boolean’, |
|
string |
Operator used for matching the values. Possible values: ‘=’, |
List Contact Segments
<?php
//...
$segments = $segmentApi->getList($searchFilter, $start, $limit, $orderBy, $orderByDir, $publishedOnly, $minimal);
Returns a list of Contact Segments available to the User. This list isn’t filterable.
HTTP Request
GET /segments
Query Parameters
Name |
Description |
---|---|
|
String or search command to filter entities by. |
|
Starting row for the entities returned. Defaults to 0. |
|
Limit number of entities to return. Defaults to the system configuration for pagination - defaults to 30. |
|
Column to sort by. Can use any column listed in the response. |
|
Sort direction: |
|
Only return currently published entities. |
Response
Expected Response Code: 200
{
"total": 13,
"lists": [
{
"id": 47,
"isPublished": 1,
"dateAdded": "2015-07-21T12:27:12-05:00",
"createdBy": 1,
"createdByUser": "Joe Smith",
"dateModified": "2015-07-21T14:12:03-05:00",
"modifiedBy": 1,
"modifiedByUser": "Joe Smith",
"category": null,
"name": "Segment A",
"alias": "segment-a",
"description": "This is my first Segment created via API.",
"filters": [
{
"glue": "and",
"field": "city",
"type": "text",
"filter": "Prague",
"display": null,
"operator": "=",
}
],
"isGlobal": true
}
]
}
Segment Properties
Name |
Type |
Description |
---|---|---|
|
int |
Count of all Segments |
|
int |
ID of the Segment |
|
boolean |
Published state |
|
|
Segment creation date/time |
|
int |
ID of the User that created the Segment |
|
string |
Name of the User that created the Segment |
|
datetime/null |
Date/time Segment was last modified |
|
int |
ID of the User that last modified the Segment |
|
string |
Name of the User that last modified the Segment |
|
object/null |
Object with the Category details |
|
string |
Segment name |
|
string |
Segment alias |
|
string |
Segment description |
|
array |
Smart filters for the Segment. See filter properties bellow |
|
boolean |
Whether the Segment is global. 0 means only the author can see it. |
Segment Filter Properties
Name |
Type |
Description |
---|---|---|
glue |
string |
How to glue the filters to others. Possible values: |
field |
string |
Alias of the Contact or Company field to based the filter on |
object |
string |
Object which have the field. Possible values: |
type |
string |
Type of the field. Possible values: ‘boolean’, |
operator |
string |
Operator used for matching the values. Possible values: ‘=’, |
Create Segment
<?php
$data = array(
'name' => 'Segment A',
'alias' => 'segment-a',
'description' => 'This is my first Segment created via API.',
'isPublished' => 1,
'filters' => array(
array(
'glue' => 'and',
'field' => 'email',
'object' => 'lead',
'type' => 'email',
'filter' => '*@gmail.com',
'operator' => 'like',
),
),
);
$segment = $segmentApi->create($data);
Create a new Segment.
HTTP Request
POST /segments/new
POST Parameters
Name |
Type |
Description |
---|---|---|
|
string |
Segment name is the only required field |
|
string |
Name alias generated automatically if not set |
|
string |
A description of the Segment. |
|
int |
A value of 0 or 1 |
|
boolean |
Whether the Segment is global. 0 means only the author can see it. |
|
array |
Array of filters. See possible properties bellow. |
Segment Filter Properties
Name |
Type |
Description |
---|---|---|
|
string |
How to glue the filters to others. Possible values: |
|
string |
Alias of the Contact or Company field to based the filter on |
|
string |
Object which have the field. Possible values: |
|
string |
Type of the field. Possible values: ‘boolean’, |
|
string |
Operator used for matching the values. Possible values: ‘=’, |
Response
Expected Response Code: 201
Properties
Same as Get Segment.
Edit Segment
<?php
$id = 1;
$data = array(
'name' => 'New Segment name',
'isPublished' => 0
);
// Create new a Segment of ID 1 isn't found?
$createIfNotFound = true;
$segment = $segmentApi->edit($id, $data, $createIfNotFound);
Edit a new Segment. Note that this supports PUT or PATCH depending on the desired behavior.
PUT creates a Segment if the given ID doesn’t exist and clears all the Segment information, adds the information from the request.
PATCH fails if the Segment with the given ID doesn’t exist and updates the Segment field values with the values from the request.
HTTP Request
To edit a Segment and return a 404 if the Segment isn’t found:
PATCH /segments/ID/edit
To edit a Segment and create a new one if the Segment isn’t found:
PUT /segments/ID/edit
POST Parameters
Name |
Type |
Description |
---|---|---|
|
string |
Segment name is the only required field |
|
string |
Name alias generated automatically if not set |
|
string |
A description of the Segment. |
|
int |
A value of 0 or 1 |
|
boolean |
Whether the Segment is global. 0 means only the author can see it. |
|
array |
Array of filters. See possible properties bellow. |
Segment Filter Properties
Name |
Type |
Description |
---|---|---|
|
string |
How to glue the filters to others. Possible values: |
|
string |
Alias of the Contact or Company field to based the filter on |
|
string |
Object which have the field. Possible values: |
|
string |
Type of the field. Possible values: ‘boolean’, |
|
string |
Operator used for matching the values. Possible values: ‘=’, |
Response
If PUT
, the expected response code is 200
if editing a Segment or 201
if creating a new one.
If PATCH
, the expected response code is 200
.
Properties
Same as Get Segment.
Delete Segment
<?php
$segment = $segmentApi->delete($id);
Delete a Segment.
HTTP Request
DELETE /segments/ID/delete
Response
Expected Response Code: 200
Properties
Same as Get Segment.
Add Contact to a Segment
<?php
//...
$response = $segmentApi->addContact($segmentId, $contactId);
if (!isset($response['success'])) {
// handle error
}
Manually add a Contact to a specific Segment.
HTTP Request
POST /segments/SEGMENT_ID/contact/CONTACT_ID/add
Response
Expected Response Code: 200
{
"success": true
}
Add Contacts to a Segment
<?php
//...
$contactIds = ['ids'=>[ 1, 45, 39]];
$response = $segmentApi->addContact($segmentId, $contactIds);
if (!isset($response['success'])) {
// handle error
}
Manually add Contacts to a specific Segment.
HTTP Request
POST /segments/SEGMENT_ID/contacts/add
Response
Expected Response Code: 200
{
"success":true,
"details":{
"1" :{"success":true},
"45":{"success":true},
"39":{"success":false}
}
}
Remove Contact from a Segment
<?php
//...
$response = $segmentApi->removeContact($segmentId, $contactId);
if (!isset($response['success'])) {
// handle error
}
Manually remove a Contact to a specific Segment.
HTTP Request
POST /segments/SEGMENT_ID/contact/CONTACT_ID/remove
Response
Expected Response Code: 200
{
"success": true
}