7

I'm trying to figure out how I can use the Gibbon gem in Rails to automatically add subscribers to specific interest groups in MailChimp?

I've found this article which details a non-Rails method for doing so: http://roman.tao.at/uncategorized/mailchimp-api-listsubscribe-listbatchsubscribe-and-groups/

I'd like to figure out how to implement that functionality using the Gibbon gem: https://github.com/amro/gibbon

FYI, I'm also a novice with both MailChimp and Rails.

cpezza85
  • 625
  • 1
  • 7
  • 12

2 Answers2

10

Finally, after hours of perusing through code. I've found the example I'm looking for!

Thanks to TJ Moretto for providing this on a Google Groups thread:

I'm using the gibbon gem, but ran into the same types of issues. Here's how I had to format the parameters to finally get this to work:

gb.list_subscribe({:id => "#{list.id}",
                   :email_address => user.email,
                   :update_existing => true,
                   :double_optin => false,
                   :send_welcome => false,
                   :merge_vars => {'FNAME' => "#{user.first_name}",
                                   'LNAME' => "#{user.last_name}",
                                   'GROUPINGS' => {0 => {'id' => grouping.id, 'groups' => "#{challenge.name}"}}}
                  })

Hope that helps.

Mailchimp Team - based on the number of issues that everyone runs into when trying to do this (across all programming languages), I suggest you update the API documentation to be more clear.

Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
cpezza85
  • 625
  • 1
  • 7
  • 12
5

Update for version 2.0 of the MailChimp API and version 1.0 of Gibbon (For @Calin and posterity). Here are the necessary changes from the previous version. The API object is accessed like this now:

gb = Gibbon::API.new

And list methods like so:

gb.lists.subscribe(params)

Finally the :email_address parameter has been replaced by the :email parameter, which should be given a value of the following form: The value should itself be a hash with one key, either 'email' or 'leid', and the value should be either the email address of the subscriber or MC's unique identifier (LEID) for the subscriber.

So a full subscription call might look something like this:

gb = Gibbon::API.new
gb.lists.subscribe(:id => "ed6d1dfef4",
                   :email => 
                     { "email" => "example@domain.com" },
                   :merge_vars =>
                     {:groupings =>
                       {
                         0 => { :id => "95", :groups => ["Some Group", "Another Group"]},
                         1 => { :id => "34", :groups => ["A Third Group"]}
                       }
                     },
                    :update_existing => "true",
                    :double_optin => "false",
                    :replace_interests => "false")
hoffm
  • 2,386
  • 23
  • 36
  • 1
    from their example here http://apidocs.mailchimp.com/api/2.0/lists/subscribe.php this does not look very intuitive, but it seems to do the job, thank you – Calin Sep 04 '13 at 18:11
  • The `groupings` could just be an array instead of an object, e.g.: `groupings: [{id: '95', groups: ['Some Group']}, ...]` – Matt Huggins Apr 18 '14 at 17:40
  • @MattHuggins This doesn't seem to work for me. I'm not passing the replace_interests option though. I checked by passing it, doesn't work anyways. – Ameen Oct 08 '14 at 16:18
  • Just checked the project where this code lives, and it was working on gem version 1.1.2 at the time it was implemented. Not sure if it's changed since then in terms of the MailChimp API or Gibbon itself though since this project was handed off to a client. – Matt Huggins Oct 08 '14 at 21:39