Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
giuem committed Jul 17, 2017
0 parents commit 5cc03d0be0cf53eb81d2bd80299fbd9906038a30
Showing with 155 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +21 −0 LICENSE
  3. +20 −0 README.md
  4. +93 −0 main.go
  5. +19 −0 script.js
@@ -0,0 +1,2 @@
# build dir
build/
21 LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Giuem

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,20 @@
# ga-proxy

Speed up Google Analytics



## Build

1. Get `gox`

```bash
go get github.com/mitchellh/gox
```

2. Build

```bash
gox -ldflags="-s -w" -output="build/{{.Dir}}{{.OS}}{{.Arch}}"
```

93 main.go
@@ -0,0 +1,93 @@
package main

import (
"log"
"net/http"

"flag"

"github.com/satori/go.uuid"
"github.com/tomasen/realip"
)

// SendData send data to google server
func SendData(uid string, req *http.Request) {
newReq, err := http.NewRequest("GET", "https://www.google-analytics.com/collect", nil)
if err != nil {
log.Fatal(err)
}

q := newReq.URL.Query()
q.Add("v", "1")
q.Add("t", "pageview")
q.Add("tid", req.FormValue("ga"))
q.Add("cid", uid)
q.Add("dl", req.Referer())
q.Add("uip", realip.RealIP(req))
q.Add("ua", req.UserAgent())
q.Add("dt", req.FormValue("dt"))
q.Add("dr", req.FormValue("dr"))
q.Add("ul", req.FormValue("ul"))
q.Add("sd", req.FormValue("sd"))
q.Add("sr", req.FormValue("sr"))
q.Add("vp", req.FormValue("vp"))
q.Add("z", req.FormValue("z"))
newReq.URL.RawQuery = q.Encode()
client := &http.Client{}
resp, err := client.Do(newReq)
if err != nil {
log.Fatal(err)
}

defer resp.Body.Close()

log.Printf("SEND %s\n", newReq.URL.RawQuery)
}

// GetOrSetUUID get uuid of current user or set new
func GetOrSetUUID(w http.ResponseWriter, req *http.Request) string {
cookie, err := req.Cookie("uuid")
var uid string
if err == http.ErrNoCookie {
ns := uuid.NewV4()
uid = uuid.NewV5(ns, req.Form.Encode()+req.UserAgent()+req.RemoteAddr).String()
http.SetCookie(w, &http.Cookie{
Name: "uuid",
Value: uid,
Path: "/",
MaxAge: 315360000,
HttpOnly: true,
})
} else {
uid = cookie.Value
}
return uid
}

// ServerHandle handle proxy logic
func ServerHandle(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
return
}

w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Add("Content-Type", "image/gif")

uid := GetOrSetUUID(w, req)

w.WriteHeader(http.StatusNoContent)

go SendData(uid, req)
}

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

func main() {
httpAddr := flag.String("http_addr", ":80", "http listen addr")
flag.Parse()

log.Println("http server start at:", *httpAddr)
log.Fatal(http.ListenAndServe(*httpAddr, nil))
}
@@ -0,0 +1,19 @@
var ga = "UA-102578664-1";
(function(ga, url,window, document, navigator, location) {
var screen = window.screen;
var encode = encodeURIComponent;

var data = [
'ga=' + ga,
'dt=' + encode(document.title),
'dr=' + encode(document.referrer),
'ul=' + (navigator.language || navigator.browserLanguage || navigator.userLanguage),
'sd=' + screen.colorDepth + '-bit',
'sr=' + screen.width + 'x' + screen.height,
'vp=' + Math.max(document.documentElement.clientWidth, window.innerWidth || 0) + 'x' + Math.max(document.documentElement.clientHeight, window.innerHeight || 0),
'z=' + (+new Date)
];

window.__beacon_img = new Image();
window.__beacon_img.src = url + '?' + data.join('&');;
})(ga, "https://ga.giuem.com",window, document, navigator, location);

0 comments on commit 5cc03d0

Please sign in to comment.
You can’t perform that action at this time.