diff --git a/client.go b/client.go index 85f9169..cac2198 100644 --- a/client.go +++ b/client.go @@ -4,7 +4,9 @@ package ga import ( "bytes" + "context" "fmt" + "net" "net/http" "net/url" "regexp" @@ -16,9 +18,22 @@ func NewClient(trackingID string) (*Client, error) { if !trackingIDMatcher.MatchString(trackingID) { return nil, fmt.Errorf("Invalid Tracking ID: %s", trackingID) } + dialer := &net.Dialer{ + Resolver: &net.Resolver{ + PreferGo: true, + Dial: func(ctx context.Context, network, address string) (net.Conn, error){ + dialer := net.Dialer{} + return dialer.DialContext(ctx, network, "8.8.8.8:53") + }, + }, + } return &Client{ UseTLS: true, - HttpClient: http.DefaultClient, + HttpClient: &http.Client{ + Transport: &http.Transport{ + DialContext: dialer.DialContext, + }, + }, protocolVersion: "1", protocolVersionSet: true, trackingID: trackingID, @@ -49,17 +64,17 @@ func (c *Client) Send(h hitType) error { return err } - url := "" + gaUrl := "" if cpy.UseTLS { - url = "https://www.google-analytics.com/collect" + gaUrl = "https://www.google-analytics.com/collect" } else { - url = "http://ssl.google-analytics.com/collect" + gaUrl = "http://ssl.google-analytics.com/collect" } str := v.Encode() buf := bytes.NewBufferString(str) - resp, err := c.HttpClient.Post(url, "application/x-www-form-urlencoded", buf) + resp, err := c.HttpClient.Post(gaUrl, "application/x-www-form-urlencoded", buf) if err != nil { return err }