Skip to content

Commit

Permalink
feat(backend): track email on new user sign in
Browse files Browse the repository at this point in the history
closes #1269
  • Loading branch information
anupcowkur committed Sep 20, 2024
1 parent 7e40577 commit d9db8ee
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions backend/api/measure/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"backend/api/authsession"
"backend/api/cipher"
"backend/api/server"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d9db8ee

Please sign in to comment.