I'm trying to use a database hook to send information to a MailChimp list.
The problem is that my code stops continuing at a certain point (processing the httpResponse)
export function sendToMailChimp(data) {
return fetch("https://us4.api.mailchimp.com/3.0/lists/" + LIST_ID + "/members", {
method: 'post',
headers: {
'Authentication': 'webForm:' + API_KEY,
'content-type': 'application/json'
},
body: data
}).then((httpResponse) => {console.log("Status: " + httpResponse.statusText);});
}
The console does not show the "Status: " message.
If I run the following, "Handle response" is printed but still not the "Status: " message:
export function sendToMailChimp(data) {
return fetch("https://us4.api.mailchimp.com/3.0/lists/" + LIST_ID + "/members", {
method: 'post',
headers: {
'anystring': '' + API_KEY,
'content-type': 'application/json'
},
body: data
}).then("Handle response").then((httpResponse) => {console.log("Status: " + httpResponse.statusText);});
}
In addition to this, the data is presumably rejected by MailChimp as it isn't there when I look for it.
It may be an authentication issue but there's no documentation online on setting up mailchimp and I cannot seem to find a way of doing basic authentication for my API_KEY other than what I have written, so I am in the dark.
Is anyone able to provide some insight as to what may be going wrong?
Thank you.
Now I am able to send contact info to the mailchimp from wix userform.
But, what I want to do is, if the user resubmits the input with differnt info, the mailchip contact is not updated! Postman says that it cannot add the info with same email address.
Is there any way to overwrite the info if email address is already existing in mailchimp?
Hi, I got it fixed. customerobject.field2 was returning empty string. When I updated with some default string, it worked.
The articles in this forum have been very useful. specially postman and tobys code.
Thanks,
Hello folks,
As Toby provided a code in the begining of this forum, I added backend code and the page code.
Mine is a custom form I created myself.
Once the submit button is pressed, user input is logged into my database called database5.
I also have couple of merge fields that I added.
I made few changes from the initial code to suite my needs.
But, this code does n't send any info to my mailchimp account. Can someone peer review my code here please.
export function database5_afterInsert() { var data = { "email_address":$w("#input2").value, "merge_fields": { "FNAME":$w("#input1").value, //"LNAME":item.lName "PHONE":$w("#input3").value, "MMERGE6": customerObject.field1, //custom merge field "MMERGE7": customerObject.field2, //custom merge field }, "status":"subscribed" }; data = JSON.stringify(data, '\t'); sendToMailChimp(data); }
Thank you so much for this Toby! Exactly what I needed! Also thanks for the tip with Postman - I needed it to sort out why my API requests weren't working (this article also came super handy: https://medium.com/@dddotcom/intro-to-mailchimp-api-3-0-adding-members-to-a-mailing-list-and-testing-with-postman-f056d900e0d9 )
One thing maybe that can help others - if you have a boolean in your wix database make sure to have a workaround for changing that to a string (maybe other type works too) before making the api call. - I guess just like Toby did with subscribed.
Works like a charm Toby, Thank you
Oh, I re-read this... you're creating a custom form.
Hey Toby. Question.. I get the point of the code, but where are you collecting the information from to go into Mailchimp? Are you using Wix Subscribe?
In the example above I check the Wix database to see if the email address is already subscribed. If they are then I do not submit to MailChimp again. In my example, the only way for someone to subscribe to the MailChimp list is through the website, so I do not need to check the MailChimp list to see if the email address is already subscribed.
That being said, you should be able to send a GET request to MailChimp, the response should tell you if they're already subscribed.
Do you know how to check if someone is already subscribed on the mailchimp list?
I am not familiar with SendGrid, but you should be able to create an templated automated email within their system. At the very least theyshould allow you to code some HTML and CSS to make it look nice.
Good luck!
Well I have done that tutorial too, but sadly the email is only in text. I'm currently trying to understand how to sent a template / newsletter email on a form submission (not just plain text), but well pardon my dumbness in understanding SendGrid documentation #killmyself
That is correct, this is just for the API integration, email is handled seperatley.
I believe you can set up welcome emails via MailChimp. Alternatively you can use this tutorial to use SendGrid to send an automated email on form submission https://support.wix.com/en/article/how-to-send-an-email-on-form-submission
This code is only for collecting the subscribers via MailChimp right. Will they get an autoresponder email after the subscription? Or do you happen to use the code in combination with sending an email on subscription?
Hi Andluism, you will need to modify the code to suit your needs depending on whether you are connecting to a subscriber form or a contact form with an opt-in to become a subscriber. The minimum you would need to change is the API key and List ID.
Hey Toby, can I know which part of the code that need to be replaced by my own elements? Thanks!
Glad to hear you figured it out.
I have an opt-in check box in my form so if it is selected the user is sent to MailChimp.
I got it to work. I just had a return statement in a wrong spot smh. I am wondering, what was context in ContactForm_afterInsert(item, context) used for?
Could you confirm that when you submit the form it submits to your database?
Could you also please post the code as it is in Wix?
I've made a form and I can connect them to databases and stuff. I did the code above and replaced it with the info I need but nothing happens.
Hi Patrick, I explained the process and pasted the necessary code above.
Please first follow the instructions in this link to create the Wix Code form https://www.wix.com/code/home/tutorials-custom-form.
Once you have created the form, you can use the code above to integrate witht the MailChimp API.
If you are still having trouble then please post where you are getting stuck and we can try to help.