diff --git a/.env.example b/.env.example index b61d332..74e47a2 100644 --- a/.env.example +++ b/.env.example @@ -16,4 +16,8 @@ DELETE_FILES_AFTER=1d MAX_STORAGE_GB=10 # Rybbit API Key -ANALYTICS_KEY= \ No newline at end of file +ANALYTICS_KEY= + +# Rybbit configuration +RYBBIT_API_KEY= +RYBBIT_SITE_ID= \ No newline at end of file diff --git a/controllers/download.go b/controllers/download.go index 8f9b417..8e046dd 100644 --- a/controllers/download.go +++ b/controllers/download.go @@ -5,8 +5,9 @@ import ( "os" "path/filepath" - "github.com/gin-gonic/gin" "fitra-backend/utils" + + "github.com/gin-gonic/gin" ) func HandleFileDownload(c *gin.Context) { @@ -25,7 +26,7 @@ func HandleFileDownload(c *gin.Context) { } // Send analytics event - utils.SendAnalyticsEvent("file_download", "/api/download/"+fileID+"/"+filename, "File Download", c.GetHeader("User-Agent"), c.ClientIP()) + utils.SendAnalyticsEvent("pageview", "/api/download/"+fileID+"/"+filename) c.Header("Content-Description", "File Transfer") c.Header("Content-Transfer-Encoding", "binary") diff --git a/controllers/upload.go b/controllers/upload.go index fe3a41e..673c082 100644 --- a/controllers/upload.go +++ b/controllers/upload.go @@ -9,8 +9,9 @@ import ( "path/filepath" "strconv" - "github.com/gin-gonic/gin" "fitra-backend/utils" + + "github.com/gin-gonic/gin" ) func generateID() (string, error) { @@ -101,7 +102,7 @@ func HandleFileUpload(c *gin.Context) { } // Send analytics event - utils.SendAnalyticsEvent("file_upload", "/api/upload", "File Upload", c.GetHeader("User-Agent"), c.ClientIP()) + utils.SendAnalyticsEvent("pageview", "/api/upload") c.JSON(http.StatusOK, gin.H{ "message": "File uploaded successfully", diff --git a/dist/fitra-darwin-amd64 b/dist/fitra-darwin-amd64 index ef4132d..0d1369a 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 34f3fe5..39fb621 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 cf0f4b7..5824591 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 7d2955a..c05f922 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 index 50a2db2..05b5129 100644 --- a/utils/analytics.go +++ b/utils/analytics.go @@ -7,52 +7,45 @@ import ( "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"` +type TrackingEvent struct { + APIKey string `json:"api_key"` + SiteID string `json:"site_id"` + Type string `json:"type"` + Pathname string `json:"pathname"` + Hostname string `json:"hostname,omitempty"` + EventName string `json:"event_name,omitempty"` + Properties string `json:"properties,omitempty"` } -func SendAnalyticsEvent(eventType, pathname, pageTitle, userAgent, ipAddress string) error { - apiKey := os.Getenv("ANALYTICS_KEY") - if apiKey == "" { +func trackEvent(event TrackingEvent) error { + event.APIKey = os.Getenv("RYBBIT_API_KEY") + event.SiteID = os.Getenv("RYBBIT_SITE_ID") + + if event.APIKey == "" { return nil // Skip if no API key configured } - hostname := os.Getenv("BASE_URL") - if hostname == "" { - hostname = "localhost:8080" - } - - 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://a.adhd.sh/api/track", bytes.NewBuffer(jsonData)) + resp, err := http.Post( + "https://a.adhd.sh/api/track", + "application/json", + bytes.NewBuffer(jsonData), + ) if err != nil { return err } + defer resp.Body.Close() - req.Header.Set("Content-Type", "application/json") - - client := &http.Client{} - _, err = client.Do(req) - return err + return nil +} + +func SendAnalyticsEvent(eventType, pathname string) error { + return trackEvent(TrackingEvent{ + Type: eventType, + Pathname: pathname, + }) }