<commit_before>import { partial } from 'lodash'
import { get } from 'lodash/fp'
import { mostRecentCommunity } from '../models/person'
import moment from 'moment-timezone'

// These strings were not used prior to hylo-redux
export const ADDED_BIO = 'Add bio'
export const ADDED_SKILL = 'Add skill'
export const ADDED_COMMUNITY = 'Add community'
export const EDITED_USER_SETTINGS = 'Edit user settings'
export const FOLLOWED_TOPIC = 'Follow topic'
export const INVITED_COMMUNITY_MEMBERS = 'Invited community members'
export const OPENED_POST_EDITOR = 'Open post editor'
export const SEARCHED = 'Search'
export const STARTED_MESSAGE = 'Messages: Start a message'
export const SENT_MESSAGE = 'Messages: Send a message'
export const VIEWED_MESSAGE_THREAD = 'Messages: View a thread'
export const VIEWED_MESSAGE_THREAD_LIST = 'Messages: View list of threads'

// These strings correspond to the names of events in Mixpanel with historical
// data, so they should be changed with care
export const ADDED_COMMENT = 'Post: Comment: Add'
export const ADDED_POST = 'Add Post'
export const CLICKTHROUGH = 'Clickthrough'
export const EDITED_POST = 'Edit Post'
export const LOGGED_IN = 'Login success'
export const SHOWED_POST_COMMENTS = 'Post: Comments: Show'
export const STARTED_LOGIN = 'Login start'
export const STARTED_SIGNUP = 'Signup start'
export const VIEWED_COMMUNITY = 'Community: Load Community'
export const VIEWED_NOTIFICATIONS = 'Notifications: View'
export const VIEWED_PERSON = 'Member Profiles: Loaded a profile'
export const VIEWED_SELF = 'Member Profiles: Loaded Own Profile'

export function trackEvent (eventName, options = {}) {
  const track = partial(window.analytics.track, eventName)

  // "context" here means "the context in which something happened", e.g.
  // context for STARTED_MESSAGE is "dropdown", "profile", etc.
  const { person, post, tag, community, context, ...otherOptions } = options

  // we're being explicit here about which properties are included with each
  // event, rather than sending everything, in order to conform to legacy data
  // and also to prevent accidental "namespace pollution" in Mixpanel
  switch (eventName) {
    case ADDED_COMMUNITY:
    case VIEWED_COMMUNITY:
    case INVITED_COMMUNITY_MEMBERS:
    case OPENED_POST_EDITOR:
      track({community: get('name', community)})
      break
    case ADDED_SKILL:
    case FOLLOWED_TOPIC:
      track({context, tag})
      break
    case ADDED_BIO:
    case STARTED_MESSAGE:
      track({context})
      break
    case VIEWED_PERSON:
      let { id, name } = person
      track({id, name})
      break
    case ADDED_COMMENT:
    case SHOWED_POST_COMMENTS:
      track({post_id: post.id})
      break
    case ADDED_POST:
    case EDITED_POST:
      track({post_tag: tag, community: community.name})
      break
    case CLICKTHROUGH:
    case SEARCHED:
      track({community, ...otherOptions})
      break
    case EDITED_USER_SETTINGS:
    case LOGGED_IN:
    case SENT_MESSAGE:
    case STARTED_LOGIN:
    case STARTED_SIGNUP:
    case VIEWED_MESSAGE_THREAD:
    case VIEWED_MESSAGE_THREAD_LIST:
    case VIEWED_NOTIFICATIONS:
    case VIEWED_SELF:
      track()
      break
    default:
      const message = `Don't know how to handle event named "${eventName}"`
      window.alert(message)
      throw new Error(message)
  }

  return Promise.resolve()
}

export const trackSearch = (term, type, community) => {
  trackEvent(SEARCHED, {community: get('name', community), term, type})
}

export const identify = person => {
  if (!person) return

  let { id, name, email, post_count, created_at } = person<commit_msg>be less restrictive about event properties<commit_after>import { partial } from 'lodash'
import { get } from 'lodash/fp'
import { mostRecentCommunity } from '../models/person'
import moment from 'moment-timezone'

// These strings were not used prior to hylo-redux
export const ADDED_BIO = 'Add bio'
export const ADDED_SKILL = 'Add skill'
export const ADDED_COMMUNITY = 'Add community'
export const EDITED_USER_SETTINGS = 'Edit user settings'
export const FOLLOWED_TOPIC = 'Follow topic'
export const LOGIN_ATTEMPTED = 'Login attempt'
export const LOGIN_FAILED = 'Login failure'
export const INVITED_COMMUNITY_MEMBERS = 'Invited community members'
export const OPENED_POST_EDITOR = 'Open post editor'
export const SEARCHED = 'Search'
export const STARTED_MESSAGE = 'Messages: Start a message'
export const SENT_MESSAGE = 'Messages: Send a message'
export const VIEWED_MESSAGE_THREAD = 'Messages: View a thread'
export const VIEWED_MESSAGE_THREAD_LIST = 'Messages: View list of threads'

// These strings correspond to the names of events in Mixpanel with historical
// data, so they should be changed with care
export const ADDED_COMMENT = 'Post: Comment: Add'
export const ADDED_POST = 'Add Post'
export const CLICKTHROUGH = 'Clickthrough'
export const EDITED_POST = 'Edit Post'
export const LOGGED_IN = 'Login success'
export const SHOWED_POST_COMMENTS = 'Post: Comments: Show'
export const STARTED_LOGIN = 'Login start'
export const STARTED_SIGNUP = 'Signup start'
export const VIEWED_COMMUNITY = 'Community: Load Community'
export const VIEWED_NOTIFICATIONS = 'Notifications: View'
export const VIEWED_PERSON = 'Member Profiles: Loaded a profile'
export const VIEWED_SELF = 'Member Profiles: Loaded Own Profile'

export function trackEvent (eventName, props = {}) {
  const track = partial(window.analytics.track, eventName)
  const { person, post, community, ...otherProps } = props

  switch (eventName) {
    case VIEWED_PERSON:
      let { id, name } = person
      track({id, name, ...otherProps})
      break
    case ADDED_COMMENT:
    case SHOWED_POST_COMMENTS:
      track({post_id: post.id, ...otherProps})
      break
    case CLICKTHROUGH:
      track({community, ...otherProps})
      break
    default:
      track({community: get('name', community), ...otherProps})
  }

  return Promise.resolve()
}

export const trackSearch = (term, type, community) => {
  trackEvent(SEARCHED, {community, term, type})
}

export const identify = person => {
  if (!person) return

  let { id, name, email, post_count, created_at } = person
{'fraction_non_alphanumeric': 0.08657587548638132, 'fraction_numerical': 0.0, 'mean_word_length': 4.245748299319728, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3586784', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1846, 'n_words': 639}