<commit_before>_ = require 'underscore'
{ CURRENT_USER } = require('sharify').data
{ setDimension } = require '../../lib/analytics.coffee'
IS_TEST_ENV = require '../../lib/is_test_env.coffee'

module.exports = class SplitTest
  constructor: ({ @key, @outcomes, @edge, @dimension }) ->
    return throw new Error('Your probability values for outcomes must add up to 1.0') if @sum() isnt 1

  _key: ->
    "split_test--#{@key}"

  cookies: ->
    if IS_TEST_ENV
      set: (->), get: (->), expire: (->)
    else
      @__cookies__ ?= require 'cookies-js'

  set: (outcome) ->
    @cookies().set @_key(), outcome

    analytics?.track 'Experiment Viewed',
      experiment_id: @key
      experiment_name: @key
      variation_id: outcome
      variation_name: outcome

    # Set for Google Analytics
    setDimension @dimension, outcome if @dimension?

    outcome

  get: ->
    @cookies().get(@_key())

  unset: ->
    @cookies().expire @_key()
    location.reload()

  cssClass: ->
    "is-splittest-#{@key}--#{@outcome()}"

  admin: ->
    CURRENT_USER?.type is 'Admin'

  toss: ->
    _.sample _.flatten _.map @outcomes, (probability, outcome) ->
      _.times(Math.round(probability * 10), -> outcome)

  sum: ->
    _.reduce @outcomes, (memo, probability, outcome) ->
      memo + probability
    , 0

  outcome: ->
    outcome = if (@admin() and @edge?) then @edge else @get()
    if outcome?
      @set outcome
    else
      @set @toss()
<commit_msg>check that track is a function<commit_after>_ = require 'underscore'
{ CURRENT_USER } = require('sharify').data
{ setDimension } = require '../../lib/analytics.coffee'
IS_TEST_ENV = require '../../lib/is_test_env.coffee'

module.exports = class SplitTest
  constructor: ({ @key, @outcomes, @edge, @dimension }) ->
    return throw new Error('Your probability values for outcomes must add up to 1.0') if @sum() isnt 1

  _key: ->
    "split_test--#{@key}"

  cookies: ->
    if IS_TEST_ENV
      set: (->), get: (->), expire: (->)
    else
      @__cookies__ ?= require 'cookies-js'

  set: (outcome) ->
    @cookies().set @_key(), outcome

    if _.isFunction analytics?.track
      analytics?.track 'Experiment Viewed',
        experiment_id: @key
        experiment_name: @key
        variation_id: outcome
        variation_name: outcome

    # Set for Google Analytics
    setDimension @dimension, outcome if @dimension?

    outcome

  get: ->
    @cookies().get(@_key())

  unset: ->
    @cookies().expire @_key()
    location.reload()

  cssClass: ->
    "is-splittest-#{@key}--#{@outcome()}"

  admin: ->
    CURRENT_USER?.type is 'Admin'

  toss: ->
    _.sample _.flatten _.map @outcomes, (probability, outcome) ->
      _.times(Math.round(probability * 10), -> outcome)

  sum: ->
    _.reduce @outcomes, (memo, probability, outcome) ->
      memo + probability
    , 0

  outcome: ->
    outcome = if (@admin() and @edge?) then @edge else @get()
    if outcome?
      @set outcome
    else
      @set @toss()

{'fraction_non_alphanumeric': 0.16017461383478845, 'fraction_numerical': 0.004029550033579583, 'mean_word_length': 2.728410513141427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '7574958', 'n_tokens_mistral': 1015, 'n_tokens_neox': 992, 'n_words': 252}