2014-11-28 20:49:41 -05:00
|
|
|
{me} = require 'core/auth'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-04-13 23:31:23 -04:00
|
|
|
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()
|
|
|
|
|
|
|
|
identify: (traits) ->
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'Would identify', traits if debugAnalytics
|
2014-11-27 23:55:28 -05:00
|
|
|
return unless me and @isProduction and analytics? and not me.isAdmin()
|
2014-01-03 13:32:13 -05:00
|
|
|
# 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
|
|
|
|
|
|
|
|
trackPageView: ->
|
2014-11-27 23:55:28 -05:00
|
|
|
return unless @isProduction and analytics? and not me.isAdmin()
|
2014-01-03 13:32:13 -05:00
|
|
|
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}"
|
|
|
|
|
2014-11-30 01:16:18 -05:00
|
|
|
trackEvent: (action, properties, includeIntegrations=null) =>
|
2014-11-28 15:05:34 -05:00
|
|
|
# 'action' is a string
|
|
|
|
# Google Analytics properties format: {category: 'Account', label: 'Premium', value: 50 }
|
|
|
|
# https://segment.com/docs/integrations/google-analytics/#track
|
|
|
|
# https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#Anatomy
|
|
|
|
# Mixpanel properties format: whatever you want unlike GA
|
|
|
|
# https://segment.com/docs/integrations/mixpanel/
|
|
|
|
console.log 'Would track analytics event:', action, properties if debugAnalytics
|
2014-11-27 23:55:28 -05:00
|
|
|
return unless me and @isProduction and analytics? and not me.isAdmin()
|
2014-11-28 15:05:34 -05:00
|
|
|
console.log 'Going to track analytics event:', action, properties if debugAnalytics
|
2014-01-03 13:32:13 -05:00
|
|
|
properties = properties or {}
|
|
|
|
context = {}
|
2014-11-30 01:16:18 -05:00
|
|
|
if includeIntegrations
|
|
|
|
# https://segment.com/docs/libraries/analytics.js/#selecting-integrations
|
|
|
|
context.integrations = {'All': false}
|
|
|
|
for integration in includeIntegrations
|
|
|
|
context.integrations[integration] = true
|
2014-11-28 15:05:34 -05:00
|
|
|
analytics?.track action, properties, context
|
2014-04-13 23:31:23 -04:00
|
|
|
|
|
|
|
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
|
2014-04-13 23:31:23 -04:00
|
|
|
window._gaq?.push ['_trackTiming', category, variable, duration, label, samplePercentage]
|