overleaf/services/web/public/coffee/analytics/AbTestingManager.coffee

119 lines
3.3 KiB
CoffeeScript
Raw Normal View History

define [
"base"
"libs/md5"
], (App) ->
2014-11-25 06:35:46 -05:00
oldKeys = [
"sl_abt_multi_currency_editor_eu-eu"
"sl_abt_multi_currency_eu-eu"
"sl_abt_multi_currency_editor_eu-usd"
"sl_abt_multi_currency_eu-usd"
"sl_abt_trial_len_14d"
"sl_abt_trial_len_7d"
"sl_abt_trial_len_30d"
"sl_utt"
"sl_utt_trial_len"
"sl_utt_multi_currency"
]
App.factory "abTestManager", ($http, ipCookie) ->
2014-11-25 06:35:46 -05:00
_.each oldKeys, (oldKey)->
ipCookie.remove(oldKey)
2014-09-02 08:54:09 -04:00
_buildCookieKey = (testName, bucket)->
key = "sl_abt_#{testName}_#{bucket}"
return key
2014-09-01 12:48:09 -04:00
_getTestCookie = (testName, bucket)->
cookieKey = _buildCookieKey(testName, bucket)
2014-09-02 08:54:09 -04:00
cookie = ipCookie(cookieKey)
return cookie
2014-09-01 12:48:09 -04:00
_persistCookieStep = (testName, bucket, newStep)->
2014-09-02 08:54:09 -04:00
cookieKey = _buildCookieKey(testName, bucket)
ipCookie(cookieKey, {step:newStep}, {expires:100, path:"/"})
2014-09-02 05:47:58 -04:00
ga('send', 'event', 'ab_tests', "#{testName}:#{bucket}", "step-#{newStep}")
_checkIfStepIsNext = (cookieStep, newStep)->
if !cookieStep? and newStep != 0
return false
else if newStep == 0
return true
else if (cookieStep+1) == newStep
return true
else
return false
2014-09-01 12:48:09 -04:00
_getUsersHash = (testName)->
2014-10-15 11:21:45 -04:00
sl_user_test_token = "sl_utt_#{testName}"
2014-09-01 12:48:09 -04:00
user_uuid = ipCookie(sl_user_test_token)
if !user_uuid?
user_uuid = Math.random()
ipCookie(sl_user_test_token, user_uuid, {expires:365, path:"/"})
2014-09-01 12:48:09 -04:00
hash = CryptoJS.MD5("#{user_uuid}:#{testName}")
return hash
processTestWithStep: processTestWithStep = (testName, bucket, newStep)->
currentCookieStep = _getTestCookie(testName, bucket)?.step
if _checkIfStepIsNext(currentCookieStep, newStep)
2014-09-01 12:48:09 -04:00
_persistCookieStep(testName, bucket, newStep)
2014-09-01 12:48:09 -04:00
getABTestBucket: getABTestBucket = (test_name, buckets) ->
hash = _getUsersHash(test_name)
bucketIndex = parseInt(hash.toString().slice(0,2), 16) % (buckets?.length or 2)
return buckets[bucketIndex]
2014-09-01 12:48:09 -04:00
2016-03-18 07:31:50 -04:00
App.factory "algoliawiki", ->
client = new AlgoliaSearch("SK53GL4JLY", "e398f35d3074fde57ca6d6c88d8be37c")
index = client.initIndex("lean-wiki-index")
return index
App.controller "SearchWikiController", ($scope, algoliawiki, _) ->
algolia = algoliawiki
$scope.hits = []
$scope.clearSearchText = ->
$scope.searchQueryText = ""
updateHits []
$scope.safeApply = (fn)->
phase = $scope.$root.$$phase
if(phase == '$apply' || phase == '$digest')
$scope.$eval(fn)
else
$scope.$apply(fn)
buildHitViewModel = (hit)->
page_underscored = hit.pageName.replace(/\s/g,'_')
section_underscored = hit.sectionName.replace(/\s/g,'_')
2016-03-18 07:31:50 -04:00
result =
name : hit._highlightResult.pageName.value + " - " + hit._highlightResult.sectionName.value
url :"/learn/#{page_underscored}##{section_underscored}"
2016-03-18 07:31:50 -04:00
console.log result
return result
updateHits = (hits)->
$scope.safeApply ->
$scope.hits = hits
$scope.search = ->
query = $scope.searchQueryText
console.log query
2016-03-18 07:31:50 -04:00
if !query? or query.length == 0
updateHits []
return
algolia.search query, (err, response)->
if response.hits.length == 0
updateHits []
else
hits = _.map response.hits, buildHitViewModel
updateHits hits
2014-09-01 12:48:09 -04:00
App.controller "AbTestController", ($scope, abTestManager)->
testKeys = _.keys(window.ab)
2014-09-01 12:48:09 -04:00
_.each window.ab, (event)->
abTestManager.processTestWithStep event.testName, event.bucket, event.step