ACCOUNT = 'UA-28812528-1'Template
(c) 2014 Alasdair Mercer
Freely distributable under the MIT license:
http://template-extension.org/license
Code for Templates analytics account.
ACCOUNT = 'UA-28812528-1'Source URL of the analytics script.
SOURCE = 'https://ssl.google-analytics.com/ga.js'
analytics = window.analytics = new class Analytics extends utils.ClassAdd analytics to the current page.
add: ->Setup tracking details for analytics.
_gaq = window._gaq ?= []
_gaq.push ['_setAccount', ACCOUNT]
_gaq.push ['_trackPageview']Inject script to capture analytics.
ga = document.createElement 'script'
ga.async = 'async'
ga.src = SOURCE
script = document.getElementsByTagName('script')[0]
script.parentNode.insertBefore ga, scriptDetermine whether or not analytics are enabled.
enabled: ->
not store? or store.get 'analytics'Remove analytics from the current page.
remove: ->Delete scripts used to capture analytics.
for script in document.querySelectorAll "script[src='#{SOURCE}']"
script.parentNode.removeChild scriptRemove tracking details for analytics.
delete window._gaqCreate an event with the information provided and track it in analytics.
track: (category, action, label, value, nonInteraction) ->
return unless @enabled()
event = ['_trackEvent']Add the required information.
event.push category
event.push actionAdd the optional information where possible.
event.push label if label?
event.push value if value?
event.push nonInteraction if nonInteraction?Add the event to analytics.
_gaq = window._gaq ?= []
_gaq.push eventInitialize analytics.
store?.init 'analytics', yes