Skip to content
Merged
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
44 changes: 30 additions & 14 deletions apps/web/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,36 @@ async function handleSignIn({
isNewUser: boolean;
}) {
if (isNewUser && user.email) {
const loops = createLoopsContact(
user.email,
user.name?.split(" ")?.[0],
).catch((error) => {
const alreadyExists =
error instanceof Error && error.message.includes("409");
if (!alreadyExists) {
logger.error("Error creating Loops contact", {
email: user.email,
error,
const loops = async () => {
const account = await prisma.account
.findFirst({
where: { userId: user.id },
select: { provider: true },
})
.catch((error) => {
logger.error("Error finding account", {
userId: user.id,
error,
});
captureException(error, undefined, user.email);
});
captureException(error, undefined, user.email);
}
});

await createLoopsContact(
user.email,
user.name?.split(" ")?.[0],
account?.provider,
).catch((error) => {
const alreadyExists =
error instanceof Error && error.message.includes("409");
if (!alreadyExists) {
logger.error("Error creating Loops contact", {
email: user.email,
error,
});
captureException(error, undefined, user.email);
}
});
};

const resend = createResendContact({ email: user.email }).catch((error) => {
logger.error("Error creating Resend contact", {
Expand All @@ -177,7 +193,7 @@ async function handleSignIn({
captureException(error, undefined, user.email);
});

await Promise.all([loops, resend, dub]);
await Promise.all([loops(), resend, dub]);
}

if (isNewUser && user.email && user.id) {
Expand Down
35 changes: 12 additions & 23 deletions packages/loops/src/loops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,26 @@ function getLoopsClient(): LoopsClient | undefined {
export async function createContact(
email: string,
firstName?: string,
provider?: string,
): Promise<{
success: boolean;
id?: string;
}> {
const loops = getLoopsClient();
if (!loops) return { success: false };
// so we can run a/b tests with 2-6 groups easily
const abTestId = getRandomInt(60);
const resp = await loops.createContact({
email,
properties: firstName ? { firstName, abTestId } : { abTestId },
});
return resp;
const properties: Record<string, string | number> = {};
if (firstName) properties.firstName = firstName;
if (provider) properties.provider = provider;

return await loops.createContact({ email, properties });
}

export async function deleteContact(
email: string,
): Promise<{ success: boolean }> {
const loops = getLoopsClient();
if (!loops) return { success: false };
const resp = await loops.deleteContact({ email });
return resp;
return await loops.deleteContact({ email });
}

export async function startedTrial(
Expand All @@ -46,13 +44,12 @@ export async function startedTrial(
): Promise<{ success: boolean }> {
const loops = getLoopsClient();
if (!loops) return { success: false };
const resp = await loops.sendEvent({
return await loops.sendEvent({
eventName: "upgraded",
email,
contactProperties: { tier },
eventProperties: { tier },
});
return resp;
}

export async function completedTrial(
Expand All @@ -61,13 +58,12 @@ export async function completedTrial(
): Promise<{ success: boolean }> {
const loops = getLoopsClient();
if (!loops) return { success: false };
const resp = await loops.sendEvent({
return await loops.sendEvent({
eventName: "completed_trial",
email,
contactProperties: { tier },
eventProperties: { tier },
});
return resp;
}

export async function switchedPremiumPlan(
Expand All @@ -76,26 +72,24 @@ export async function switchedPremiumPlan(
): Promise<{ success: boolean }> {
const loops = getLoopsClient();
if (!loops) return { success: false };
const resp = await loops.sendEvent({
return await loops.sendEvent({
eventName: "switched_premium_plan",
email,
contactProperties: { tier },
eventProperties: { tier },
});
return resp;
}

export async function cancelledPremium(
email: string,
): Promise<{ success: boolean }> {
const loops = getLoopsClient();
if (!loops) return { success: false };
const resp = await loops.sendEvent({
return await loops.sendEvent({
eventName: "cancelled",
email,
contactProperties: { tier: "" },
});
return resp;
}

async function updateContactProperty(
Expand All @@ -105,11 +99,10 @@ async function updateContactProperty(
const loops = getLoopsClient();
if (!loops) return { success: false };

const resp = await loops.updateContact({
return await loops.updateContact({
email,
properties,
});
return resp;
}

export async function updateContactRole({
Expand All @@ -131,7 +124,3 @@ export async function updateContactCompanySize({
}) {
return updateContactProperty(email, { companySize });
}

function getRandomInt(max: number) {
return Math.ceil(Math.random() * max);
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.17.5
v2.17.6
Loading