Skip to content

Commit dd96cd4

Browse files
committed
Merge pull request #41 from Guisardo/master
try to use referer when the path is not forced
2 parents bfec0d6 + 3f69cf0 commit dd96cd4

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Or RDoc:
3434

3535
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!
3636

37+
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.
3738

3839
### FAQ
3940

ga-beacon/ga-beacon.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,33 @@ func handler(w http.ResponseWriter, r *http.Request) {
9494
c := appengine.NewContext(r)
9595
params := strings.SplitN(strings.Trim(r.URL.Path, "/"), "/", 2)
9696
query, _ := url.ParseQuery(r.URL.RawQuery)
97+
refOrg := r.Header.Get("Referer")
9798

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

105+
// activate referrer path if ?useReferer is used and if referer exists
106+
if _, ok := query["useReferer"]; ok {
107+
if len(refOrg) != 0 {
108+
referer := strings.Replace(strings.Replace(refOrg, "http://", "", 1), "https://", "", 1);
109+
if len(referer) != 0 {
110+
// if the useReferer is present and the referer information exists
111+
// the path is ignored and the beacon referer information is used instead.
112+
params = strings.SplitN(strings.Trim(r.URL.Path, "/") + "/" + referer, "/", 2)
113+
}
114+
}
115+
}
104116
// /account -> account template
105117
if len(params) == 1 {
106118
templateParams := struct {
107119
Account string
120+
Referer string
108121
}{
109122
Account: params[0],
123+
Referer: refOrg,
110124
}
111125
if err := pageTemplate.ExecuteTemplate(w, "page.html", templateParams); err != nil {
112126
http.Error(w, "could not show account page", 500)

ga-beacon/page.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

0 commit comments

Comments
 (0)