codecombat/app/lib/Tracker.coffee

65 lines
2.6 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
{me} = require 'lib/auth'
debugAnalytics = false
2014-01-03 13:32:13 -05:00
module.exports = class Tracker
constructor: ->
if window.tracker
2014-06-30 22:16:26 -04:00
console.error 'Overwrote our Tracker!', window.tracker
2014-01-03 13:32:13 -05:00
window.tracker = @
2014-06-30 22:16:26 -04:00
@isProduction = document.location.href.search('codecombat.com') isnt -1
2014-01-03 13:32:13 -05:00
@identify()
@updateOlark()
identify: (traits) ->
2014-06-30 22:16:26 -04:00
console.log 'Would identify', traits if debugAnalytics
2014-01-03 13:32:13 -05:00
return unless me and @isProduction and analytics?
# https://segment.io/docs/methods/identify
traits ?= {}
for userTrait in ['email', 'anonymous', 'dateCreated', 'name', 'wizardColor1', 'testGroupNumber', 'gender']
traits[userTrait] ?= me.get(userTrait)
analytics.identify me.id, traits
updateOlark: ->
return unless me and olark?
olark 'api.chat.updateVisitorStatus', snippet: ["User ID: #{me.id}"]
2014-06-30 22:16:26 -04:00
return if me.get('anonymous')
olark 'api.visitor.updateEmailAddress', emailAddress: me.get('email') if me.get('email')
2014-01-03 13:32:13 -05:00
olark 'api.chat.updateVisitorNickname', snippet: me.displayName()
2014-01-03 13:32:13 -05:00
updatePlayState: (level, session) ->
2014-02-06 14:16:57 -05:00
return unless olark?
2014-01-03 13:32:13 -05:00
link = "codecombat.com/play/level/#{level.get('slug') or level.id}?session=#{session.id}"
snippet = [
"#{link}"
"User ID: #{me.id}"
"Session ID: #{session.id}"
"Level: #{level.get('name')}"
]
olark 'api.chat.updateVisitorStatus', snippet: snippet
2014-01-03 13:32:13 -05:00
trackPageView: ->
return unless @isProduction and analytics?
url = Backbone.history.getFragment()
2014-06-30 22:16:26 -04:00
console.log 'Going to track visit for', "/#{url}" if debugAnalytics
2014-01-03 13:32:13 -05:00
analytics.pageview "/#{url}"
trackEvent: (event, properties, includeProviders=null) =>
2014-06-30 22:16:26 -04:00
console.log 'Would track analytics event:', event, properties if debugAnalytics
2014-01-03 13:32:13 -05:00
return unless me and @isProduction and analytics?
2014-06-30 22:16:26 -04:00
console.log 'Going to track analytics event:', event, properties if debugAnalytics
2014-01-03 13:32:13 -05:00
properties = properties or {}
context = {}
if includeProviders
context.providers = {'All': false}
for provider in includeProviders
context.providers[provider] = true
event.label = properties.label if properties.label
analytics?.track event, properties, context
trackTiming: (duration, category, variable, label, samplePercentage=5) ->
# https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingTiming
return console.warn "Duration #{duration} invalid for trackTiming call." unless duration >= 0 and duration < 60 * 60 * 1000
2014-06-30 22:16:26 -04:00
console.log 'Would track timing event:', arguments if debugAnalytics
window._gaq?.push ['_trackTiming', category, variable, duration, label, samplePercentage]