-_ = require 'underscore'
-_s = require 'underscore.string'
-sd = require('sharify').data
-qs = require('querystring')
+{ extend, reduce } = require 'underscore'
+{ capitalize } = require 'underscore.string'
+{ NODE_ENV, CURRENT_USER, GOOGLE_ANALYTICS_ID } = require('sharify').data
 
 module.exports = (options) =>
   return if module.exports.getUserAgent()?.indexOf?('PhantomJS') > -1
 
   { @ga, @location } = options
+
   @location ?= window?.location
-  if sd.GOOGLE_ANALYTICS_ID
+
+  if GOOGLE_ANALYTICS_ID
     googleAnalyticsParams = cookieDomain: 'are.na'
 
-    if sd.CURRENT_USER?.id
-      googleAnalyticsParams.userId = sd.CURRENT_USER?.id
+    if CURRENT_USER?.id
+      googleAnalyticsParams.userId = CURRENT_USER?.id
 
-    @ga? 'create', sd.GOOGLE_ANALYTICS_ID, googleAnalyticsParams
+    @ga? 'create', GOOGLE_ANALYTICS_ID, googleAnalyticsParams
 
 module.exports.getUserAgent = ->
   window?.navigator?.userAgent
 
 module.exports.trackPageview = (args = {}) =>
-  opts = _.extend { hitType: 'pageview' }, args
+  opts = extend { hitType: 'pageview' }, args
   ga? 'send', opts
 
 module.exports.trackOutboundLink = (url, cb = $.noop) =>

  other: 'Other Events'

module.exports.track = track =
  _.reduce(Object.keys(categories), (memo, kind) ->
    memo[kind] = (description, options = {}) ->
      # Send google analytics event
      ga? 'send', {

    'hitCallback': cb

module.exports.registerCurrentUser = ->
  userType = if CURRENT_USER then "Logged In" else "Logged Out"
  ga?('set', 'dimension1', userType)
  ga?('set', 'dimension2', CURRENT_USER?.registered) if CURRENT_USER

module.exports.modelNameAndIdToLabel = (modelName, id) ->
  throw new Error('Requires modelName and id') unless modelName? and id?

  "#{capitalize modelName}:#{id}"

categories =
  impression: 'Impressions'
