MailChimp interest groups in update profile forms

November 13, 2014

programming

I’ve recently been doing some customization of MailChimp forms – essentially just wrapping the data in our own HTML so we can style it. While doing this, specifically with the Update Profile form that subscribers can click in the footer of emails, I ran into issues where the checkboxes were not correctly checked, leaving users with no way of understanding which newsletters they might already be subscribed to.

MailChimp has some documentation on this subject at this link. The relevant section reads like this:

(NOTE: “X” is the grouping ID, which can be found when you “revert” your forms, or you can generate an embed form for your list to get the ID’s for each grouping.)

For a checkbox it’d look like this: <input type=checkbox name="group[X]" value="1" |INTERESTCHECKED:X:1|> Cars

So the idea is that, in order to show the proper status, you need to know the correct numbers for X:1, which depend on the display order in MailChimp.

In our case, we had several interests. Some of them were legacy, and we didn’t want to get rid of them but we did want to keep them out of the display. We generated an embed form to get the IDs, but they didn’t work.

Finally I decided to use the MailChimp API to see if I might get any different information. The process there worked like this:

  1. Generate an API key.
  2. Make a request to /lists/list.json (with API key) to find the ID of the mailing list I was working with.
  3. Make a request to /lists/interest-groupings.json (with API key and list ID) to see the IDs of the groups I was dealing with.

In this data, I saw that each group had structure like this:

"id" : 3,
"display_order" : "0",
"name" : "[name]",
"form_field" : "checkboxes"

Then for each interest within the group, there was a structure like this:

"id" : 1,
"subscribers" : null,
"display_order" : "6",
"bit" : "32",
"name" : "[name]"

Sometimes display_order and bit had the same value, but other times – presumably if we had reordered them – they didn’t.

The method that worked, after a few attempts, looks like this:

<input type="checkbox" value="[bit]" *|INTERESTCHECKED:[groupid]:[display_order]|* name="group[groupid][bit]">

The default from an embed form generated in MailChimp is like this:

<input type="checkbox" value="[bit]" name="group[groupid][bit]" id="mce-group[5]-5-0"><label for="mce-group[5]-5-0">

I didn’t change the ID, but I’m not really sure how MailChimp generates that. In any case, this is the formula that seems to work. This will definitely change if we reorder our fields, but I’m not sure if it will change if we were to remove the legacy fields as well.

I wanted to document this, as there’s very little documentation out there about using the interest groups in non-default ways.