diff --git a/.env.example b/.env.example index a928760..b61d332 100644 --- a/.env.example +++ b/.env.example @@ -13,4 +13,7 @@ BASE_URL=http://localhost:8080 DELETE_FILES_AFTER=1d # Maximum storage capacity in GB -MAX_STORAGE_GB=10 \ No newline at end of file +MAX_STORAGE_GB=10 + +# Rybbit API Key +ANALYTICS_KEY= \ No newline at end of file diff --git a/controllers/download.go b/controllers/download.go index 8ab7b30..8f9b417 100644 --- a/controllers/download.go +++ b/controllers/download.go @@ -6,6 +6,7 @@ import ( "path/filepath" "github.com/gin-gonic/gin" + "fitra-backend/utils" ) func HandleFileDownload(c *gin.Context) { @@ -23,6 +24,9 @@ func HandleFileDownload(c *gin.Context) { return } + // Send analytics event + utils.SendAnalyticsEvent("file_download", "/api/download/"+fileID+"/"+filename, "File Download", c.GetHeader("User-Agent"), c.ClientIP()) + c.Header("Content-Description", "File Transfer") c.Header("Content-Transfer-Encoding", "binary") c.Header("Content-Disposition", "attachment; filename="+filename) diff --git a/controllers/upload.go b/controllers/upload.go index 809dc10..fe3a41e 100644 --- a/controllers/upload.go +++ b/controllers/upload.go @@ -10,6 +10,7 @@ import ( "strconv" "github.com/gin-gonic/gin" + "fitra-backend/utils" ) func generateID() (string, error) { @@ -99,6 +100,9 @@ func HandleFileUpload(c *gin.Context) { baseURL = "http://localhost:8080" } + // Send analytics event + utils.SendAnalyticsEvent("file_upload", "/api/upload", "File Upload", c.GetHeader("User-Agent"), c.ClientIP()) + c.JSON(http.StatusOK, gin.H{ "message": "File uploaded successfully", "id": fileID, diff --git a/dist/fitra-darwin-amd64 b/dist/fitra-darwin-amd64 index 91ca09e..62d4729 100755 Binary files a/dist/fitra-darwin-amd64 and b/dist/fitra-darwin-amd64 differ diff --git a/dist/fitra-darwin-arm64 b/dist/fitra-darwin-arm64 index 2e5b471..6e84028 100755 Binary files a/dist/fitra-darwin-arm64 and b/dist/fitra-darwin-arm64 differ diff --git a/dist/fitra-linux-amd64 b/dist/fitra-linux-amd64 index 76ad099..fae7ff3 100755 Binary files a/dist/fitra-linux-amd64 and b/dist/fitra-linux-amd64 differ diff --git a/dist/fitra-linux-arm64 b/dist/fitra-linux-arm64 index 8a1d886..b899e68 100755 Binary files a/dist/fitra-linux-arm64 and b/dist/fitra-linux-arm64 differ diff --git a/utils/analytics.go b/utils/analytics.go new file mode 100644 index 0000000..781565e --- /dev/null +++ b/utils/analytics.go @@ -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 +} \ No newline at end of file