AbTestingManager.coffee 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. return key
  23. _getTestCookie = (testName, bucket)->
  24. cookieKey = _buildCookieKey(testName, bucket)
  25. cookie = ipCookie(cookieKey)
  26. return cookie
  27. _persistCookieStep = (testName, bucket, newStep)->
  28. cookieKey = _buildCookieKey(testName, bucket)
  29. ipCookie(cookieKey, {step:newStep}, {expires:100, path:"/"})
  30. ga('send', 'event', 'ab_tests', "#{testName}:#{bucket}", "step-#{newStep}")
  31. _checkIfStepIsNext = (cookieStep, newStep)->
  32. if !cookieStep? and newStep != 0
  33. return false
  34. else if newStep == 0
  35. return true
  36. else if (cookieStep+1) == newStep
  37. return true
  38. else
  39. return false
  40. _getUsersHash = (testName)->
  41. sl_user_test_token = "sl_utt_#{testName}"
  42. user_uuid = ipCookie(sl_user_test_token)
  43. if !user_uuid?
  44. user_uuid = Math.random()
  45. ipCookie(sl_user_test_token, user_uuid, {expires:365, path:"/"})
  46. hash = CryptoJS.MD5("#{user_uuid}:#{testName}")
  47. return hash
  48. processTestWithStep: processTestWithStep = (testName, bucket, newStep)->
  49. currentCookieStep = _getTestCookie(testName, bucket)?.step
  50. if _checkIfStepIsNext(currentCookieStep, newStep)
  51. _persistCookieStep(testName, bucket, newStep)
  52. getABTestBucket: getABTestBucket = (test_name, buckets) ->
  53. hash = _getUsersHash(test_name)
  54. bucketIndex = parseInt(hash.toString().slice(0,2), 16) % (buckets?.length or 2)
  55. return buckets[bucketIndex]
  56. App.controller "AbTestController", ($scope, abTestManager)->
  57. testKeys = _.keys(window.ab)
  58. _.each window.ab, (event)->
  59. abTestManager.processTestWithStep event.testName, event.bucket, event.step