Skip to content

Commit

Permalink
Added more options to hit and event options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Apr 30, 2022
1 parent 860a7eb commit 1710d28
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.7.0

* added more options to hit and event options

## 1.6.1

* fixed conversion goal return type
Expand Down
72 changes: 42 additions & 30 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,19 @@ type ClientConfig struct {

// HitOptions optional parameters to send with the hit request.
type HitOptions struct {
ScreenWidth int
ScreenHeight int
Hostname string
URL string
IP string
CFConnectingIP string
XForwardedFor string
Forwarded string
XRealIP string
UserAgent string
AcceptLanguage string
Title string
Referrer string
ScreenWidth int
ScreenHeight int
}

// NewClient creates a new client for given client ID, client secret, hostname, and optional configuration.
Expand Down Expand Up @@ -128,20 +139,8 @@ func (client *Client) HitWithOptions(r *http.Request, options *HitOptions) error
options = new(HitOptions)
}

return client.performPost(client.baseURL+hitEndpoint, &Hit{
Hostname: client.hostname,
URL: r.URL.String(),
IP: r.RemoteAddr,
CFConnectingIP: r.Header.Get("CF-Connecting-IP"),
XForwardedFor: r.Header.Get("X-Forwarded-For"),
Forwarded: r.Header.Get("Forwarded"),
XRealIP: r.Header.Get("X-Real-IP"),
UserAgent: r.Header.Get("User-Agent"),
AcceptLanguage: r.Header.Get("Accept-Language"),
Referrer: client.getReferrerFromHeaderOrQuery(r),
ScreenWidth: options.ScreenWidth,
ScreenHeight: options.ScreenHeight,
}, requestRetries)
hit := client.getHit(r, options)
return client.performPost(client.baseURL+hitEndpoint, &hit, requestRetries)
}

// Event sends an event to Pirsch for given http.Request.
Expand All @@ -163,20 +162,7 @@ func (client *Client) EventWithOptions(name string, durationSeconds int, meta ma
Name: name,
DurationSeconds: durationSeconds,
Metadata: meta,
Hit: Hit{
Hostname: client.hostname,
URL: r.URL.String(),
IP: r.RemoteAddr,
CFConnectingIP: r.Header.Get("CF-Connecting-IP"),
XForwardedFor: r.Header.Get("X-Forwarded-For"),
Forwarded: r.Header.Get("Forwarded"),
XRealIP: r.Header.Get("X-Real-IP"),
UserAgent: r.Header.Get("User-Agent"),
AcceptLanguage: r.Header.Get("Accept-Language"),
Referrer: client.getReferrerFromHeaderOrQuery(r),
ScreenWidth: options.ScreenWidth,
ScreenHeight: options.ScreenHeight,
},
Hit: client.getHit(r, options),
}, requestRetries)
}

Expand Down Expand Up @@ -552,6 +538,24 @@ func (client *Client) Keywords(filter *Filter) ([]Keyword, error) {
return stats, nil
}

func (client *Client) getHit(r *http.Request, options *HitOptions) Hit {
return Hit{
Hostname: client.selectField(options.Hostname, client.hostname),
URL: client.selectField(options.URL, r.URL.String()),
IP: client.selectField(options.IP, r.RemoteAddr),
CFConnectingIP: client.selectField(options.CFConnectingIP, r.Header.Get("CF-Connecting-IP")),
XForwardedFor: client.selectField(options.XForwardedFor, r.Header.Get("X-Forwarded-For")),
Forwarded: client.selectField(options.Forwarded, r.Header.Get("Forwarded")),
XRealIP: client.selectField(options.XRealIP, r.Header.Get("X-Real-IP")),
UserAgent: client.selectField(options.UserAgent, r.Header.Get("User-Agent")),
AcceptLanguage: client.selectField(options.AcceptLanguage, r.Header.Get("Accept-Language")),
Title: options.Title,
Referrer: client.selectField(options.Referrer, client.getReferrerFromHeaderOrQuery(r)),
ScreenWidth: options.ScreenWidth,
ScreenHeight: options.ScreenHeight,
}
}

func (client *Client) getReferrerFromHeaderOrQuery(r *http.Request) string {
referrer := r.Header.Get("Referer")

Expand Down Expand Up @@ -786,3 +790,11 @@ func (client *Client) getStatsRequestURL(endpoint string, filter *Filter) string
func (client *Client) waitBeforeNextRequest(retry int) {
time.Sleep(time.Second * time.Duration(requestRetries-retry))
}

func (client *Client) selectField(a, b string) string {
if a != "" {
return a
}

return b
}
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Hit struct {
XRealIP string `json:"x_real_ip"`
UserAgent string `json:"user_agent"`
AcceptLanguage string `json:"accept_language"`
Title string `json:"title"`
Referrer string `json:"referrer"`
ScreenWidth int `json:"screen_width"`
ScreenHeight int `json:"screen_height"`
Expand Down

0 comments on commit 1710d28

Please sign in to comment.