I'm trying to use Keen.io, I converted their JS to coffee as follows:
# Keen init
Keen = Keen or
configure: (e) ->
@_cf = e
addEvent: (e, t, n, i) ->
@_eq = @_eq or []
@_eq.push([e, t, n, i])
setGlobalProperties: (e) ->
@_gp = e
onChartsReady: (e) ->
@_ocrq = @_ocrq or []
@_ocrq.push(e)
(->
e = document.createElement("script")
e.type = "text/javascript"
e.async = not 0
e.src = ((if "https:" is document.location.protocol then "https://" else "http://")) + "dc8na2hxrj29i.cloudfront.net/code/keen-2.1.0-min.js"
t = document.getElementsByTagName("script")[0]
t.parentNode.insertBefore e, t
)()
Keen.configure myParams
Keen.addEvent "script_tag_init"
But looks like events aren't hitting. What gives?
Yeah, that would be the problem. The Keen object won't be visible to the global scope due to how the CoffeeScript will compile.
"Exporting" Keen to window after initializing will work.
Alternatively you can initialize Keen directly on the window object:
# Keen init
window.Keen =
configure: (e) ->
@_cf = e
...
Note: This method does exclude checking if Keen already exists on the page first, which is a corner-case performance optimization and isn't necessary for most applications. In other words it should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With