如何使用Ruby和Gibbon更新MailChimp API批量订阅中的所有字段

时间:2013-11-11 17:31:40

标签: ruby mailchimp gibbon

我使用的是没有Rails的Ruby 1.9.3和Gibbon gem的1.0.4版本。

我的推文填充了我的列表,可以使用Gibbon将以下内容发送给MailChimp。但是,只有电子邮件地址和电子邮件类型字段填充在MailChimp的列表中。我做错了什么,禁止通过API导入所有合并字段?

这是列表的批处理和地图。

referrals.each_slice(3) do |batch|
  begin
    prepared_batch = batch.map do |referral|
      { 
        :EMAIL => {:email => referral['client_email']}, 
        :EMAIL_TYPE => 'html', 
        :MMERGE6 => referral['field_1'],
        :MMERGE7 => referral['field_2'],
        :MMERGE8 => referral['field_3'],
        :MMERGE9 => referral['field_4'],
        :MMERGE11 => referral['field_5'],
        :MMERGE12 => referral['field_6'],
        :MMERGE13 => referral['field_7'],
        :MMERGE14 => referral['field_8'],
        :MMERGE15 => referral['field_9'],
        :FNAME => referral['client_first_name']
      }
    end
    @log.info("prepared_batch : #{prepared_batch}")

result = @gibbon.lists.batch_subscribe(
  :id => @mc_list_id, 
  :batch => prepared_batch,
  :double_optin => false,
  :update_existing => true
)
@log.info("#{result}")

  rescue Exception => e
    @log.warn("Unable to load batch into mailchimp because #{e.message}")
  end
end

以上执行成功。但是,仅填充电子邮件地址和电子邮件类型,但应填充大多数字段。

这是其中一个prepared_batches的日志输出。我用Value替换了真值。我用自己的电子邮件进行测试。

I, [2013-11-11T09:01:14.778907 #70827]  INFO -- : prepared_batch : [{:EMAIL=>
{:email=>"jason+6@marketingscience.co"}, :EMAIL_TYPE=>"html", :MMERGE6=>"Value",
 :MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/6/13 0:00",
 :MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>"10/18/13 19:09", :MMERGE15=>"Value",
 :FNAME=>"Value"}, {:EMAIL=>{:email=>"jason+7@marketingscience.co"}, :EMAIL_TYPE=>"html",
 :MMERGE6=>"Value", :MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/6/13
 0:00", :MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>nil, :MMERGE15=>"Value",
 :FNAME=>"Value"}, {:EMAIL=>{:email=>"jason+8@marketingscience.co"}, :EMAIL_TYPE=>"html",
 :MMERGE6=>"Value", :MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/7/13
 0:00", :MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>nil, :MMERGE15=>"Value",
 :FNAME=>"Value"}]

以下是MailChimp调用结果的日志输出。

I, [2013-11-11T09:01:14.778691 #70827]  INFO -- : {"add_count"=>3, "adds"=>
[{"email"=>"jason+3@marketingscience.co", "euid"=>"ab512177b4", "leid"=>"54637465"},
 {"email"=>"jason+4@marketingscience.co", "euid"=>"eeb8388524", "leid"=>"54637469"},
 {"email"=>"jason+5@marketingscience.co", "euid"=>"7dbc84cb75", "leid"=>"54637473"}],
 "update_count"=>0, "updates"=>[], "error_count"=>0, "errors"=>[]} 

有关如何在MailChimp中更新所有字段的任何建议,我们表示赞赏。感谢。

1 个答案:

答案 0 :(得分:1)

原来使用Gibbon gem批量订阅的文档不正确。您需要添加:merge_vars结构以包含电子邮件和电子邮件类型以外的字段。我的最终代码如下所示。我还将完整更新此代码:https://gist.github.com/analyticsPierce/7434085

referrals.each_slice(3) do |batch|
  begin
    prepared_batch = batch.map do |referral|
      { 
        :EMAIL => {:email => referral['email']}, 
        :EMAIL_TYPE => 'html', 
        :merge_vars => {
        :MMERGE6 => referral['field_1'],
        :MMERGE7 => referral['field_2'],
        :MMERGE8 => referral['field_3'],
        :MMERGE9 => referral['field_4'],
        :MMERGE11 => referral['field_5'],
        :MMERGE12 => referral['field_6'],
        :MMERGE13 => referral['field_7'],
        :MMERGE14 => referral['field_8'],
        :MMERGE15 => referral['field_9'],
        :FNAME => referral['first_name']
        } 
      }
    end
    @log.info("prepared_batch : #{prepared_batch}")
    result = @gibbon.lists.batch_subscribe(
      :id => @mc_list_id, 
      :batch => prepared_batch,
      :double_optin => false,
      :update_existing => true
    )
    @log.info("#{result}")
  rescue Exception => e
    @log.warn("Unable to load batch into mailchimp because #{e.message}")
  end
end