AbTestingManager.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.factory "algoliawiki", ->
  57. client = new AlgoliaSearch("SK53GL4JLY", "e398f35d3074fde57ca6d6c88d8be37c")
  58. index = client.initIndex("lean-wiki-index")
  59. return index
  60. App.controller "SearchWikiController", ($scope, algoliawiki, _) ->
  61. algolia = algoliawiki
  62. $scope.hits = []
  63. $scope.clearSearchText = ->
  64. $scope.searchQueryText = ""
  65. updateHits []
  66. $scope.safeApply = (fn)->
  67. phase = $scope.$root.$$phase
  68. if(phase == '$apply' || phase == '$digest')
  69. $scope.$eval(fn)
  70. else
  71. $scope.$apply(fn)
  72. buildHitViewModel = (hit)->
  73. page_underscored = hit.pageName.replace(/\s/g,'_')
  74. section_underscored = hit.sectionName.replace(/\s/g,'_')
  75. result =
  76. name : hit._highlightResult.pageName.value + " - " + hit._highlightResult.sectionName.value
  77. url :"/learn/#{page_underscored}##{section_underscored}"
  78. console.log result
  79. return result
  80. updateHits = (hits)->
  81. $scope.safeApply ->
  82. $scope.hits = hits
  83. $scope.search = ->
  84. query = $scope.searchQueryText
  85. console.log query
  86. if !query? or query.length == 0
  87. updateHits []
  88. return
  89. algolia.search query, (err, response)->
  90. if response.hits.length == 0
  91. updateHits []
  92. else
  93. hits = _.map response.hits, buildHitViewModel
  94. updateHits hits
  95. App.controller "AbTestController", ($scope, abTestManager)->
  96. testKeys = _.keys(window.ab)
  97. _.each window.ab, (event)->
  98. abTestManager.processTestWithStep event.testName, event.bucket, event.step