github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/analytics/client/sync/reporters/pixel.go (about) 1 package reporters 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 "os" 8 9 "github.com/ActiveState/cli/internal/analytics/dimensions" 10 "github.com/ActiveState/cli/internal/constants" 11 "github.com/ActiveState/cli/internal/errs" 12 ) 13 14 type PixelReporter struct { 15 url string 16 } 17 18 func NewPixelReporter() *PixelReporter { 19 var pixelUrl string 20 21 // Attempt to get the value for the pixel URL from the environment. Fall back to default if that fails 22 if pixelUrl = os.Getenv(constants.AnalyticsPixelOverrideEnv); pixelUrl == "" { 23 pixelUrl = constants.DefaultAnalyticsPixel 24 } 25 return &PixelReporter{pixelUrl} 26 } 27 28 func (r *PixelReporter) ID() string { 29 return "PixelReporter" 30 } 31 32 func (r *PixelReporter) Event(category, action, source, label string, d *dimensions.Values) error { 33 pixelURL, err := url.Parse(r.url) 34 if err != nil { 35 return errs.Wrap(err, "Invalid pixel URL: %s", r.url) 36 } 37 38 query := &url.Values{} 39 query.Add("x-category", category) 40 query.Add("x-action", action) 41 query.Add("x-source", source) 42 query.Add("x-label", label) 43 44 for num, value := range legacyDimensionMap(d) { 45 key := fmt.Sprintf("x-custom%s", num) 46 query.Add(key, value) 47 } 48 pixelURL.RawQuery = query.Encode() 49 50 // logging.Debug("Using S3 pixel URL: %v", pixelURL.String()) 51 _, err = http.Head(pixelURL.String()) 52 if err != nil { 53 return errs.Wrap(err, "Could not download S3 pixel") 54 } 55 56 return nil 57 }