Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to use referer when the path is not forced #41

Merged
merged 4 commits into from
Apr 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth documenting what will happen when you specify both.. a path and enable useReferer?

}
}
}
// /account -> account template
if len(params) == 1 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is only invoked if you don't pass in the account name + path.. I'm not sure that's what you're after?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when len(params) == 1 is because the path only contains the account. The account is still required, although we can probably set a default one in some configuration file if non is passed.

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>