Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding lifecycle event to add existing users to Mailchimp audience #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 0.4.1

- Add lifecycle event to add existing users to a Mailchimp audience.

## Version 0.4.0

- Runtime and dependencies bump (#45)
Expand Down
34 changes: 33 additions & 1 deletion extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

name: mailchimp-firebase-sync
version: 0.4.0
version: 0.4.1
specVersion: v1beta

displayName: Manage Marketing with Mailchimp
Expand Down Expand Up @@ -114,6 +114,15 @@ resources:
eventType: providers/cloud.firestore/eventTypes/document.write
resource: projects/${param:PROJECT_ID}/databases/(default)/documents/${param:MAILCHIMP_MEMBER_EVENTS_WATCH_PATH}/{documentId}

- name: addExistingUsersToList
type: firebaseextensions.v1beta.function
description:
Adds existing users into the specified Mailchimp audience.
properties:
location: ${param:LOCATION}
runtime: nodejs16
taskQueueTrigger: {}

params:
- param: LOCATION
label: Cloud Functions location
Expand Down Expand Up @@ -389,3 +398,26 @@ params:

required: true
default: '{}'

- param: DO_BACKFILL
label: Import existing users into Mailchimp audience
description: >-
Do you want to add existing users to the Mailchimp audience when you install or update this extension?
type: select
required: true
options:
- label: Yes
value: true
- label: No
value: false

lifecycleEvents:
onInstall:
function: addExistingUsersToList
processingMessage: "Adding existing users to audience ${param:MAILCHIMP_AUDIENCE_ID}"
onUpdate:
function: addExistingUsersToList
processingMessage: "Adding existing users to audience ${param:MAILCHIMP_AUDIENCE_ID}"
onConfigure:
function: addExistingUsersToList
processingMessage: "Adding existing users to audience ${param:MAILCHIMP_AUDIENCE_ID}"
3 changes: 2 additions & 1 deletion functions/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
mailchimpMergeFieldWatchPath: process.env.MAILCHIMP_MERGE_FIELDS_WATCH_PATH,
mailchimpMergeField: process.env.MAILCHIMP_MERGE_FIELDS_CONFIG,
mailchimpMemberEventsWatchPath: process.env.MAILCHIMP_MEMBER_EVENTS_WATCH_PATH,
mailchimpMemberEvents: process.env.MAILCHIMP_MEMBER_EVENTS_CONFIG
mailchimpMemberEvents: process.env.MAILCHIMP_MEMBER_EVENTS_CONFIG,
doBackfill: process.env.DO_BACKFILL === "true",
};
97 changes: 95 additions & 2 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const crypto = require('crypto');
const _ = require('lodash');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Mailchimp = require('mailchimp-api-v3');

const { initializeApp } = require('firebase-admin/app')
const { getAuth } = require("firebase-admin/auth");
const { getExtensions } = require('firebase-admin/extensions');
const { getFunctions } = require('firebase-admin/functions');

let config = require('./config');
const logs = require('./logs');

Expand All @@ -13,7 +17,7 @@ const CONFIG_PARAM_NAMES = Object.freeze([
'mailchimpMemberEvents'
]);

admin.initializeApp();
initializeApp();
logs.init();

const processConfig = (configInput) => {
Expand Down Expand Up @@ -310,4 +314,93 @@ exports.memberEventsHandler = functions.handler.firestore.document
}
});

exports.addExistingUsersToList = functions.tasks
.taskQueue()
.onDispatch(async (data) => {
const runtime = getExtensions().runtime();
if (!config.doBackfill) {
await runtime.setProcessingState(
"PROCESSING_COMPLETE",
"No processing requested."
);
return;
}
const nextPageToken = data.nextPageToken;
const pastSuccessCount = parseInt(data["successCount"]) ?? 0;
const pastErrorCount = parseInt(data["errorCount"]) ?? 0;

const res = await getAuth().listUsers(100, nextPageToken);
const mailchimpPromises = res.users.map(async (user) => {
const { email, uid } = user;
if (!email) {
logs.userNoEmail();
return;
}

try {
logs.userAdding(uid, config.mailchimpAudienceId);
const results = await mailchimp.post(
`/lists/${config.mailchimpAudienceId}/members`,
{
email_address: email,
status: config.mailchimpContactStatus,
}
);
logs.userAdded(
uid,
config.mailchimpAudienceId,
results.id,
config.mailchimpContactStatus
);
} catch (err) {
if (err.title === 'Member Exists' ) {
logs.userAlreadyInAudience(uid, config.mailchimpAudienceId);
// Don't throw error if the memeber already exists.
} else {
logs.errorAddUser(err);
// For other errors, rethrow them so that we can report the total number at the end of the lifecycle event.
throw err;
}
}
});
const results = await Promise.allSettled(mailchimpPromises);
const newSucessCount =
pastSuccessCount + results.filter((p) => p.status === "fulfilled").length;
const newErrorCount =
pastErrorCount + results.filter((p) => p.status === "rejected").length;
if (res.pageToken) {
// If there is a pageToken, we have more users to add, so we enqueue another task.
const queue = getFunctions().taskQueue(
"addExistingUsersToList",
process.env.EXT_INSTANCE_ID
);
await queue.enqueue({
nextPageToken: res.pageToken,
successCount: newSucessCount,
errorCount: newErrorCount,
});
} else {
// The backfill is complete, now we need to set the processing state.
logs.backfillComplete(newSucessCount, newErrorCount);
if (newErrorCount === 0) {
await runtime.setProcessingState(
"PROCESSING_COMPLETE",
`${newSucessCount} users added (or already existed) in Mailchimp audience ${config.mailchimpAudienceId}.`
);
} else if (newErrorCount > 0 && newSucessCount > 0) {
await runtime.setProcessingState(
"PROCESSING_WARNING",
`${newSucessCount} users added (or already existed) in Mailchimp audience ${config.mailchimpAudienceId}, ` +
`failed to add ${newErrorCount} users. Check function logs for more information.`
);
}
if (newErrorCount > 0 && newSucessCount === 0) {
await runtime.setProcessingState(
"PROCESSING_FAILED",
`Failed to add ${newErrorCount} users to ${config.mailchimpAudienceId}. Check function logs for more information.`
);
}
}
});

exports.processConfig = processConfig
5 changes: 5 additions & 0 deletions functions/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ module.exports = {
`Removing user: ${userId} with hashed email: ${hashedEmail} from Mailchimp audience: ${audienceId}`
);
},
backfillComplete: (successCount, errorCount) => {
logger.log(
`Finished adding existing users to Mailchimp audience. ${successCount} users added, ${errorCount} errors.`
);
},
};
Loading