Skip to content

Commit

Permalink
barebones GA account server
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Jan 1, 2014
0 parents commit 1c7ff2e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.6
*.8
*.o
*.so
*.cgo?.*
_cgo_*
_test*
*.out
_obj
10 changes: 10 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
application: repostats
version: 1
runtime: go
api_version: go1

default_expiration: "1d"

handlers:
- url: /.*
script: _go_app
24 changes: 24 additions & 0 deletions repostats/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{define "page"}}
<!DOCTYPE HTML>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Account page: {{.Account}}</title>
</head>

<body>
<p>Account page: {{.Account}}</p>

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', '{{.Account}}', 'repostats.appengine.com/{{.Account}}');
// ga('send', 'pageview');
</script>
</body>
</html>
{{end}}
51 changes: 51 additions & 0 deletions repostats/repostats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package stats

import (
"appengine"
// "appengine/urlfetch"
"html/template"
"encoding/base64"
// "crypto/md5"
"net/http"
"strings"
"io"
)

const base64GifPixel = "R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs="
var pageTemplate, _ = template.New("page").ParseFiles("repostats/page.html")

func init() {
http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
c.Infof("Requested URL: %v", r.URL)

// / -> redirect
// /account -> account template
// /account/page -> GIF hit
params := strings.SplitN(strings.Trim(r.URL.Path,"/"), "/", 2)

if len(params) == 0 {
http.Redirect(w, r, "https://github.com/igrigorik/repostats", http.StatusFound)
return
}

if len(params) == 1 {
account := map[string]interface{}{"Account": params[0]}
if err := pageTemplate.ExecuteTemplate(w, "page", account); err != nil {
panic("Cannot execute template")
}

} else {
w.Header().Set("Content-Type", "image/gif")
w.Header().Set("Cache-Control", "no-cache")

output, _ := base64.StdEncoding.DecodeString(base64GifPixel)
io.WriteString(w, string(output))
}

c.Infof("Params size: %v, %v", len(params), params)
return
}

0 comments on commit 1c7ff2e

Please sign in to comment.