Created
August 18, 2012 11:52
-
-
Save mfo/3386361 to your computer and use it in GitHub Desktop.
Async GA trackr for 3rd party Analytics
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
# 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 | |
### | |
(-> | |
sharypic.ga = document.createElement('script') | |
sharypic.ga.type = 'text/javascript' | |
sharypic.ga.async = true | |
sharypic.ga.src = "#{(if 'https:' == document.location.protocol then 'https://ssl' else 'http://www')}.google-analytics.com/ga.js" | |
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(sharypic.ga); | |
)(); | |
### | |
@desc trigger an event [categorized as widget], for more info about google events, see: | |
https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide | |
@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]); | |
)(window.sharypic.$) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment