Skip to content

How to remove filter/ segmentation from campaign via API? #18

@franz-josef-kaiser

Description

@franz-josef-kaiser

Task

House cleaning to not end up in an unmaintainable state with left overs all over the place:

  1. Add a new /contactfilter resource
  2. Add the new contactfilter to a /campaigndraft resource
  3. Delete the contact filter
  4. (Maybe:) Remove it from the campaigndraft first?

(Note: This is a follow up during the work on #15 while testing setups).

Steps to reproduce

1. Create the filter

  • (optional) Install jq or json_pp (prefer the further) for human readable JSON output in the shell

  • Create a file named mjkey (w/o extension) and store nothing than the $APIKEY:$SECRET, that you can find in the MailJet Webapp: https://app.mailjet.com/account/api_keys

  • Create a second file, named filters-create.sh and store the following contents:

      #!/usr/bin/env bash
      # @example sh filter-create.sh $(cat mjkey) ${FILTER_NAME}.json
      # @example sh filter-create.sh $(cat mjkey) expr_puchase_avg.json | jq # or: json_pp for pretty/ human-readable JSON format
      curl -sX POST https://api.mailjet.com/v3/REST/contactfilter \
          --user "${1}" \
          --header 'Content-Type: application/json' \
          -d @${2}
    
  • Create a contact meta data entry named purchase in the MailJet Webapp: https://app.mailjet.com/contacts/lists/properties

  • Create a file named filter.json with the following contents:

          {
              "Description": "Contactfilter to test deletion of itself.",
              "Name": "DeleteMe"
              "Expression": "max(1,purchase) > 50"
              "": ""
          }
    
  • Call the file in your CLI/ terminal/ shell like in the following example. Pipe the output to jq:

      $ sh filter-create.sh $(cat mjkey) filter.json |jq
    

    Write down the Segmentation ID from the response (or query it via the API GET–request at https://api.mailjet.com/v3/REST/contactfilter later on).

2. Attach the filter to a /campaigndraft resource

– Create a file named campaign-create.sh with the following contents:

    #!/usr/bin/env bash
    # @example sh campaign-create.sh $(cat mjkey) campaign.json
    # @example sh campaign-create.sh $(cat mjkey) campaign.json | jq # or: json_pp for pretty/ human-readable JSON format
    curl -sX POST https://api.mailjet.com/v3/REST/campaigndraft \
        --user "${1}" \
        -H "Content-Type: application/json" \
        -d @${2}

– Create a file named campaign.json with the following contents and substitute ContactsListID with the ID of an existing contacts list and segmentation ID :

            {
                    "Locale"         : "en_US",
                    "Sender"         : "API Testuser",
                    "SenderEmail"    : "[email protected]",
                    "Subject"        : "Hello, nothing to see here",
                    "Title"          : "Draft to serve as testcase for an attached segmentation.",
                    "ContactsListID" : 20070,
                    "SegmentationID" : 365
            }

3. Attempt to delete the filter again

  • List all filters. Create a file named filters-list.sh:

      #!/usr/bin/env bash
      # @example sh filters-list.sh $(cat mjkey)
      # @example sh filters-list.sh $(cat mjkey) | jq # or: json_pp for pretty/ human-readable JSON format
      curl -sX GET https://api.mailjet.com/v3/REST/contactfilter \
          --user "${1}" \
          --header 'Content-Type: application/json'
    
  • Execute the file in your CLI

      $ sh filters-list.sh $(cat mjkey) |jq
    

    Response:

      {
        "Count": 1,
        "Data": [
          {
            "Description": "Contactfilter to test deletion of itself.",
            "Expression": "max(1,purchase) > 50",
            "ID": 365,
            "Name": "DeleteMe",
            "Status": "used"
          }
        ],
        "Total": 1
      }
    
  • Delete the filter again: Create a file named filter-del.sh and add the following contents:

      #!/usr/bin/env bash
      # @example sh filter-del.sh $(cat mjkey) 123
      # @example sh filter-del.sh $(cat mjkey) "Users above 50"
      # @example sh filter-del.sh $(cat mjkey) 123 | jq # or: json_pp for pretty/ human-readable JSON format
      curl -sX DELETE "https://api.mailjet.com/v3/REST/contactfilter/${2}" \
          --user "${1}" \
          --header 'Content-Type: application/json'
    
  • Call the file in your CLI

       $ sh filter-del.sh $(cat mjkey) 365 |jq
    

    Response:

      {
        "ErrorInfo": "",
        "ErrorMessage": "Method not allowed: Filter is used",
        "StatusCode": 405
      } 
    

Other attempts

I tried to remove the segmentation from existing /campaigndraft resources by updating the resource:

curl -s -X PUT --user $(cat mjkey) https://api.mailjet.com/v3/REST/campaigndraft/15056 -H 'Content-Type: application/json' -d '{"SegmentationID": "0"}' |jq

The response was the following:

{
  "ErrorInfo": "",
  "ErrorMessage": "Newsletter segmentation must match AXTesting.Segmentation",
  "StatusCode": 400
}

I then updated the AXTesting value as well:

curl -s -X PUT --user $(cat mjkey) https://api.mailjet.com/v3/REST/campaigndraft/15056 -H 'Content-Type: application/json' -d '{"SegmentationID": 0, "AXTestingID":0}' |jq

The response was the following:

{
  "Count": 1,
  "Data": [
    {
      "AXFraction": 0,
      "AXFractionName": "",
      "AXTestingID": 0,
      "ContactsListID": 20070,
      "CreatedAt": "2018-08-23T15:53:25Z",
      "Current": 94213,
      "DeliveredAt": "",
      "EditMode": "tool2",
      "ID": 15056,
      "IsStarred": false,
      "IsTextPartIncluded": false,
      "Locale": "en_US",
      "ModifiedAt": "2018-08-23T15:54:25Z",
      "Preset": "{}",
      "SegmentationID": 0,
      "Sender": "",
      "SenderEmail": "[email protected]",
      "SenderName": "Testuser",
      "Status": 0,
      "Subject": "FooBar",
      "TemplateID": 513130,
      "Title": "Ungarn–Kampagne",
      "Url": "",
      "Used": false
    }
  ],
  "Total": 1
}

Finally I tried to update the /contactsfilter value Status to unused:

curl -s -X PUT --user $(cat mjkey) https://api.mailjet.com/v3/REST/contactfilter/365 -H 'Content-Type: application/json' -d '{"Status":"unused"}' |jq

The response was the following:

{
  "ErrorInfo": "",
  "ErrorMessage": "Cannot change status from used to unused",
  "StatusCode": 400
}

Expected Outcome / Behavior

I expected to be able to do one of the following:

  • remove the /contactsfilter resource via the API and the connections/ dependencies/ links update themselves.
  • (at least be able to) search up the linked resources where the contactsfilter is used and remove the /contactsfilter by ID and then be able to delete it.

Questions:

  • How can I delete an existing contacts filter resource and all its references?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions