Peri Hartman
Peri Hartman

Reputation: 19484

mailchimp: how to set a merge var value for a specific list member

What API in mailchimp should I use to set a merge var value for a specific list member?

If I'm adding a new list member, I can use lists/subscribe to set a merge var value for that new member. However, I do not see an API to change the value if the list member already exists.

The API lists/merge-var-set sets the value for all list members. And lists/merge-var-update sets attributes for the merge var.

What is the API for setting a specific value?

Upvotes: 6

Views: 2798

Answers (4)

Félix Paradis
Félix Paradis

Reputation: 6041

Using Mailchimp api v3's npm package

After cursing Mailchimp for 30 minutes I realized you need to send the merge_fields object inside the body. Else it will fail silently -_-'

This does not work

mailchimp.patch({
    path: '/lists/666trolol0/members/' + emailHash,
    "merge_fields" : { 
        "FNAME:": "whatever" 
    }
}, ...

This does work

mailchimp.patch({
    path: '/lists/666trolol0/members/' + emailHash,
    body: {
        "merge_fields" : { 
            "FNAME:": "whatever" 
        }
    }
}
}, ...

Upvotes: 1

WEBjuju
WEBjuju

Reputation: 6581

I spent some time circling the landing zone on this one. Perhaps a v3 api Mailchimp, update lists member merge_field key value pair php class will help clarify this for someone.

It seems self explanatory, but I would be delighted if what is unclear were called out to me; I'll be happy to update this post with clarification.

In short, lukasz-wiktor's answer is correct; however, I was apparently stuck on the escaping necessary for the curl php passthru command. Note that addslashes() in the below mc::get_url() method solved my issue.


<?php

// v3 api http basic authentication
class mc {

  public $show_cmd = 1;

  function __construct($api_key) {
    $this->api_key = $api_key;
  }

  function update_member_merge_field($list_id, $mem_id, $fld, $val) {
    //lists/{list_id}/members/{subscriber_hash}
    $uri = 'lists/'.$list_id.'/members/'.$mem_id;
    $data = array(
      'merge_fields' => array(
        $fld => $val,
      )
    );
    $json = $this->get_url($uri, 'PATCH', $data);
    die('<pre>'.print_r($json->merge_fields,true).PHP_EOL);
  }

  function get_list_members($list_id, $show_everything=0) {
    //lists/{list_id}
    $uri = 'lists/'.$list_id.'/members';
    $json = $this->get_url($uri);
    if (empty($json->members)) {
      exit('hmmm, do you have any members in this list?'.PHP_EOL);
    } else {
      foreach ($json->members as &$mem) {
        echo ' use id "'.$mem->id.'" for member "'.substr($mem->email_address, 0, 100).PHP_EOL;
      }
    }
    if (! empty($show_everything)) {
      die('<pre>'.print_r($json,true).PHP_EOL);
    }
  }

  function get_lists($show_everything=0) {
    //lists
    $uri = 'lists/';
    $json = $this->get_url($uri);
    if (empty($json->lists)) {
      exit('hmmm, do you have any lists?'.PHP_EOL);
    } else {
      foreach ($json->lists as &$list) {
        echo ' use id "'.$list->id.'" for list "'.substr($list->name, 0, 100).PHP_EOL;
      }
    }
    if (! empty($show_everything)) {
      die('<pre>'.print_r($json,true).PHP_EOL);
    }
  }

  private function get_url($uri, $method='GET', $data='') {
    $x = $this->api_key;
    $pos = strrpos($x, '-');
    $datacenter = substr($x, ($pos + 1));
    if ($method == 'GET') {
      // be aware of pagination...count is 10 by default...not sure of max, but 100 works
      $uri .= '?offset=0&count=100';
    }

    // be careful about accepting unclean input here
    // this is designed for developer use only where
    // the four vars: method, datacenter, uri, and x
    // are know to contain only appropriate content!

    $cmd = "curl --request $method --url 'https://$datacenter.api.mailchimp.com/3.0/$uri' --user 'apijuju:$x'";
    if (! empty($data)) {
      $cmd .= ' --data '.addslashes(json_encode($data));
    }

    if ($this->show_cmd) echo $cmd.'<br><br>';

    ob_start(); passthru($cmd); $out = ob_get_contents(); ob_end_clean();
    $ret = json_decode(trim($out));

    return $ret;
  }
}

/*
// EXAMPLE USAGE:

// to find api_key use dropdown under profile pic...
// find it under "Account" => "Extras" => "API Keys"
$api_key = 'examplenhg93ehqle33elqhik32nh-us1';

// to find $list_id see mailchimp::get_lists(1);
$list_id = 'exampleihqle39';
// to find $mem_id see mailchimp::get_list_members($list_id, 1);
$mem_id = 'examplehk32nhg93elqhiexamplehk32nhg93elqhi';

$merge_field = 'NAME';
$merge_value = 'Joe Momma';

$mc = new mailchimp($api_key);
$mc->update_member_merge_field($list_id, $mem_id, $merge_field, $merge_value);
*/

Upvotes: 0

Lukasz Wiktor
Lukasz Wiktor

Reputation: 20422

In Mailchimp API v3 you can set a merge field value this way:

PUT {api_endpoint}/3.0/lists/{list_id}/members/{member_hash}

{
    "merge_fields": {
        "BIRTHDAY": "01/23"
    }
}

Where {member_hash} is md5 hash of member's email address.

You don't have to provide all merge fields. Mailchimp will update only those you've specified.

Upvotes: 2

TooMuchPete
TooMuchPete

Reputation: 4643

In APIv2, you still use lists/subscribe, but you pass update_existing as well. This is considerably more intuitive in APIv3, which you should use if you can, since 2.0 is officially deprecated.

Upvotes: 1

Related Questions