AbTestingManager.coffee 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. define [
  2. "base"
  3. "libs/md5"
  4. ], (App) ->
  5. oldKeys = [
  6. "sl_abt_multi_currency_editor_eu-eu"
  7. "sl_abt_multi_currency_eu-eu"
  8. "sl_abt_multi_currency_editor_eu-usd"
  9. "sl_abt_multi_currency_eu-usd"
  10. "sl_abt_trial_len_14d"
  11. "sl_abt_trial_len_7d"
  12. "sl_abt_trial_len_30d"
  13. "sl_utt"
  14. "sl_utt_trial_len"
  15. "sl_utt_multi_currency"
  16. ]
  17. App.factory "abTestManager", ($http, ipCookie) ->
  18. _.each oldKeys, (oldKey)->
  19. ipCookie.remove(oldKey)
  20. _buildCookieKey = (testName, bucket)->
  21. key = "sl_abt_#{testName}_#{bucket}"
  22. #console.log key
  23. return key
  24. _getTestCookie = (testName, bucket)->
  25. cookieKey = _buildCookieKey(testName, bucket)
  26. cookie = ipCookie(cookieKey)
  27. #console.log cookieKey, cookie
  28. return cookie
  29. _persistCookieStep = (testName, bucket, newStep)->
  30. cookieKey = _buildCookieKey(testName, bucket)
  31. ipCookie(cookieKey, {step:newStep}, {expires:100, path:"/"})
  32. #console.log("persisting", cookieKey, {step:newStep})
  33. ga('send', 'event', 'ab_tests', "#{testName}:#{bucket}", "step-#{newStep}")
  34. _checkIfStepIsNext = (cookieStep, newStep)->
  35. #console.log cookieStep, newStep, "checking if step is next"
  36. if !cookieStep? and newStep != 0
  37. return false
  38. else if newStep == 0
  39. return true
  40. else if (cookieStep+1) == newStep
  41. return true
  42. else
  43. return false
  44. _getUsersHash = (testName)->
  45. sl_user_test_token = "sl_utt_#{testName}"
  46. user_uuid = ipCookie(sl_user_test_token)
  47. if !user_uuid?
  48. user_uuid = Math.random()
  49. ipCookie(sl_user_test_token, user_uuid, {expires:365, path:"/"})
  50. hash = CryptoJS.MD5("#{user_uuid}:#{testName}")
  51. return hash
  52. processTestWithStep: processTestWithStep = (testName, bucket, newStep)->
  53. currentCookieStep = _getTestCookie(testName, bucket)?.step
  54. if _checkIfStepIsNext(currentCookieStep, newStep)
  55. _persistCookieStep(testName, bucket, newStep)
  56. getABTestBucket: getABTestBucket = (test_name, buckets) ->
  57. hash = _getUsersHash(test_name)
  58. bucketIndex = parseInt(hash.toString().slice(0,2), 16) % (buckets?.length or 2)
  59. return buckets[bucketIndex]
  60. App.controller "AbTestController", ($scope, abTestManager)->
  61. testKeys = _.keys(window.ab)
  62. _.each window.ab, (event)->
  63. abTestManager.processTestWithStep event.testName, event.bucket, event.step