I'm using the backbone route filter, https://github.com/fantactuka/backbone-route-filter, in my backbone.js app. It's a one page app, so I was trying to record pageviews using a KissMetrics event tracking snippet. Here is the code:
before: {
'*any': function(fragment, args) {
}
},
after: {
'*any': function(fragment) {
var _kmq = window._kmq || [];
_kmq.push(['record', 'Viewed ' + fragment]);
}
},
The question is, the event wasn't tracking unless I specified the 'window' scope of the _kmq variable. Why? In my index.html, or the one page with all my js code, I have:
var _kmq = _kmq || [];
which I thought would make the variable at the global level automatically...this is the link to a typical implementation: http://support.kissmetrics.com/apis/javascript/index.html In every case I've seen prior the common api method worked, without setting the scope to window: http://support.kissmetrics.com/apis/javascript/javascript-specific/index.html
Why did I need to specify 'window._kmq' rather than just '_kmq'?