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

@patryk.gruszka I tested your json and it works perfectly, however when I tried to apply the concept to my scenario doing something very simple like sending a single email to a follow-up I got an error in the mautic log saying that the json is poorly formatted. If I put yours, it works perfectly. Do you know where I might be going wrong?

{
    "isPublished": true,
    "name": "Teste sunday mail ",
    "description": "My sunday mail",
    "allowRestart": 0,
    "events": [
        {
            "id": "new1",
            "name": "Enviar email de domingo",
            "type": "email.send",
            "eventType": "action",
            "channel": "email",
            "channelId": 691,
            "order": 0,
            "properties": {
                "canvasSettings": {
                    "droppedX": "621",
                    "droppedY": "155"
                },
                "triggerMode": "immediate",
                "triggerInterval": "1",
                "triggerIntervalUnit": "d",
                "anchor": "leadsource",
                "properties": {
                    "email": "691",
                    "email_type": "transactional",
                    "priority": "2",
                    "attempts": "3"
                }
            },
            "triggerMode": "immediate",
            "parent": null
        }
    ],
    "lists": [
        {  
        	"createdByUser": "Admin User",
            "modifiedByUser": null,
            "id": 123,
            "name": "teste",
            "alias": "teste1"
        }
    ],
    "canvasSettings": {
        "nodes": [
            {
                "id": "new1",
                "positionX": "621",
                "positionY": "155"
            },
            {
                "id": "lists",
                "positionX": "721",
                "positionY": "50"
            }
        ],
        "connections": [
            {
                "sourceId": "lists",
                "targetId": "new1",
                "anchors": {
                    "source": "leadsource",
                    "target": "top"
                }
            }
        ]
    }
}

Coming back to this…
I also find that the triggerDate. its marked as ‘datetime’ in the API spec, but you cannot JSON serialise this. So if I post a string ‘YYYY-mm-dd HH:MM:SS’, I get a stack trace

If i set triggerDate to null, it works, but I need a triggerDate.

Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "JMS\Serializer\Handler\DateHandler::serializeDateTime(): Argument #2 ($date) must be of type DateTime, string given, called in /app/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php on line 209" at /app/vendor/jms/serializer/src/Handler/DateHandler.php line 105 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): JMS\\Serializer\\Handler\\DateHandler::serializeDateTime(): Argument #2 ($date) must be of type DateTime, string given, called in /app/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php on line 209 at /app/vendor/jms/serializer/src/Handler/DateHandler.php:105)

If I do a post of below to /campaigns/new, it gives 500 error. If I replace the triggerDate with null, it passes.

{
  "isPublished": true,
  "name": "test-campaign-1",
  "events": [
    {
      "id": "new0",
      "name": "0: SQL QUAD-1",
      "type": "sql.condition",
      "eventType": "condition",
      "properties": {
        "sql": "300001"
      },
      "triggerDate": "2014-12-22 08:31:53",
      "triggerMode": "date",
      "parent": null,
      "children": []
    }
  ],
  "lists": [1440098]
}

If i create it with ‘null’, and then use PATCH

{
  "id": 330085,
  "events": [
    {
      "id": 361267,
      "triggerDate": "2024-11-11 01:02:03",
      "triggerMode": "date"
    }
  ]
}

i get no error, but it doesn’t stick.