Added tracking for uploading and downloading files

This commit is contained in:
2025-08-17 00:47:10 +02:00
parent 91fb6c62aa
commit 496f9140a1
8 changed files with 65 additions and 1 deletions

53
utils/analytics.go Normal file
View File

@@ -0,0 +1,53 @@
package utils
import (
"bytes"
"encoding/json"
"net/http"
"os"
)
type AnalyticsEvent struct {
APIKey string `json:"api_key"`
SiteID string `json:"site_id"`
Type string `json:"type"`
Pathname string `json:"pathname"`
Hostname string `json:"hostname"`
PageTitle string `json:"page_title"`
UserAgent string `json:"user_agent"`
IPAddress string `json:"ip_address"`
}
func SendAnalyticsEvent(eventType, pathname, pageTitle, userAgent, ipAddress string) error {
apiKey := os.Getenv("ANALYTICS_KEY")
if apiKey == "" {
return nil // Skip if no API key configured
}
event := AnalyticsEvent{
APIKey: apiKey,
SiteID: "fitra-backend",
Type: eventType,
Pathname: pathname,
Hostname: "a.adhd.sh",
PageTitle: pageTitle,
UserAgent: userAgent,
IPAddress: ipAddress,
}
jsonData, err := json.Marshal(event)
if err != nil {
return err
}
req, err := http.NewRequest("POST", "https://app.rybbit.io/api/track", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
_, err = client.Do(req)
return err
}