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

53 lines
1.6 KiB
CoffeeScript
Raw Normal View History

define [
"base"
"libs/md5"
], (App) ->
App.factory "abTestManager", ($http, ipCookie) ->
2014-09-01 12:48:09 -04:00
_buildCookieKey = (testName, bucket)-> "sl_abt_#{testName}_#{bucket}"
2014-09-01 12:48:09 -04:00
_getTestCookie = (testName, bucket)->
cookieKey = _buildCookieKey(testName, bucket)
return ipCookie(cookieKey)
2014-09-01 12:48:09 -04:00
_persistCookieStep = (testName, bucket, newStep)->
ipCookie(_buildCookieKey(testName, bucket), {step:newStep}, {expires:100})
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)->
sl_user_test_token = "sl_utt"
user_uuid = ipCookie(sl_user_test_token)
if !user_uuid?
user_uuid = Math.random()
ipCookie(sl_user_test_token, user_uuid, {expires:365})
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
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