Skip to content

Commit

Permalink
Add start and end session recording closes #3089
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Sep 3, 2015
1 parent 978425c commit 045f22b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/github.com/getlantern/flashlight/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@ var (
log = golog.LoggerFor("flashlight.analytics")
)

func Configure(cfg *config.Config, version string) {
func Configure(cfg *config.Config, version string) func() {
if cfg.AutoReport != nil && *cfg.AutoReport {
addr := ""
pubsub.Sub(pubsub.IP, func(ip string) {
log.Debugf("Got IP %v -- starting analytics", ip)
go trackSession(ip, version, cfg.Addr, cfg.InstanceId)
addr = ip
go startSession(ip, version, cfg.Addr, cfg.InstanceId)
})
return func() {
if addr != "" {
log.Debugf("Ending analytics session with ip %v", addr)
endSession(addr, version, cfg.Addr, cfg.InstanceId)
}
}
}
return func() {}
}

func trackSession(ip string, version string, proxyAddr string, clientId string) {
func sessionVals(ip string, version string, clientId string, sc string) string {
vals := make(url.Values, 0)

vals.Add("v", "1")
Expand All @@ -52,8 +61,24 @@ func trackSession(ip string, version string, proxyAddr string, clientId string)
// Custom variable for the Lantern version
vals.Add("cd1", version)

args := vals.Encode()
// This forces the recording of the session duration. It must be either
// "start" or "end". See:
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
vals.Add("sc", sc)
return vals.Encode()
}

func endSession(ip, version, proxyAddr, clientId string) {
args := sessionVals(ip, version, clientId, "end")
trackSession(args, proxyAddr)
}

func startSession(ip, version, proxyAddr, clientId string) {
args := sessionVals(ip, version, clientId, "start")
trackSession(args, proxyAddr)
}

func trackSession(args, proxyAddr string) {
r, err := http.NewRequest("POST", ApiEndpoint, bytes.NewBufferString(args))

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/github.com/getlantern/flashlight/flashlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func applyClientConfig(client *client.Client, cfg *config.Config) {
version, revisionDate)
settings.Configure(cfg, version, revisionDate, buildDate)
proxiedsites.Configure(cfg.ProxiedSites)
analytics.Configure(cfg, version)
addExitFunc(analytics.Configure(cfg, version))
log.Debugf("Proxy all traffic or not: %v", cfg.Client.ProxyAll)
ServeProxyAllPacFile(cfg.Client.ProxyAll)
// Note - we deliberately ignore the error from statreporter.Configure here
Expand Down

0 comments on commit 045f22b

Please sign in to comment.