Skip to content

Instantly share code, notes, and snippets.

@rezwyi
Last active January 16, 2017 11:35
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rezwyi/3919572 to your computer and use it in GitHub Desktop.
Save rezwyi/3919572 to your computer and use it in GitHub Desktop.
Google Analytics script in CoffeeScript
# See http://stackoverflow.com/questions/4214731/coffeescript-global-variables
root = exports ? this
root._gaq = [['_setAccount', 'UA-xxxxxxxx-y'], ['_trackPageview']]
insertGAScript = ->
ga = document.createElement 'script'
ga.type = 'text/javascript'
ga.async = true
proto = document.location.protocol
proto = if (proto is 'https:') then 'https://ssl' else 'http://www'
ga.src = "#{proto}.google-analytics.com/ga.js"
s = document.getElementsByTagName('script')[0]
s[0].parentNode.insertBefore ga, s
insertGAScript()
@aarongray
Copy link

Very helpful, thanks! ^_^

@leonardteo
Copy link

Any idea why this is failing in Firefox? It is failing on this line:

s[0].parentNode.insertBefore ga, s

Error:

TypeError: Argument 2 of Node.insertBefore does not implement interface Node.

@joneszach
Copy link

Hi leonardteo. I ran into this same issue, and I fixed it!

Change

s = document.getElementsByTagName 'script'
s[0].parentNode.insertBefore ga, s

To

s = document.getElementsByTagName('script')[0]
s.parentNode.insertBefore ga, s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment