Mautic api for campaign events

Using mautic 4.x on php 7.4

Observations and asks for assistance on the api for campaigns:

  1. Events. The UI can use an endpoint of /s/campaigns/events/new, but there is no analog in the REST API
  2. Read vs write. When you do a GET on a campaign, you end up with a format not suitable for posting back, notably:
  • dates. The publishUp date comes back in iso8601 format (YYYY-mm-ddTHH:MM:SS+offset). But, the post format only accepts YYYY-mm-dd HH:MM:SS without offset and without T.
  • category. on POST, i put category: #, on GET, it comes back as category: {object}
  1. events. Events refer to each other by ID, but, you cannot create an event individually, and, cannot post them in the api w/o the ID.
  2. the campaign interconnects seem to relate to the canvas of display, making it tough to use programatically.

Has anyone any addvice here? I’ve found a messy workaround, i post a campaign with a single event, this fails (500), but, actually creates the campaign and event. I then do a find by name, retreiving the campaign id, then fetch the campaign, then correct all the fields that are now formatted incorrectly. I then fidn the event-id assigned, add a new event without an id, post, repeat. Very slow and error prone.

any suggestions?

Mautic Developer Documentation is the docs, but it shows e.g. a struct for events, but the return value is an array.

i can get 1 event created (by passing in 1 in the array with no id, it creates one), but i cannot get 2 events created (this trick only works one time).

Any suggestions?

{
    "campaign": {
        "id": 3,
        "name": "Email A/B Test",
        "description": null,
        "isPublished": true,
        "publishUp": null,
        "publishDown": null,
        "dateAdded": "2015-07-15T15:06:02-05:00",
        "createdBy": 1,
        "createdByUser": "Joe Smith",
        "dateModified": "2015-07-20T13:11:56-05:00",
        "modifiedBy": 1,
        "modifiedByUser": "Joe Smith",
        "category": null,
        "events": {
            "28": {
                "id": 28,
                "type": "lead.changepoints",
                "eventType": "action",
                "name": "Adjust lead points",
                "description": null,
                "order": 1,
                "properties": {
                  "points": 20
                },
                "triggerDate": null,
                "triggerInterval": 1,
                "triggerIntervalUnit": "d",
                "triggerMode": "immediate",
                "children": [],
                "parent": null,
                "decisionPath": null
            }
        }
    }
}

Hi @donbowman - I was able to create events, but ran into other trouble as well: API for campaigns broken? - Support - Mautic Community Forums

Hello donbowman, you made any progress in creating events via API? (Creating events via API should be simpler.)

You can use the create campaign API endpoint to create the events.

The payload should contain:

  • The starting segment or form definition (lists or forms in the JSON)
  • Events temporary IDs starting with the keyword ‘new’, they will be replaced with real IDs when the campaign is created
  • Correct references to parent events
  • Nodes and connections settings in canvasSettings

Here is an example payload POST /api/campaigns/new:

{
    "isPublished": true,
    "name": "API Campaign",
    "description": null,
    "allowRestart": 0,
    "events": [
        {
            "id": "new1",
            "name": "Contact field value",
            "type": "lead.field_value",
            "eventType": "condition",
            "order": 1,
            "properties": {
                "canvasSettings": {
                    "droppedX": "520",
                    "droppedY": "155"
                },
                "name": "",
                "triggerMode": "immediate",
                "triggerDate": null,
                "triggerInterval": "1",
                "triggerIntervalUnit": "d",
                "triggerHour": "",
                "triggerRestrictedStartHour": "",
                "triggerRestrictedStopHour": "",
                "anchor": "leadsource",
                "properties": {
                    "field": "email",
                    "operator": "endsWith",
                    "value": "example.com"
                },
                "type": "lead.field_value",
                "eventType": "condition",
                "anchorEventType": "source",
                "buttons": {
                    "save": ""
                },
                "field": "email",
                "operator": "endsWith",
                "value": "example.com"
            },
            "triggerDate": null,
            "triggerInterval": 1,
            "triggerIntervalUnit": "d",
            "triggerHour": null,
            "triggerRestrictedStartHour": null,
            "triggerRestrictedStopHour": null,
            "triggerRestrictedDaysOfWeek": [],
            "triggerMode": "immediate",
            "decisionPath": null,
            "parent": null
        },
        {
            "id": "new2",
            "name": "Modify contact's tags",
            "type": "lead.changetags",
            "eventType": "action",
            "order": 2,
            "properties": {
                "canvasSettings": {
                    "droppedX": "710",
                    "droppedY": "291"
                },
                "name": "",
                "triggerMode": "immediate",
                "triggerDate": null,
                "triggerInterval": "1",
                "triggerIntervalUnit": "d",
                "triggerHour": "",
                "triggerRestrictedStartHour": "",
                "triggerRestrictedStopHour": "",
                "anchor": "yes",
                "properties": {
                    "add_tags": [
                        "2"
                    ]
                },
                "type": "lead.changetags",
                "eventType": "action",
                "anchorEventType": "condition",
                "buttons": {
                    "save": ""
                },
                "triggerRestrictedDaysOfWeek": [],
                "add_tags": [
                    "example"
                ],
                "remove_tags": []
            },
            "triggerDate": null,
            "triggerInterval": 1,
            "triggerIntervalUnit": "d",
            "triggerHour": null,
            "triggerRestrictedStartHour": null,
            "triggerRestrictedStopHour": null,
            "triggerRestrictedDaysOfWeek": [],
            "triggerMode": "immediate",
            "decisionPath": "yes",
            "parent": {"id": "new1"}
        }
    ],
    "forms": [],
    "lists": [
        {
            "createdByUser": "Admin User",
            "modifiedByUser": null,
            "id": 9,
            "name": "Segment Test 1",
            "publicName": "Segment Test 1",
            "alias": "segment-test-1",
            "description": null,
            "category": null
        }
    ],
    "canvasSettings": {
        "nodes": [
            {
                "id": "new1",
                "positionX": "810",
                "positionY": "186"
            },
            {
                "id": "new2",
                "positionX": "710",
                "positionY": "291"
            },
            {
                "id": "lists",
                "positionX": "833",
                "positionY": "50"
            }
        ],
        "connections": [
            {
                "sourceId": "lists",
                "targetId": "new1",
                "anchors": {
                    "source": "leadsource",
                    "target": "top"
                }
            },
            {
                "sourceId": "new1",
                "targetId": "new2",
                "anchors": {
                    "source": "yes",
                    "target": "top"
                }
            }
        ]
    }
}
1 Like