client.go 933 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package ga
  2. import (
  3. "net/http"
  4. "net/url"
  5. "strings"
  6. "github.com/pkg/errors"
  7. )
  8. const gaURL = "https://www.google-analytics.com/collect"
  9. var httpClient = &http.Client{}
  10. func send(qs string) error {
  11. req, err := http.NewRequest(http.MethodPost, gaURL, strings.NewReader(qs))
  12. if err != nil {
  13. return errors.Wrap(err, "could not create request")
  14. }
  15. // https://golang.org/pkg/net/http/#Client.Do
  16. // On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when
  17. // CheckRedirect fails, and even then the returned Response.Body is already closed.
  18. resp, err := httpClient.Do(req)
  19. if err != nil {
  20. return errors.Wrap(err, "could not make request")
  21. }
  22. defer resp.Body.Close()
  23. return nil
  24. }
  25. func concatURLValues(v1 url.Values, v2 url.Values) {
  26. for key, values := range v2 {
  27. if len(v2.Get(key)) != 0 && len(v1.Get(key)) == 0 {
  28. // do not replace existed key
  29. v1[key] = values
  30. }
  31. }
  32. }