Permalink
Browse files

barebones GA account server

  • Loading branch information...
igrigorik committed Jan 1, 2014
0 parents commit 1c7ff2e5fb78c621f1791c62c8b76a2b7af06b0f
Showing with 94 additions and 0 deletions.
  1. +9 −0 .gitignore
  2. +10 −0 app.yaml
  3. +24 −0 repostats/page.html
  4. +51 −0 repostats/repostats.go
@@ -0,0 +1,9 @@
*.6
*.8
*.o
*.so
*.cgo?.*
_cgo_*
_test*
*.out
_obj
@@ -0,0 +1,10 @@
application: repostats
version: 1
runtime: go
api_version: go1
default_expiration: "1d"
handlers:
- url: /.*
script: _go_app
@@ -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}}
@@ -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.