Skip to content

Implement a basic email nurture campaign #491

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

Open
wants to merge 2 commits into
base: main
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
1 change: 0 additions & 1 deletion apps/zipper.dev/src/emails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Head,
Heading,
Html,
Link,
Preview,
Text,
Section,
Expand Down
18 changes: 3 additions & 15 deletions apps/zipper.dev/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { createUserSlug } from '~/utils/create-user-slug';
import { resend } from '~/server/resend';
import crypto from 'crypto';
import { trackEvent } from '~/utils/api-analytics';
import { queues } from '~/server/queue';
import { startNurtureCampaign } from '~/server/utils/nurtureCampaign.utils';

export function PrismaAdapter(p: PrismaClient): Adapter {
return {
Expand Down Expand Up @@ -390,21 +392,7 @@ export const authOptions: AuthOptions = {
});

if (isNewUser && user.email) {
resend.emails.send({
to: user.email,
from: 'Sachin & Ibu <[email protected]>',
reply_to: ['[email protected]', '[email protected]'],
subject: 'Thank you for checking out Zipper',
text: `Hey there,

We just wanted to drop you a quick note to say thank you for checking out Zipper.

If you have any questions, feedback, or general comments, we'd genuinely love to hear them. Feel free to reply to this email at any point - your emails go directly into our inboxes (and not a general mailbox or support queue).

Regards,
Sachin & Ibu
`,
});
startNurtureCampaign(user.email);
}

trackEvent({
Expand Down
24 changes: 22 additions & 2 deletions apps/zipper.dev/src/server/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,35 @@ import { prisma } from './prisma';
import fetch from 'node-fetch';
import getRunUrl from '../utils/get-run-url';
import { generateAccessToken } from '../utils/jwt-utils';
import { sendNurtureEmail } from './utils/nurtureCampaign.utils';

export const redis = new IORedis(+env.REDIS_PORT, env.REDIS_HOST, {
maxRetriesPerRequest: null,
});

const queueWorkersGlobal = global as typeof global & {
workers?: Worker[];
queues?: Record<'schedule', Queue>;
queues?: Record<'schedule' | 'nurture', Queue>;
};

const initializeWorkers = () => {
console.log('[BullMQ] Initializing workers');
return [
new Worker(
'nurture',
async (job) => {
await sendNurtureEmail(job.data.step, job.data.email);
},
{ connection: redis },
)
?.on('completed', (job) => {
console.log(`[Job Queue] Completed nurture job ID ${job?.id}`);
})
?.on('failed', (job, err) => {
console.log(
`[Job Queue] Failed nurture job ID ${job?.id} with error ${err}`,
);
}),
new Worker(
'schedule-queue',
async (job) => {
Expand Down Expand Up @@ -92,10 +108,14 @@ const initializeQueues = () => {
connection: redis,
defaultJobOptions: { removeOnComplete: 1000, removeOnFail: 5000 },
}),
nurture: new Queue('nurture', {
connection: redis,
defaultJobOptions: { removeOnComplete: 1000, removeOnFail: 5000 },
}),
};
};

export const queues: Record<'schedule', Queue> =
export const queues: Record<'schedule' | 'nurture', Queue> =
queueWorkersGlobal.queues || initializeQueues();

export const initializeQueuesAndWorkers = () => {
Expand Down
59 changes: 59 additions & 0 deletions apps/zipper.dev/src/server/utils/nurtureCampaign.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { queues } from '../queue';
import { resend } from '../resend';

export const startNurtureCampaign = (email: string) => {
const hour = 1000 * 60 * 60;

queues.nurture.addBulk([
{
name: 'first-nurture-email',
data: { step: 1, email },
// opts: { delay: hour / 4 },
opts: { delay: hour / 60 },
},
{
name: 'second-nurture-email',
data: { step: 2, email },
// opts: { delay: hour * 24 },
opts: { delay: (hour / 60) * 2 },
},
]);
};

const STEP_ONE_CONTENT = `Hey there,

We just wanted to drop you a quick note to say thank you for checking out Zipper.

If you have any questions, feedback, or general comments, we'd genuinely love to hear them. Feel free to reply to this email at any point - your emails go directly into our inboxes (and not a general mailbox or support queue).

Regards,
Sachin & Ibu
`;

const STEP_TWO_CONTENT = `This is the second email!`;

export const sendNurtureEmail = async (step: 1 | 2, email: string) => {
switch (step) {
case 1:
await resend.emails.send({
to: email,
from: 'Sachin & Ibu <[email protected]>',
reply_to: ['[email protected]', '[email protected]'],
subject: 'Thank you for checking out Zipper',
text: STEP_ONE_CONTENT,
});

break;
case 2:
await resend.emails.send({
to: email,
from: 'Zipper <[email protected]>',
reply_to: ['[email protected]', '[email protected]'],
subject: 'What can you build?',
text: STEP_TWO_CONTENT,
});
break;
default:
throw new Error('invalid nurture step');
}
};