Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2012 11:52
Show Gist options
  • Save mfo/3386361 to your computer and use it in GitHub Desktop.
Save mfo/3386361 to your computer and use it in GitHub Desktop.
Async GA trackr for 3rd party Analytics
# it's an IIFE (Immediately-Invoked Function Expression) expeting jQuery as argument
(($) ->
# by default, i'm expecting window.sharypic declared as a namespace to declare a new class
class window.sharypic.WidgetTrackr
# should be called one time, and inserting / creating [or not], a new GA trackr
constructor: -> @insert()
# insert the google tracking code inside the webpage
insert: ->
window._gaq = window._gaq || [];
_gaq.push -> _gat._createTracker sharypic.gaProfileId, 'sharypicWidgetTracker' # create a new GA trackr, for the 3rd party widget
# setup some conf stuffs
_gaq.push "sharypicWidgetTracker._setDomainName", 'none'
_gaq.push "sharypicWidgetTracker._setAllowLinker", true
_gaq.push "sharypicWidgetTracker._trackPageview"
if GA is included, don't care, i'll create a new HTTP request, but HTTP cache will say OK in case it's already required
(-> = document.createElement('script') = 'text/javascript' = true = "#{(if 'https:' == document.location.protocol then 'https://ssl' else 'http://www')}"
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(;
@desc trigger an event [categorized as widget], for more info about google events, see:
@param the name of the action
@param action String, the kind of action user done
@param label String, additional dimension
track: (category, action, opt_label) ->
window._gaq.push(['sharypicWidgetTracker._trackEvent', category, action, opt_label]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment