Created
August 19, 2010 05:37
-
-
Save choonkeat/537133 to your computer and use it in GitHub Desktop.
Paste at the bottom of a webpage
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
| // See: http://gist.github.com/537133 | |
| // standard script to track clicks on document elements, and send google analytic events | |
| // identifying which element was clicked (classname, id, href, src, csspath) | |
| // if "cssprefix_optional" is null, identify element as either using classname, id, href, src, csspath (order of preference) | |
| // if "pagename_optional" is null, identify "event category" as page filename | |
| (function(jQuery, pageTracker, cssselector, cssprefix_optional, pagename_optional) { | |
| if (! jQuery) return; | |
| try { | |
| var baseurl = window.location.href.split(/[?#;]/)[0]; | |
| var pathsegments = baseurl.split('/'); | |
| var filename = unescape(pathsegments.pop() || pathsegments.pop()); | |
| if (! pagename_optional) pagename_optional = filename; | |
| var basepath = pathsegments.join('/') + '/'; | |
| function log() { window.console && window.console.log && console.log(arguments); } | |
| function element2identifier(element) { | |
| var match = cssprefix_optional && (element.className || "").match('\\b' + cssprefix_optional + '-(\\w+)\\b'); | |
| return (match && match[1]) || (element.id && ('#' + element.id)) || (element.className && ('.' + element.className.replace(/\s+.*$/, ''))) || (element.href || element.src || "").replace(baseurl, '').replace(basepath, ''); | |
| } | |
| var trackEvent = | |
| (pageTracker && function(category, action, opt_label, opt_value) { log(arguments); pageTracker._trackEvent( category, action, opt_label, opt_value ); }) || | |
| (window._gaq && function(category, action, opt_label, opt_value) { log(arguments); _gaq.push(['_trackEvent', category, action, opt_label, opt_value]); }) || | |
| log; | |
| trackEvent(pagename_optional, 'pageview'); | |
| jQuery(document.body).ready(function() { | |
| jQuery(cssselector).click(function(evt) { | |
| try { | |
| var identifier = element2identifier(this); | |
| if (!identifier) { | |
| identifier = this.tagName; | |
| jQuery(this).parents().each(function(index, parent) { | |
| if (parent.className) identifier = '.' + parent.className.replace(/\s+.*$/, '') + ' ' + identifier; | |
| if (parent.id) identifier = '#' + parent.id + (parent.className ? '' : ' ') + identifier; | |
| identifier = parent.tagName + ((parent.id || parent.className) ? '' : ':nth(' + jQuery(parent).prevAll().length + ') ') + identifier; | |
| }); | |
| } | |
| trackEvent(pagename_optional, identifier.toLowerCase()); | |
| } catch (e) { | |
| log(e); | |
| } | |
| }); | |
| }); | |
| } catch (e) { | |
| log(e); | |
| } | |
| })(window.jQuery, window.pageTracker, 'a,input,textarea,button'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment