<commit_before>bongo    = require 'bongo'

{ secure, signature } = bongo

{ argv } = require 'optimist'

_ = require 'lodash'

KONFIG        = require('koding-config-manager').load("main.#{argv.c}")
{ socialapi } = KONFIG
exchangeName  = "#{socialapi.eventExchangeName}:0"
exchangeOpts  = { autoDelete: no, durable:yes, type :'fanout' }

Analytics = require('analytics-node')
analytics = new Analytics(KONFIG.segment)

mqClient = null

module.exports = class Tracker extends bongo.Base

  @share()

  @set
    sharedMethods:
      static:
        track: (signature String, Object, Function)

  KodingError = require '../error'

  { forcedRecipient, defaultFromMail } = KONFIG.email

  EVENT_TYPE = 'api.mail_send'

  @types =
    START_REGISTER       : 'started to register'
    FINISH_REGISTER      : 'finished register'
    LOGGED_IN            : 'logged in'
    CONFIRM_USING_TOKEN  : 'confirmed & logged in using token'
    REQUEST_NEW_PASSWORD : '<PASSWORD>'
    CHANGED_PASSWORD     : '<PASSWORD>'
    REQUEST_EMAIL_CHANGE : 'requested pin to change email'
    CHANGED_EMAIL        : 'changed their email'
    INVITED_GROUP        : 'was invited to a group'
    INVITED_CREATE_TEAM  : 'was invited to create a team'
    SENT_FEEDBACK        : 'sent feedback'

  @properties = {}

  @properties[@types.FINISH_REGISTER] = {
    category: 'NewAccount', label: 'VerifyAccount'
  }

  @identifyAndTrack = (username, event, eventProperties = {}) ->
    @identify username
    @track username, event, eventProperties


  @identify = (username, traits = {}) ->
    # use `forcedRecipient` for both username and email
    if forcedRecipient
      username     = forcedRecipient
      traits.email = forcedRecipient

    traits = @addDefaults traits
    analytics.identify { userId: username, traits }

    # force flush so identify call doesn't sit in queue, while events
    # from Go/other systems are being sent
    analytics.flush (err, batch) ->
      console.error "flushing identify failed: #{err} @sent-hil"  if err


  @track$ = secure (client, subject, options = {}, callback) ->

    { profile: { nickname } } = client.connection.delegate

    event = { subject }

    @track nickname, event, options

    callback()


  @track = (username, event, options = {}) ->
    _.extend options, @properties[event.subject]

    # use `forcedRecipient` for both username and email
    if forcedRecipient
      username = forcedRecipient
      event.to  = forcedRecipient

    event.from       or= defaultFromMail
    event.properties   = @addDefaults { options, username }

    unless mqClient
      return console.error 'RabbitMQ client not found for class `Tracker` @sent-hil'
<commit_msg>Disable tracker module on configuration option<commit_after>bongo    = require 'bongo'

{ secure, signature } = bongo

{ argv } = require 'optimist'

_ = require 'lodash'

KONFIG        = require('koding-config-manager').load("main.#{argv.c}")
{ socialapi } = KONFIG
exchangeName  = "#{socialapi.eventExchangeName}:0"
exchangeOpts  = { autoDelete: no, durable:yes, type :'fanout' }

Analytics = require('analytics-node')
analytics = new Analytics(KONFIG.segment)

mqClient = null

module.exports = class Tracker extends bongo.Base

  @share()

  @set
    sharedMethods:
      static:
        track: (signature String, Object, Function)

  KodingError = require '../error'

  { forcedRecipient, defaultFromMail } = KONFIG.email

  EVENT_TYPE = 'api.mail_send'

  @types =
    START_REGISTER       : 'started to register'
    FINISH_REGISTER      : 'finished register'
    LOGGED_IN            : 'logged in'
    CONFIRM_USING_TOKEN  : 'confirmed & logged in using token'
    REQUEST_NEW_PASSWORD : '<PASSWORD>'
    CHANGED_PASSWORD     : '<PASSWORD>'
    REQUEST_EMAIL_CHANGE : 'requested pin to change email'
    CHANGED_EMAIL        : 'changed their email'
    INVITED_GROUP        : 'was invited to a group'
    INVITED_CREATE_TEAM  : 'was invited to create a team'
    SENT_FEEDBACK        : 'sent feedback'

  @properties = {}

  @properties[@types.FINISH_REGISTER] = {
    category: 'NewAccount', label: 'VerifyAccount'
  }

  @identifyAndTrack = (username, event, eventProperties = {}) ->
    @identify username
    @track username, event, eventProperties


  @identify = (username, traits = {}) ->
    return  unless KONFIG.sendEventsToSegment

    # use `forcedRecipient` for both username and email
    if forcedRecipient
      username     = forcedRecipient
      traits.email = forcedRecipient

    traits = @addDefaults traits
    analytics.identify { userId: username, traits }

    # force flush so identify call doesn't sit in queue, while events
    # from Go/other systems are being sent
    analytics.flush (err, batch) ->
      console.error "flushing identify failed: #{err} @sent-hil"  if err


  @track$ = secure (client, subject, options = {}, callback) ->

    { profile: { nickname } } = client.connection.delegate

    event = { subject }

    @track nickname, event, options

    callback()


  @track = (username, event, options = {}) ->
    return  unless KONFIG.sendEventsToSegment

    _.extend options, @properties[event.subject]

    # use `forcedRecipient` for both username and email
    if forcedRecipient
      username = forcedRecipient
      event.to  = forcedRecipient

    event.from       or= defaultFromMail
    event.properties   = @addDefaults { options, username }

    unless mqClient
      return console.error 'RabbitMQ client not found for class `Tracker` @sent-hil'

{'fraction_non_alphanumeric': 0.10208900999091734, 'fraction_numerical': 0.00036330608537693006, 'mean_word_length': 3.152337858220211, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '5388195', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1627, 'n_words': 453}