Skip to content

Send a “sign-up” event to Loops #1129

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

Merged
merged 1 commit into from
May 24, 2024
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
2 changes: 2 additions & 0 deletions apps/webapp/app/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ const EnvironmentSchema = z.object({
ALERT_RESEND_API_KEY: z.string().optional(),

MAX_SEQUENTIAL_INDEX_FAILURE_COUNT: z.coerce.number().default(96),

LOOPS_API_KEY: z.string().optional(),
});

export type Environment = z.infer<typeof EnvironmentSchema>;
Expand Down
75 changes: 75 additions & 0 deletions apps/webapp/app/services/loops.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { env } from "~/env.server";
import { logger } from "./logger.server";

class LoopsClient {
constructor(private readonly apiKey: string) {}

async userCreated({
userId,
email,
name,
}: {
userId: string;
email: string;
name: string | null;
}) {
logger.info(`Loops send "sign-up" event`, { userId, email, name });
return this.#sendEvent({
email,
userId,
firstName: name?.split(" ").at(0),
eventName: "sign-up",
});
}

async #sendEvent({
email,
userId,
firstName,
eventName,
eventProperties,
}: {
email: string;
userId: string;
firstName?: string;
eventName: string;
eventProperties?: Record<string, string | number | boolean>;
}) {
const options = {
method: "POST",
headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json" },
body: JSON.stringify({
email,
userId,
firstName,
eventName,
eventProperties,
}),
};

try {
const response = await fetch("https://app.loops.so/api/v1/events/send", options);

if (!response.ok) {
logger.error(`Loops sendEvent ${eventName} bad status`, { status: response.status });
return false;
}

const responseBody = (await response.json()) as any;

if (!responseBody.success) {
logger.error(`Loops sendEvent ${eventName} failed response`, {
message: responseBody.message,
});
return false;
}

return true;
} catch (error) {
logger.error(`Loops sendEvent ${eventName} failed`, { error });
return false;
}
}
}

export const loopsClient = env.LOOPS_API_KEY ? new LoopsClient(env.LOOPS_API_KEY) : null;
32 changes: 20 additions & 12 deletions apps/webapp/app/services/telemetry.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Organization } from "~/models/organization.server";
import type { Project } from "~/models/project.server";
import type { User } from "~/models/user.server";
import { singleton } from "~/utils/singleton";
import { loopsClient } from "./loops.server";

type Options = {
postHogApiKey?: string;
Expand Down Expand Up @@ -39,18 +40,19 @@ class Telemetry {

user = {
identify: ({ user, isNewUser }: { user: User; isNewUser: boolean }) => {
if (this.#posthogClient === undefined) return;
this.#posthogClient.identify({
distinctId: user.id,
properties: {
email: user.email,
name: user.name,
authenticationMethod: user.authenticationMethod,
admin: user.admin,
createdAt: user.createdAt,
isNewUser,
},
});
if (this.#posthogClient) {
this.#posthogClient.identify({
distinctId: user.id,
properties: {
email: user.email,
name: user.name,
authenticationMethod: user.authenticationMethod,
admin: user.admin,
createdAt: user.createdAt,
isNewUser,
},
});
}
if (isNewUser) {
this.#capture({
userId: user.id,
Expand All @@ -64,6 +66,12 @@ class Telemetry {
},
});

loopsClient?.userCreated({
userId: user.id,
email: user.email,
name: user.name,
});

this.#triggerClient?.sendEvent({
name: "user.created",
payload: {
Expand Down
Loading