better api
This commit is contained in:
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user