Hi,
I have a hook setup and it works fine in testing mode and also works for some signups but not for all. Many of my new users is not send to my Mailchimp API where I used to add them to a newsletter.
It is very difficult to debug as I cannot see my logs more than 2 days back.
My last user fired the hook on the 1st of September, but there are others after that date. I suspect that the users with social signups are the ones not triggering the hook, but just a guess.
var request = require('request');
module.exports = function (user, context, cb) {
// Perform any asynchronous actions, e.g. send notification to Slack.
var apiKey = "<MY_API_KEY>";
var urlMailchimp = "https://us11.api.mailchimp.com/3.0/lists/<LIST_ID>/members";
var userMailchimp = "<MYEMAIL>";
request({
method: 'POST',
url: urlMailchimp,
headers: {
'Content-Type': 'application/json',
'authorization': "MY_API_KEY"
},
body: `{
"email_address": "${user.email}",
"status": "subscribed",
"tags": ["TowerUp user"],
"merge_fields": {
"MMERGE3": "${user.family_name}",
"MMERGE1": "${user.given_name}"
}
}`,
}, function(error, response, body) {
console.log(user)
console.log(error);
cb(error, response)
});
cb();
};
At the Post-User Registration extensibility point, Hooks allow custom actions to be executed after a new user registers an account and is added to the database.
The Post User Registration Hook will only run after a user signs up or is manually created for a database connection. This means that it will not run after a social connection sign up.
However, to catch all connection types, you can use a Rule instead.
Rules run after any successful authentication and can determine if it is the first time a user has authenticated.
For the rule, you can add this code at the top of the function so that it will only run for the user’s first authentication:
var count = context.stats && context.stats.loginsCount ? context.stats.loginsCount : 0;
if (count > 1) {
return callback(null, user, context);
}
Here is an example of using this approach to add a role upon the first authentication in a Rule:
I believe your cb() at the end could be called before the async response from the api has returned.
It would explain the: sometimes it works, sometimes it doesn’t part.
You could try the async/await structure.
Edit: This doesn’t apply if we’re talking about the Post-Registration hook of course. Wasn’t clear in the question