Created
December 15, 2011 12:13
-
-
Save leon/1480885 to your computer and use it in GitHub Desktop.
MooTools Analytics Tracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Monitors link clicks and pushes them to google analytics | |
It automatically tracks mail links and downloads, and if a link doesn't leed to the same domain it tracks it as an outbound link. | |
based on the drupalmodule http://drupalcontrib.org/api/drupal/contributions--google_analytics--googleanalytics.module | |
@author Leon Radley | |
Custom tracking example: | |
Tracker.track('download', 'customfile', 'mycustomfile'); | |
*/ | |
var _gaq = _gaq || []; | |
var Tracker = new Class({ | |
Implements: [Options], | |
options: { | |
// track downloads | |
downloads: true, | |
// what extensions to track | |
extensions: '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip', | |
// track mail links | |
mailto: true, | |
// track outbound links | |
outbound: true, | |
// outbound links show up under exit links | |
outboundAsPageView: false, | |
// subdomain.domain.com|subdomain2.domain.com | |
crossDomains: null | |
}, | |
initialize: function(options){ | |
this.setOptions(options); | |
this.isInternal = new RegExp('^(https?):\/\/' + window.location.host, 'i'); | |
this.isDownload = new RegExp('\\.(' + this.options.extensions + ')$', 'i'); | |
this.options.crossDomains = this.options.crossDomains ? this.options.crossDomains.replace(/[-[\]{}()*+?.,\\^$#\s]/g, '\\$&') : ''; | |
this.isCrossDomain = new RegExp('^(https?|ftp|news|nntp|telnet|irc|ssh|sftp|webcal):\/\/.*(' + this.options.crossDomains + ')', 'i'); | |
document.addEvent('click:relay(a)', this.track.bind(this)); | |
}, | |
track: function(event, a){ | |
if(this.isInternal.test(a.href)){ | |
if(this.options.download && this.isDownload.test(a.href)){ | |
var extension = this.isDownload.exec(a.href); | |
_gaq.push(['_trackEvent', 'Downloads', extension[1].toLowerCase(), a.href.replace(this.isInternal, '')]); | |
} | |
} else { | |
if(this.options.mailto && a.href.test(/^mailto:/)){ | |
_gaq.push(['_trackEvent', 'Mails', 'Click', a.href.substring(7)]); | |
} else if(this.options.outbound && a.href){ | |
if(this.isCrossDomain.test(a.href)){ | |
_gaq.push(["_link", a.href]); | |
} else if(this.options.outboundAsPageView){ | |
_gaq.push(["_trackPageview", '/outbound/' + a.href.replace(/^(https?|ftp|news|nntp|telnet|irc|ssh|sftp|webcal):\/\//i, '').split('/').join('--')]); | |
} else { | |
_gaq.push(["_trackEvent", "Outbound links", "Click", a.href]); | |
} | |
} | |
} | |
} | |
}); | |
Tracker.extend({ | |
pageView: function(url){ | |
_gaq.push(['_trackPageview', url]); | |
}, | |
track: function(category, name, comment){ | |
_gaq.push(['_trackEvent', category, name, comment || '']); | |
} | |
}); | |
new Tracker(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment