Skip to content

Commit

Permalink
Add Loops Transparency (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-gordon authored Dec 16, 2023
1 parent 7ad575b commit aa1abc9
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions app/src/lib/sendLoopsEvent.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
// keep a simple cache that prevents the same loops event from being sent twice
// in the same session
const sentEvents = new Set<string>();

export function sendLoopsEvent(body: { email: string; eventName: string }) {
if (process.env.REACT_APP_VERCEL_ENV !== "production") return;
if (process.env.REACT_APP_VERCEL_ENV !== "production") {
console.log("Not sending loops event in dev mode");
return;
}

const key = `${body.email}:${body.eventName}`;
if (sentEvents.has(key)) {
console.log(
`Loops event: ${body.eventName} for ${body.email} already sent`
);
return;
}
sentEvents.add(key);

console.log(`Loops event: ${body.eventName} for ${body.email} sent`);

fetch("/api/loops-event", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
})
.then((res) => {
if (!res.ok) {
console.error("Failed to send loops event", res);
} else {
console.log("Sent loops event", body);
}
})
.catch((err) => {
console.error("Failed to send loops event", err);
});
}

export function loopsNewSubscriber(email: string) {
Expand Down

0 comments on commit aa1abc9

Please sign in to comment.