Skip to content

Commit

Permalink
feat: server-side google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Feb 7, 2019
1 parent 42b6a18 commit 8e6f0cc
Show file tree
Hide file tree
Showing 21 changed files with 2,069 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PASSWORD ?= 'xor+=cool'

.PHONY: run
run: install
yolo -t $(CIRCLE_TOKEN) serve --debug --no-slack
yolo -t $(CIRCLE_TOKEN) serve --debug --no-slack --no-ga

.PHONY: install
install:
Expand Down
3 changes: 2 additions & 1 deletion cmd/yolo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func init() {
serveCmd.PersistentFlags().StringVarP(&serverCfg.Username, "username", "u", "berty", "user")
serveCmd.PersistentFlags().StringVarP(&serverCfg.Password, "password", "p", "", "password")
serveCmd.PersistentFlags().BoolVarP(&serverCfg.Debug, "debug", "", false, "debug mode")
serveCmd.PersistentFlags().BoolVarP(&serverCfg.NoSlack, "no-slack", "", false, "disable-slack")
serveCmd.PersistentFlags().BoolVarP(&serverCfg.NoSlack, "no-slack", "", false, "disable slack")
serveCmd.PersistentFlags().BoolVarP(&serverCfg.NoGa, "no-ga", "", false, "disable google analytics")

rootCmd.AddCommand(serveCmd)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead
github.com/huandu/xstrings v1.2.0 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/jpillora/go-ogle-analytics v0.0.0-20161213085824-14b04e0594ef
github.com/jszwedko/go-circleci v0.2.0
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/labstack/echo v3.3.10+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ github.com/jmoiron/sqlx v0.0.0-20180614180643-0dae4fefe7c0/go.mod h1:IiEW3SEiiEr
github.com/joho/godotenv v1.2.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/jpillora/go-ogle-analytics v0.0.0-20161213085824-14b04e0594ef h1:jLpa0vamfyIGeIJ/CfUJEWoKriw4ODeOgF1XxDvgMZ4=
github.com/jpillora/go-ogle-analytics v0.0.0-20161213085824-14b04e0594ef/go.mod h1:PlwhC7q1VSK73InDzdDatVetQrTsQHIbOvcJAZzitY0=
github.com/jszwedko/go-circleci v0.2.0 h1:ytpLKtpjQn2wq+/IArlBvqfb1CgbwA2UiBGXCwWqNw4=
github.com/jszwedko/go-circleci v0.2.0/go.mod h1:z1630OiB7oGxZwE90het04Ld7rIu0AKvY9JCRnaBdoE=
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
Expand Down
62 changes: 61 additions & 1 deletion server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

slack "github.com/ashwanthkumar/slack-go-webhook"
"github.com/hako/durafmt"
ga "github.com/jpillora/go-ogle-analytics"
circleci "github.com/jszwedko/go-circleci"
"github.com/labstack/echo"
"github.com/labstack/echo-contrib/session"
Expand Down Expand Up @@ -109,16 +110,29 @@ func userProfileFromContext(c echo.Context) (map[string]interface{}, error) {
return nil, nil
}

func clientIDFromContext(c echo.Context) string {
username := "anonymous"
profile, err := userProfileFromContext(c)
if err != nil {
username = err.Error()
}
if profile != nil {
username = profile["name"].(string)
}
username += ":" + c.RealIP()
return username
}

func (s *Server) sendUserActionToSlack(c echo.Context, action, color, channel, floodChannel string) {
if s.NoSlack {
return
}

profile, err := userProfileFromContext(c)
attachment := slack.Attachment{}
auth := c.RealIP()

username := "anonymous"
profile, err := userProfileFromContext(c)
if err != nil {
username = err.Error()
}
Expand Down Expand Up @@ -172,10 +186,12 @@ func (s *Server) sendUserActionToSlack(c echo.Context, action, color, channel, f
}

func (s *Server) sendUserErrorToSlack(c echo.Context, err error) {
s.ga(c, ga.NewException().Description(err.Error()))
s.sendUserActionToSlack(c, fmt.Sprintf("error: %v", err), "#ff0000", "#yolodebug", "#yolodebug")
}

func (s *Server) GetIPA(c echo.Context) error {
s.ga(c, ga.NewPageview())
s.sendUserActionToSlack(c, "IPA download", "#0000ff", "#yolologs", "#yolologs")
id := c.Param("*")
arts, err := s.client.GetArtifacts(id, true)
Expand All @@ -201,6 +217,7 @@ func (s *Server) GetIPA(c echo.Context) error {
}

func (s *Server) GetAPK(c echo.Context) error {
s.ga(c, ga.NewPageview())
s.sendUserActionToSlack(c, "Android download", "#00ff00", "#yolologs", "#yolologs")
id := c.Param("*")
arts, err := s.client.GetArtifacts(id, true)
Expand Down Expand Up @@ -244,6 +261,7 @@ func (s *Server) ListReleaseAndroidBetaJson(c echo.Context) error {
}

func (s *Server) ListReleaseJson(c echo.Context, job string) error {
s.ga(c, ga.NewPageview())
// FIXME: reuse code from ListRelease
type release struct {
Branch string `json:"branch"`
Expand Down Expand Up @@ -371,7 +389,47 @@ type ReleasesDay struct {
Releases []ReleaseEntry
}

func (s *Server) ga(c echo.Context, event interface{}) {
if s.NoGa {
return
}
go func() {
analytics, err := ga.NewClient("UA-133664781-3")
if err != nil {
c.Logger().Warn("failed to initialize GA client: ", fmt.Sprintf("%v", err))
return
}

analytics.ClientID(clientIDFromContext(c))
analytics.UserAgentOverride(c.Request().Header.Get("User-Agent"))
analytics.DocumentLocationURL(c.Request().URL.String())
ref := c.Request().Header.Get("Referrer")
if ref == "" {
ref = c.Request().Header.Get("Origin")
}
analytics.DocumentReferrer(ref)
analytics.IPOverride(c.RealIP())
switch e := event.(type) {
case *ga.Pageview:
if err := analytics.Send(e); err != nil {
c.Logger().Warn("failed to send analytics: ", fmt.Sprintf("%v", err))
}
case *ga.Exception:
if err := analytics.Send(e); err != nil {
c.Logger().Warn("failed to send analytics: ", fmt.Sprintf("%v", err))
}
case *ga.Event:
if err := analytics.Send(e); err != nil {
c.Logger().Warn("failed to send analytics: ", fmt.Sprintf("%v", err))
}
default:
c.Logger().Warn("unknown event type %T", e)
}
}()
}

func (s *Server) ListRelease(c echo.Context, job string) error {
s.ga(c, ga.NewPageview())
s.sendUserActionToSlack(c, fmt.Sprintf("List Releases (%s)", job), "#00ffff", "#yolologs", "#yolodebug")

data := map[string]interface{}{}
Expand All @@ -386,6 +444,7 @@ func (s *Server) ListRelease(c echo.Context, job string) error {
}

func (s *Server) GetReleasesByDate(c echo.Context, job string) (*ReleasesByDate, error) {
s.ga(c, ga.NewPageview())
releasesByDateMap := map[string][]ReleaseEntry{}

oncePerBranch := map[string]bool{}
Expand Down Expand Up @@ -547,6 +606,7 @@ func (s *Server) ReleaseIOS(c echo.Context) error {
}

func (s *Server) Itms(c echo.Context) error {
s.ga(c, ga.NewPageview())
s.sendUserActionToSlack(c, "ITMS download", "#0000ff", "#yolologs", "#yolologs")

pull := c.Param("*")
Expand Down
9 changes: 7 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"github.com/oxtoacart/bpool"
)

var reAndroidAgent = regexp.MustCompile("(?i)android")
var reIOSAgent = regexp.MustCompile("(?i)iPad|iPhone|iPod")
var (
reAndroidAgent = regexp.MustCompile("(?i)android")
reIOSAgent = regexp.MustCompile("(?i)iPad|iPhone|iPod")
)

type httperror struct {
message string `json:message`
Expand All @@ -40,6 +42,7 @@ type ServerConfig struct {

Debug bool
NoSlack bool
NoGa bool
}

type buildMap map[int]*circleci.Build
Expand Down Expand Up @@ -79,6 +82,7 @@ type Server struct {
cache Cache
Debug bool
NoSlack bool
NoGa bool

// templates/static
funcmap *ctxFuncmap
Expand Down Expand Up @@ -124,6 +128,7 @@ func NewServer(cfg *ServerConfig) (*Server, error) {
TemplatesBox: &templatesBox,
Debug: cfg.Debug,
NoSlack: cfg.NoSlack,
NoGa: cfg.NoGa,
}
e.HTTPErrorHandler = func(err error, c echo.Context) {
s.sendUserErrorToSlack(c, err)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/jpillora/go-ogle-analytics/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions vendor/github.com/jpillora/go-ogle-analytics/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions vendor/github.com/jpillora/go-ogle-analytics/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8e6f0cc

Please sign in to comment.