A canjs Control that setup Google Analytics (injects JS) and can automatically:
- track hashChange (on or off) as pageView
- track any can.Model / Observe / Model.List / Observe.List events as Google Analytics events
Per observe Class, you can pass the following configuration attribute:
- construct: the Constructor function (type "Function", mandatory)
- listenTo: an array of string event (type Array, mandatory)
- trackingCategory: a string to (type String, optional - default: model.shortName) (@todo: rename to trackingCategory)
- labelAttribute: an attribute name (type string, optional - default: 'id')
The following value will be sent to Google:
- category: trackingCategory or class.shortName or '<unknown_classname>'
- action: the event name
- label: instance.attr(labelAttribute) or instance.attr('id')
An sample is better than 1 thousand words:
require(
...
, 'can-ga'
, 'path/to/model1',
, 'path/to/model2'
...
, function(..., Analytics, ModelClass1, ModelClass2...) {
new Analytics(
{
// options
trackHashChange: true, // boolean
classes: [{ // @todo: rename 'model(s)' because it works with any Observe
class: ModelClass1,
// see http://donejs.com/docs.html#!can.Model and http://donejs.com/docs.html#!can.Observe for default events
// it can also track specific attribute changes
// for ex., assuming that ModelClass1 instance myModelInstance1 have a 'name' attribute
// the following options will track every successful call to:
// - myModelInstance1.save() on new instance ('created' event)
// - myModelInstance1.save() on existing instance ('updated' event)
// - myModelInstance1.destroy() ('destroyed' event)
// - myModelInstance1.attr('name', newValue) ('name' event)
// - can.trigger(myModelInstance1, 'customEvent') ('customEvent' event)
listenTo: ['created', 'updated', 'deleted', 'name', 'customEvent']
}]
}
)
});
)
(maybe) No need to have a Control, it can be a Construct... Pros and cons:
- Pro control: we can add an 'prefix' in option and have multiple analytics trackingID associated to different controls
- is previous argument really an argument??