Trying your function, it seems to solve a lot of time with API documentation. Anyway I read it and it made it work... but no update occurs on Mailchimp list.
Execution console from Google shots "Complete" (so success when executing the functions).
I had to move from onEdit to its own function name and trigger it with onEdit trigger.
And at Mailchimp latest API calls messages are always:
Result: Ok
Throtteld: --
Response: n/a
I don't know if it's because field format, but I tried just updating one single value and it doesn't get updated in mailchimp list.
Any clue about what could be wrong? Script is like this now:
function sendToMailChimp(id_customer,id_shop,id_gender,fn,ln,em,optin,date_add,last_order,num_pedidos,aov,good_experience){
var API_KEY = '3e61f4b01ca32c8258117d22b971fb82-us10';
var mc_base_url = 'https://us10.api.mailchimp.com/3.0';
var mc_list_id = '3ad825a38e';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('');
//Header of PUT request
var headers = {
'Authorization': 'Basic ' + Utilities.base64Encode('username' + ':' + API_KEY, Utilities.Charset.UTF_8),
};
//MailChimp API requires MD5 hash of email to identify a subscriber
function md5(str) {
return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, str).reduce(function(str,chr){
chr = (chr < 0 ? chr + 256 : chr).toString(16);
return str + (chr.length==1?'0':'') + chr;
},'');
}
var emHash = md5(em.toString().toLowerCase());
//Body of PUT request
var payload = JSON.stringify({
"status_if_new": "subscribed",
"email_address": em,
"merge_fields": {
"id_customer": id_customer,
"id_shop": id_shop,
"id_gender": id_gender,
"optin": optin
}
});
//put the above variables together into 'options'
var options = {
"headers" : headers,
"payload": payload,
"method" : "put",
"muteHttpExceptions" : true
};
//PUT request to specific subscriber in Mailchimp
var response = UrlFetchApp.fetch(mc_base_url + '/lists/' + mc_list_id + '/members/' + emHash,options);
if( response.getResponseCode() == 200 ) {
console.info( "SUCCESS to PROD for " + em + payload);
} else {
console.log(response);
}
}
/**
- onEdit Trigger Function
*/
function actualiza_usuario(e) {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = e.range;
//gets Index value of row which contains edited cell (e.g. cell A60 was edited, rowedited = 60)
var rowedited = range.getRowIndex();
//if edited sheet is not "Points", die. else continue
if (activeSheet.getName() !== "Mailchimp"){
return;
Logger.log("Oops :(");
}
else {
//capture value of first name, last name, email, and points specifically of the row that was edited
var values = activeSheet.getRange(rowedited, 1, 1, 12).getValues()[0];
//separate values variable
var id_customer = values[0];
var id_shop = values[1];
var id_gender = values[2];
var fn = values[3];
var ln = values[4];
var em = values[5];
var optin = values[6];
var date_add = values[7];
var last_order = values[8];
var num_pedidos = values[9];
var aov = values[10];
var good_experience = values[11];
sendToMailChimp(id_customer,id_shop,id_gender,fn,ln,em,optin,date_add,last_order,num_pedidos,aov,good_experience);
Logger.log("Enviado");
}
}