Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Keen.io with CoffeeScript doesn't work?

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?

like image 207
pfrank Avatar asked Jun 26 '26 23:06

pfrank


1 Answers

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.

like image 66
Josh Dzielak Avatar answered Jun 28 '26 13:06

Josh Dzielak