Skip to content

Instantly share code, notes, and snippets.

@danethurber
Created December 5, 2014 01:51
Show Gist options
  • Save danethurber/ddec42697dd8867c25ca to your computer and use it in GitHub Desktop.
Save danethurber/ddec42697dd8867c25ca to your computer and use it in GitHub Desktop.
Angular event tracking
angular.module('tracker', [])
angular.module('tracker').factory 'Mixpanel', () ->
class Mixpanel
specialProperties: ['first_name', 'last_name', 'name', 'email', 'created']
isAvailable: () ->
window.mixpanel?
identify: (userId, traits={}) ->
if @isAvailable()
mixpanel.identify userId
mixpanel.people.set @transformTraits(traits)
track: (eventName, params={}) ->
mixpanel.track(eventName, params) if @isAvailable()
transformTraits: (traits) ->
out = angular.copy traits
for key, val of traits
if key in @specialProperties
delete out[key]
out["$#{key}"] = val
out
angular.module('tracker').factory 'GoogleAnalytics', () ->
class GoogleAnalytics
isAvailable: () ->
window._gaq?
track: (eventName, params={})->
if @isAvailable()
_gaq.push([ '_trackEvent', params.category || 'All', eventName ])
pageview: (url) ->
if @isAvailable()
window._gaq.push ['_trackPageview', url]
angular.module('tracker').factory 'Woopra', () ->
class Woopra
isAvailable: () ->
window.woopra?
identify: (userId, traits={}) ->
woopra.identify(traits).push() if @isAvailable()
track: (eventName, params={}) ->
woopra.track(eventName, params) if @isAvailable()
pageview: (url) ->
if window.woopra?
woopra.track('pv', { url: url, title: document.title })
angular.module('tracker').factory 'Intercom', (IntercomConfig) ->
class Intercom
isAvailable: () ->
window.Intercom?
identify: (userId, traits={}) ->
if @isAvailable()
window.Intercom 'boot', @buildTraits(traits)
pageview: (url) ->
@update()
update: () ->
if @isAvailable()
window.Intercom 'update'
buildTraits: (traits) ->
app_id: IntercomConfig.app_id
name: traits.name
email: traits.email
created_at: traits.created
company:
id: traits.company.id
name: traits.company.name
angular.module('tracker').factory 'tracker',
($log, Mixpanel, GA, Woopra, Intercom) ->
providers = [new Mixpanel, new GoogleAnalytics, new Woopra, new Intercom]
identify: (userId, traits) ->
$log.info "identify: #{userId}"
for p in providers
p.identify(userId, traits) if angular.isFunction p.identify
track: (eventName, params) ->
$log.info "track: #{eventName}"
for p in providers
p.track(eventName, params) if angular.isFunction p.track
pageview: (url) ->
$log.info "pageview: #{url}"
for p in providers
p.pageview(url) if angular.isFunction p.pageview
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment