When I save a campaign programmatically using the function mailchimp_campaign_save_campaign
And I want to have a new segments_opts without saved_segment_id specified, i'm getting:
An error occurred testing a list segment: 404: Resource Not Found - The requested resource could not be found.

The problem is in this part-
mailchimp\modules\mailchimp_campaign\mailchimp_campaign.module line 91:

  // Test for valid list segment, if selected.
  if (isset($recipients->segment_opts)) {
    if (mailchimp_test_list_segment($recipients->list_id, $recipients->segment_opts->saved_segment_id) === NULL) {
      return NULL;
    }
  }

And this function:
mailchimp_test_list_segment that requires a value for $recipients->segment_opts->saved_segment_id .

It should be an optional field, and in the case of missing this parameter- a new segment should be created.

From MailChimp documentation: campaigns -> recipients -> segment_opts -
https://developer.mailchimp.com/documentation/mailchimp/reference/campai...

segment_opts	
An object representing all segmentation options. This object should contain a saved_segment_id to use an existing segment, or you can create a new segment by including both match and conditions options.
 

Example of $recipients variable to be sent to mailchimp_campaign_save_campaign function -

  'list_id' => string '123'
  'segment_opts' => 
      'match' => 'any'
      'conditions' => 
        0 => 
          'condition_type' => 'Interests'
          'op' => string 'interestcontainsall'
          'field' => string 'interests-1e2351'
          'value' => 
            0 => string 'a11'

Comments

tamarpe created an issue. See original summary.

tamarpe’s picture

Issue summary: View changes
tamarpe’s picture

Title: A campaign with Segment Options must contain a saved_segment_id » saved_segment_id attribute should be optional when a campaign has segment options
tamarpe’s picture

StatusFileSize
new697 bytes
tamarpe’s picture

Status: Active » Needs review
firfin’s picture

Status: Needs review » Reviewed & tested by the community

I have tested this and it does circumnavigate the problem of saved_segment_id being hardcoded into the check. Problem solved.
Now I can continue my work on #2999677: Add support for Multiple Tags Thanks @tamarpe

But ..
Not sure if this is the correct way to solve the issue though, now we are not testing at all if 'saved_segement_id' is not present. Should we instead not test if there are 'match' and 'conditions' parameters in segment_opts? We can not really check if it is a valid segment though, because it has yet to be created.

Leaving this for the maintainers to decide.

firfin’s picture

Priority: Normal » Major

Raising this to Major as this block use of any other segmentation (on of the main strengths of Mailchimp) than 'saved_segment_id'.

firfin’s picture

brendanthinkshout’s picture

StatusFileSize
new813 bytes

Thanks to both of you for some good catches here. @firfin, is this patch what you had in mind?

brendanthinkshout’s picture

Assigned: Unassigned » brendanthinkshout
firfin’s picture

I think that is a pretty good solution @brendanthinkshout. I will test the patch this week.

firfin’s picture

Applied cleanly and seems to lift the block for using something other than 'saved_segment_id'.
Thanks Brendan!

  • brendanthinkshout committed e23d397 on 8.x-1.x
    Issue #3079700 by tamarpe, brendanthinkshout, firfin: saved_segment_id...
firfin’s picture

This not totally done after all it seems ,follow-up in #3156283: Can not save campaign without 'saved_segment_id'

Also added issue about this same issue, but in D7 #3054670: Can't create new campaigns with non existing segmentation rules

brendanthinkshout’s picture

Status: Reviewed & tested by the community » Fixed

Following up in the @firfin's new issues.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.