Skip to content

Instantly share code, notes, and snippets.

@hugooliveirad
Created January 27, 2014 23:14
Show Gist options
  • Save hugooliveirad/8659318 to your computer and use it in GitHub Desktop.
Save hugooliveirad/8659318 to your computer and use it in GitHub Desktop.
Tracker
Tracker =
###
Sends Analytics events
@param {String}
@param {String}
@param {String}
@returns {Tracker}
###
trackEvent: (category, action, label) ->
if typeof ga != "undefined"
ga('send', 'event', category, action, label)
else
console.log("Track Event -- Google Analytics -- #{new Date()}\n"+
"\tCategory: #{category}\n"+
"\tAction: #{action}\n"+
"\tLabel: #{label}")
@
###
Sends Analytics pageviews
@param {String}
@returns {Tracker}
###
trackPageview: (url) ->
# production
if typeof ga != "undefined"
ga('send', 'pageview')
# dev
else
console.log("Track Pageview -- Google Analytics -- #{new Date()}\n"+
"\tURL: #{url}")
@
###
Sends Social events
@param {String}
@param {String}
@param {String}
@param {String}
@returns {Tracker}
###
trackSocial: (network, action, target, page) ->
# production
if typeof ga != "undefined"
ga('send', 'social', network, action, target, page)
# dev
else
console.log("Track Social -- Google Analytics -- #{new Date()}\n"+
"\tNetwork: #{network}\n"+
"\tAction: #{action}\n"+
"\tTarget: #{target}\n"+
"\tPage: #{JSON.stringify(page)}")
@
###
Subscribes to Facebook and Twitter events
@returns {Tracker}
###
analytics: ->
if FB?
FB.Event.subscribe('edge.create', (targetUrl) ->
Tracker.trackSocial('Facebook', 'like', targetUrl)
)
FB.Event.subscribe('comment.create', (e) ->
Tracker.trackSocial('Facebook', 'comment', e.href)
)
if twttr?
twttr.ready( (twttr) ->
twttr.events.bind('tweet', (e) ->
Tracker.trackSocial('Twitter', 'tweet', window.location.href)
)
)
@
# if you want to subscribe to social events as soon as page loads, uncomment
# window.addEventListener('load', Tracker.analytics)
# Global object
window.Tracker = Tracker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment