Last active
January 16, 2017 11:35
-
-
Save rezwyi/3919572 to your computer and use it in GitHub Desktop.
Google Analytics script in CoffeeScript
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
# 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() |
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.
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
Very helpful, thanks! ^_^