Skip to content

Commit

Permalink
Merge pull request #40 from getlantern/issue-3581
Browse files Browse the repository at this point in the history
Tracking analytics per server closes getlantern/lantern#3581
  • Loading branch information
myleshorton committed Mar 7, 2016
2 parents 62217ab + a9cca59 commit eccff71
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -42,6 +43,7 @@ type siteAccess struct {
// handler chain.
type AnalyticsMiddleware struct {
trackingId string
hostname string
samplePercentage float64
next http.Handler
siteAccesses chan *siteAccess
Expand All @@ -50,9 +52,15 @@ type AnalyticsMiddleware struct {
}

func New(trackingId string, samplePercentage float64, next http.Handler) *AnalyticsMiddleware {
log.Debugf("Will report analytics to Google as %v, sampling %d percent of requests", trackingId, int(samplePercentage*100))
hostname, err := os.Hostname()
if err != nil {
log.Errorf("Unable to determine hostname, will use '(direct))': %v", hostname)
hostname = "(direct)"
}
log.Debugf("Will report analytics to Google as %v using hostname '%v', sampling %d percent of requests", trackingId, hostname, int(samplePercentage*100))
am := &AnalyticsMiddleware{
trackingId: trackingId,
hostname: hostname,
samplePercentage: samplePercentage,
next: next,
siteAccesses: make(chan *siteAccess, 1000),
Expand Down Expand Up @@ -121,6 +129,14 @@ func (am *AnalyticsMiddleware) sessionVals(sa *siteAccess, site string) string {
// Use the user-agent reported by the client
vals.Add("ua", sa.userAgent)

// Use the server's hostname as the campaign source so that we can track
// activity per server
vals.Add("cs", am.hostname)
// Campaign medium and campaign name are required for campaign tracking to do
// anything. We just fill them in with some dummy values.
vals.Add("cm", "proxy")
vals.Add("cn", "proxy")

// Note the absence of session tracking. We don't have a good way to tell
// when a session ends, so we don't bother with it.

Expand Down

0 comments on commit eccff71

Please sign in to comment.