Skip to content
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

feat(backend): track email on new user sign in #1270

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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
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