From aa1abc9593a8eee378b8f308a3eb12bdc819c551 Mon Sep 17 00:00:00 2001 From: Rob Gordon Date: Sat, 16 Dec 2023 11:37:58 -0500 Subject: [PATCH] Add Loops Transparency (#631) --- app/src/lib/sendLoopsEvent.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/app/src/lib/sendLoopsEvent.ts b/app/src/lib/sendLoopsEvent.ts index 6b6d330f9..f56c6a43b 100644 --- a/app/src/lib/sendLoopsEvent.ts +++ b/app/src/lib/sendLoopsEvent.ts @@ -1,5 +1,23 @@ +// keep a simple cache that prevents the same loops event from being sent twice +// in the same session +const sentEvents = new Set(); + 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", @@ -7,7 +25,17 @@ export function sendLoopsEvent(body: { email: string; eventName: string }) { "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) {