<commit_before>{ 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 GOOGLE_ANALYTICS_ID
    googleAnalyticsParams = cookieDomain: 'are.na'

    if CURRENT_USER?.id
      googleAnalyticsParams.userId = CURRENT_USER?.id

    @ga? 'create', GOOGLE_ANALYTICS_ID, googleAnalyticsParams

module.exports.getUserAgent = ->
  window?.navigator?.userAgent

module.exports.trackPageview = (args = {}) =>
  opts = extend { hitType: 'pageview' }, args
  ga? 'send', opts

module.exports.trackOutboundLink = (url, cb = $.noop) =>
  ga? 'send', 'event', 'Outbound link', 'click', url,
    'transport': 'beacon'
    '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'
  hover: 'UI Interactions'
  click: 'UI Interactions'
  submit: 'UI Interactions'
  funnel: 'Funnel Progressions'
  segment: 'UI A/B Test Segments'
  error: 'UI Errors'
  multi: 'Multi-object Events'
  timing: 'Timing'
  other: 'Other Events'

module.exports.track = track =
  reduce(Object.keys(categories), (memo, kind) ->
    memo[kind] = (description, options = {}) ->
      # Send google analytics event
      ga? 'send', {
        hitType: 'event'
        eventCategory: options.category || 'UI Interactions'
        eventAction: description
        eventLabel: options.label
        eventValue: options.value
        nonInteraction: (if options.category in ['Funnel Progressions', 'Impressions', 'Timing'] then 1 else 0)
      }
    memo
  , {})

module.exports.exception = (errorObj) ->
  @ga? 'send', 'exception', errorObj

# These need to be set up individually before using. Read this non-sense:
# https://developers.google.com/analytics/devguides/platform/customdimsmets
module.exports.setDimension = (index, value) ->
  @ga? 'set', index, value<commit_msg>Improves development/debugging experience for analytics<commit_after>{ 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 GOOGLE_ANALYTICS_ID
    googleAnalyticsParams = cookieDomain: 'are.na'

    if CURRENT_USER?.id
      googleAnalyticsParams.userId = CURRENT_USER?.id

    @ga? 'create', GOOGLE_ANALYTICS_ID, googleAnalyticsParams

module.exports.getUserAgent = ->
  window?.navigator?.userAgent

module.exports.trackPageview = (args = {}) =>
  opts = extend { hitType: 'pageview' }, args
  ga? 'send', opts

module.exports.trackOutboundLink = (url, cb = $.noop) =>
  ga? 'send', 'event', 'Outbound link', 'click', url,
    'transport': 'beacon'
    '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'
  hover: 'UI Interactions'
  click: 'UI Interactions'
  submit: 'UI Interactions'
  funnel: 'Funnel Progressions'
  segment: 'UI A/B Test Segments'
  error: 'UI Errors'
  multi: 'Multi-object Events'
  timing: 'Timing'
  other: 'Other Events'

module.exports.track = track =
  reduce Object.keys(categories), (memo, kind) ->
    memo[kind] = (description, options = {}) ->
      if NODE_ENV is 'development'
        console.info "analytics.#{kind}: #{description}", options

      else
        ga? 'send',
          hitType: 'event'
          eventCategory: options.category or 'UI Interactions'
          eventAction: description
          eventLabel: options.label
          eventValue: options.value
          nonInteraction: if options.category in ['Funnel Progressions', 'Impressions', 'Timing'] then 1 else 0

    memo
  , {}

module.exports.exception = (errorObj) ->
  @ga? 'send', 'exception', errorObj

# These need to be set up individually before using. Read this non-sense:
# https://developers.google.com/analytics/devguides/platform/customdimsmets
module.exports.setDimension = (index, value) ->
  @ga? 'set', index, value
{'fraction_non_alphanumeric': 0.13524004085801838, 'fraction_numerical': 0.0020429009193054137, 'mean_word_length': 4.011258955987717, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6233173', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1555, 'n_words': 394}