<commit_before>_ = require 'underscore'
Reflux = require 'reflux'
Mixpanel = require 'mixpanel'

Actions = require '../actions'
AccountStore = require './account-store'

printToConsole = false

module.exports =
AnalyticsStore = Reflux.createStore
  init: ->
    @listenAndTrack = (dispatcher=Actions) => (callback, action) =>
      @listenTo dispatcher[action], (args...) =>
        @track(action, callback(args...))

    @analytics = Mixpanel.init("625e2300ef07cb4eb70a69b3638ca579")
    @listenTo AccountStore, => @identify()
    @identify()

    @_listenToCoreActions()

    @_setupGlobalPackageActions()

  addPackageActions: (listeners, dispatcher=Actions) ->
    _.each listeners, @listenAndTrack(dispatcher)

  addGlobalPackageActions: (listeners) ->
    @_globalPackageActions = _.extend @_globalPackageActions, listeners

  _setupGlobalPackageActions: ->
    @_globalPackageActions = {}
    @listenTo Actions.sendToAllWindows, (actionData={}) =>
      return unless atom.isMainWindow()
      callback = @_globalPackageActions[actionData.action]
      if callback?
        @track(actionData.action, callback(actionData))

  # We white list actions to track.
  #
  # The Key is the action and the value is the callback function for that
  # action. That callback function should return the data we pass along to
  # our analytics service based on the sending data.
  #
  # IMPORTANT: Be VERY careful about what private data we send to our
  # analytics service!!
  #
  # Only completely anonymous data essential to future metrics or
  # debugging may be sent.
  coreWindowActions: ->
    showDeveloperConsole: -> {}
    composeReply: ({threadId, messageId}) -> {threadId, messageId}
    composeForward: ({threadId, messageId}) -> {threadId, messageId}
    composeReplyAll: ({threadId, messageId}) -> {threadId, messageId}
    composePopoutDraft: (draftLocalId) -> {draftLocalId: draftLocalId}
    composeNewBlankDraft: -> {}
    sendDraft: (draftLocalId) -> {draftLocalId: draftLocalId}
    destroyDraft: (draftLocalId) -> {draftLocalId: draftLocalId}
    searchQueryCommitted: (query) -> {}
    fetchAndOpenFile: -> {}
    fetchAndSaveFile: -> {}
    abortFetchFile: -> {}
    fileDownloaded: -> {}

  coreGlobalActions: ->
    fileAborted: (uploadData={}) -> {fileSize: uploadData.fileSize}
    fileUploaded: (uploadData={}) -> {fileSize: uploadData.fileSize}
    sendDraftSuccess: ({draftLocalId}) -> {draftLocalId: draftLocalId}

  track: (action, data={}) ->
    _.defer =>
      # send to the analytics service
      @analytics.track(action, _.extend(data, accountId: AccountStore.current()?.id))

      # send to the logs that we ship to LogStash
      console.debug(printToConsole, {action, data})

  identify: ->
    account = AccountStore.current()
    if account
      @analytics.alias("distinct_id", account.id)
      @analytics.people.set account.id,<commit_msg>fix(mixpanel): Correctly associate events with distinct_ids<commit_after>_ = require 'underscore'
Reflux = require 'reflux'
Mixpanel = require 'mixpanel'

Actions = require '../actions'
AccountStore = require './account-store'

printToConsole = false

module.exports =
AnalyticsStore = Reflux.createStore
  init: ->
    @listenAndTrack = (dispatcher=Actions) => (callback, action) =>
      @listenTo dispatcher[action], (args...) =>
        @track(action, callback(args...))

    @analytics = Mixpanel.init("625e2300ef07cb4eb70a69b3638ca579")
    @listenTo AccountStore, => @identify()
    @identify()

    @_listenToCoreActions()

    @_setupGlobalPackageActions()

  addPackageActions: (listeners, dispatcher=Actions) ->
    _.each listeners, @listenAndTrack(dispatcher)

  addGlobalPackageActions: (listeners) ->
    @_globalPackageActions = _.extend @_globalPackageActions, listeners

  _setupGlobalPackageActions: ->
    @_globalPackageActions = {}
    @listenTo Actions.sendToAllWindows, (actionData={}) =>
      return unless atom.isMainWindow()
      callback = @_globalPackageActions[actionData.action]
      if callback?
        @track(actionData.action, callback(actionData))

  # We white list actions to track.
  #
  # The Key is the action and the value is the callback function for that
  # action. That callback function should return the data we pass along to
  # our analytics service based on the sending data.
  #
  # IMPORTANT: Be VERY careful about what private data we send to our
  # analytics service!!
  #
  # Only completely anonymous data essential to future metrics or
  # debugging may be sent.
  coreWindowActions: ->
    showDeveloperConsole: -> {}
    composeReply: ({threadId, messageId}) -> {threadId, messageId}
    composeForward: ({threadId, messageId}) -> {threadId, messageId}
    composeReplyAll: ({threadId, messageId}) -> {threadId, messageId}
    composePopoutDraft: (draftLocalId) -> {draftLocalId: draftLocalId}
    composeNewBlankDraft: -> {}
    sendDraft: (draftLocalId) -> {draftLocalId: draftLocalId}
    destroyDraft: (draftLocalId) -> {draftLocalId: draftLocalId}
    searchQueryCommitted: (query) -> {}
    fetchAndOpenFile: -> {}
    fetchAndSaveFile: -> {}
    abortFetchFile: -> {}
    fileDownloaded: -> {}

  coreGlobalActions: ->
    fileAborted: (uploadData={}) -> {fileSize: uploadData.fileSize}
    fileUploaded: (uploadData={}) -> {fileSize: uploadData.fileSize}
    sendDraftSuccess: ({draftLocalId}) -> {draftLocalId: draftLocalId}

  track: (action, data={}) ->
    _.defer =>
      # send to the analytics service
      @analytics.track(action, _.extend(data, {
        accountId: AccountStore.current()?.id
        distinct_id: AccountStore.current()?.id
      }))

      # send to the logs that we ship to LogStash
      console.debug(printToConsole, {action, data})

  identify: ->
    account = AccountStore.current()
    if account
      @analytics.alias("distinct_id", account.id)
      @analytics.people.set account.id,
{'fraction_non_alphanumeric': 0.12100068073519402, 'fraction_numerical': 0.007147719537100068, 'mean_word_length': 3.8812292358803986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6424312', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1721, 'n_words': 434}