From 14687bdfb43d013c8e1b61a5ffa2d6ad5838f5bf Mon Sep 17 00:00:00 2001 From: Anup Cowkur Date: Fri, 20 Sep 2024 14:13:46 +0530 Subject: [PATCH] feat(backend): track email on new user sign in closes #1269 --- backend/api/measure/auth.go | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/backend/api/measure/auth.go b/backend/api/measure/auth.go index 34b8df111..ff0713e5c 100644 --- a/backend/api/measure/auth.go +++ b/backend/api/measure/auth.go @@ -4,9 +4,11 @@ import ( "backend/api/authsession" "backend/api/cipher" "backend/api/server" + "bytes" "encoding/json" "errors" "fmt" + "io" "net/http" "strings" @@ -39,6 +41,51 @@ func extractToken(c *gin.Context) (token string) { return } +// trackEmail tracks email in third party email list +func trackEmail(email string) { + emailListId := "cm1aew9540002el4efgoa48ne" + emailListApiKey := "cm1aew9540003el4e9kefrf4w" + + url := "https://www.waitlist.email/api/subscribers/create" + + requestBody, err := json.Marshal(map[string]string{ + "waitlist": emailListId, + "email": email, + }) + if err != nil { + fmt.Println("Error creating email tracking request body:", err) + return + } + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody)) + if err != nil { + fmt.Println("Error creating email tracking request:", err) + return + } + + // Set headers + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Waitlist-Api-Key", emailListApiKey) + + // Send the request + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + fmt.Println("Error tracking email: ", err) + return + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading email tracking response:", err) + return + } + + fmt.Println("Email tracking Response Status:", resp.Status) + fmt.Println("Email tracking Response Body:", string(body)) +} + // ValidateAPIKey validates the Measure API key. func ValidateAPIKey() gin.HandlerFunc { return func(c *gin.Context) { @@ -318,6 +365,9 @@ func SigninGitHub(c *gin.Context) { }) return } + + // Once new user creation is done, track email + trackEmail(ghUser.Email) } else { // update user's last sign in at value if err := msrUser.touchLastSignInAt(ctx); err != nil { @@ -520,6 +570,9 @@ func SigninGoogle(c *gin.Context) { }) return } + + // Once new user creation is done, track email + trackEmail(googUser.Email) } else { // update user's last sign in at value if err := msrUser.touchLastSignInAt(ctx); err != nil {