Sending ADDRESS in merge field - Doesn't make it through to Mailchimp
Closed this issue · 7 comments
I've been trying to figure out how to send ADDRESS in my merge fields (everything else is fine), but I can't seem to get the format right. Anyone else run into this?
Whether I pass a string:
ADDRESS: "123 Some St, Toronto, ON, CA"
or I try to pass parts (some googling pointed me to this):
ADDRESS: { addr1: "123 Some St", city: "Toronto", state: "ON", country: "CA" }
It doesn't save. When I made it a required field on the Mailchimp side it throws an error saying "Please enter a complete address" so obviously I'm just not nailing the format.
Wasn't able to find any Gibbon specific documentation on passing nested merge field values like this.
Hi @rapind. Can you share the line or two of Ruby you're using to try to set an address merge field?
I'd expect the second format to work assuming you're nesting the field properly when your making your request.
Gibbon maps Ruby syntax onto the MC API directly so as you say it's a matter of figuring out what the API expects and matching it.
@amro sure thing, but I don't think it tells you any thing more. The problem is the ADDRESS tag, everything else makes it through to Mailchimp just fine.:
gibbon.lists(mailchimp_list_id).members(lower_case_md5_hashed_email_address).upsert(
body: {
email_address: user.email,
status: "subscribed",
merge_fields: {
FNAME: user.first_name,
LNAME: user.last_name,
EMAIL: user.email,
PHONE: user.phone,
ADDRESS: user.address
}
}
)
In the above I've tried a user.address string value of: 123 Some St, Toronto, ON, CA
, and a user.address hash value of { addr1: "123 Some St", city: "Toronto", state: "ON", country: "CA" }
. Neither of which makes it through. I've also tried a json version of the hash: E.g. { addr1: "123 Some St", city: "Toronto", state: "ON", country: "CA" }.to_json
I looked into this and at first I thought there was a bug in MailChimp's API (spoiler: there's not).
I first I tried your snippet and ran into the same problem. I intentionally fudged the type of one of the fields (zip code) in my case to make it numeric and the API returned a validation error (because the zip is supposed to be a string). At that point, I wondered if this worked at all since, as I mentioned, Gibbon simply takes Ruby syntax and maps it onto HTTP requests. It's a really simple transformation. So...I tried Curl:
curl --request POST \
--url 'https://us1.api.mailchimp.com/3.0/lists/<list_id>/members' \
--user 'anystring:<api_key>' \
--data '{"email_address":"someemail@gmail.com", "status":"subscribed", "merge_fields": {"FNAME":"Bob", "MYADDY": {"addr1":"1234 Someplace Way", "addr2":"", "city":"Atlanta", "state":"GA", "zip":"30318", "country":"US"}}}' \
--include
And I ran into the same problem! At this point I noticed the response included the user's merge fields, which referred to the address field I added by the MailChimp-defined field name (MMERGE17
in my case) so I tried that instead and it worked:
curl --request POST \
--url 'https://us1.api.mailchimp.com/3.0/lists/<list_id>/members' \
--user 'anystring:<api_key>' \
--data '{"email_address":"someemail@gmail.com", "status":"subscribed", "merge_fields": {"FNAME":"Bob", "MMERGE17": {"addr1":"1234 Someplace Way", "addr2":"", "city":"Atlanta", "state":"GA", "zip":"30318", "country":"US"}}}' \
--include
I then tried the same with Gibbon and it worked as well.
gibbon.lists(<list_id>).members.create(body: {
email_address: "someemail@gmail.com",
status: "subscribed",
merge_fields: {
MMERGE17: {
addr1: "1234 Someplace Way",
city: "Atlanta",
state: "GA",
zip: "30318"
}
}
})
Hmm, I thought. So I looked back at how I'd defined the new address variable on my list:
Ah! The issue in my case was there's a display name, which I'd made look like a merge field, but I haven't updated the API-visible merge field name to the right.
Once I did so it all worked fine:
So what's odd, I think, is the API seems to validate data types for merge fields that "don't exist", but
ignores the data. I guess they do this to increase success rate of API requests when people make mistakes (e.g. you get the email address, at least).
OK so it looks like zip
is required. Including zip
fixes it for me. Wish they documented that, or that the error returned from their API was more specific. I think we can close this. Thanks for your help @amro !
OK so it looks like
zip
is required. Includingzip
fixes it for me. Wish they documented that, or that the error returned from their API was more specific. I think we can close this. Thanks for your help @amro !
Thanks @amro and @rapind... after banging my head against a brick wall for sometime and then finding this, supplying a zip
attribute was all I needed to get my address imports working.
My ADDRESS
merge field data looks like this:
{
addr1: address.address_line1,
addr2: address.address_line2,
city: address.city,
country: address.country, # Needs to be 2 characters i.e GB, US
state: address.county,
zip: address.postcode # All important!
}
Kind of annoying that this still isn't documented by Mailchimp... at least as far as I could find anyway.