Skip to content

Commit

Permalink
Merge pull request #41 from Guisardo/master
Browse files Browse the repository at this point in the history
try to use referer when the path is not forced
  • Loading branch information
igrigorik committed Apr 9, 2016
2 parents bfec0d6 + 3f69cf0 commit dd96cd4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Or RDoc:

If you prefer, you can skip the badge and use a transparent pixel. To do so, simply append `?pixel` to the image URL. There are also "flat" style variants available, which are available when appending `?flat` or `?flat-gif` to the image URL. And that's it, add the tracker image to the pages you want to track and then head to your Google Analytics account to see real-time and aggregated visit analytics for your projects!

You may also auto-calculate the tracking path based in the "referer" information of the image. To activate this simple add `?useReferrer` to the image URL (or `&useReferer` if you need to combine this with the `?pixel`, `?flat` or `?flat-gif` parameter). Although they are some odd browsers that don't always send the referer header, the amount of traffic coming from those browsers is usually not relevant at all. Of course that if you need to measure the traffic from those odd browsers you should not use this method.

### FAQ

Expand Down
14 changes: 14 additions & 0 deletions ga-beacon/ga-beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,33 @@ func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
params := strings.SplitN(strings.Trim(r.URL.Path, "/"), "/", 2)
query, _ := url.ParseQuery(r.URL.RawQuery)
refOrg := r.Header.Get("Referer")

// / -> redirect
if len(params[0]) == 0 {
http.Redirect(w, r, "https://github.com/igrigorik/ga-beacon", http.StatusFound)
return
}

// activate referrer path if ?useReferer is used and if referer exists
if _, ok := query["useReferer"]; ok {
if len(refOrg) != 0 {
referer := strings.Replace(strings.Replace(refOrg, "http://", "", 1), "https://", "", 1);
if len(referer) != 0 {
// if the useReferer is present and the referer information exists
// the path is ignored and the beacon referer information is used instead.
params = strings.SplitN(strings.Trim(r.URL.Path, "/") + "/" + referer, "/", 2)
}
}
}
// /account -> account template
if len(params) == 1 {
templateParams := struct {
Account string
Referer string
}{
Account: params[0],
Referer: refOrg,
}
if err := pageTemplate.ExecuteTemplate(w, "page.html", templateParams); err != nil {
http.Error(w, "could not show account page", 500)
Expand Down
1 change: 1 addition & 0 deletions ga-beacon/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<body>
<p>GA account: {{.Account}}</p>
<p>Beacon Referrer: {{.Referer}}</p>
<p>Setup instructions: <a href="https://github.com/igrigorik/ga-beacon">https://github.com/igrigorik/ga-beacon</a></p>
</body>
</html>

0 comments on commit dd96cd4

Please sign in to comment.