directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stefano Bagnara <apa...@bago.org>
Subject Re: Adding some google analytics to our downloads
Date Mon, 10 Sep 2007 23:07:54 GMT
Emmanuel Lecharny ha scritto:
> Hi,
> 
> we can't currently use GA to count the number of server downloads,
> because the link is on a dynamic server (a mirror).
> 
> We should create a page where we redirect to these dynamic pages, so
> that we can have a GA count.
> 
> Who want to do that ?
> 
> Thanks !


In Apache JAMES I added a javascript tracking to the download links.
As we generate the pages using maven I added it to our skin.

Here is the javascript that automatically track every link to gz, bz2,
zip, jar, asc, sar files. Please note that the trackDownloads methods
include the JAMES specific way to find the common part of the path in
mirror strings so that they are rewritten to /downloads/relativefilepaths.

---------------------
<script type="text/javascript">
if (document.getElementsByTagName) {
  var hrefs = document.getElementsByTagName('a');
  var extensions = ["gz","bz2","zip","jar","asc","sar"];
  for (var l = 0; l < hrefs.length; l++) {
    // 0+ is a workaround for download.cgi script by ASF that
    // is not happy with simple words in square brackets
    if (hrefs[0+l] != "") {
      var path = hrefs[0+l].pathname;
      var external = hrefs[0+l].hostname != location.host;
      if (external) {
        var splitted = path.split('.');
        var ext = splitted[0+splitted.length-1];
        for (var e = 0; e < extensions.length; e++) {
          if (extensions[0+e] == ext) {
            startListening(hrefs[0+l],"click",trackDownloads);
          }
	}
      }
    }
  }
}

function startListening (obj,evnt,func) {
  if (obj.addEventListener) {
    obj.addEventListener(evnt,func,false);
  } else if (obj.attachEvent) {
    obj.attachEvent("on" + evnt,func);
  }
}

function trackDownloads (evnt) {
  var lnk;
  if (evnt.srcElement) {
    var elmnt = evnt.srcElement;
    while (elmnt.tagName != "A") {
      var newelmnt = elmnt.parentNode;
      elmnt = newelmnt;
    }
    lnk = elmnt.hostname + "/" + elmnt.pathname + elmnt.search;
  } else {
    lnk = this.hostname + this.pathname + this.search;
  }

  var pos = lnk.indexOf("/james/");
  if (pos != -1) {
    lnk = lnk.substr(pos+7);
  }
  lnk = "/downloads/"+lnk;

  if (typeof(urchinTracker) == "function") {
    urchinTracker(lnk);
  }
}
</script>
------------------------------------

Hope this helps,
Stefano


Mime
View raw message