AbTestingManager.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. define(['../base', 'crypto-js/md5'], function(App, CryptoJS) {
  16. const oldKeys = [
  17. 'sl_abt_multi_currency_editor_eu-eu',
  18. 'sl_abt_multi_currency_eu-eu',
  19. 'sl_abt_multi_currency_editor_eu-usd',
  20. 'sl_abt_multi_currency_eu-usd',
  21. 'sl_abt_trial_len_14d',
  22. 'sl_abt_trial_len_7d',
  23. 'sl_abt_trial_len_30d',
  24. 'sl_utt',
  25. 'sl_utt_trial_len',
  26. 'sl_utt_multi_currency'
  27. ]
  28. App.factory('abTestManager', function($http, ipCookie) {
  29. let getABTestBucket, processTestWithStep
  30. _.each(oldKeys, oldKey => ipCookie.remove(oldKey))
  31. const _buildCookieKey = function(testName, bucket) {
  32. const key = `sl_abt_${testName}_${bucket}`
  33. return key
  34. }
  35. const _getTestCookie = function(testName, bucket) {
  36. const cookieKey = _buildCookieKey(testName, bucket)
  37. const cookie = ipCookie(cookieKey)
  38. return cookie
  39. }
  40. const _persistCookieStep = function(testName, bucket, newStep) {
  41. const cookieKey = _buildCookieKey(testName, bucket)
  42. ipCookie(cookieKey, { step: newStep }, { expires: 100, path: '/' })
  43. return ga(
  44. 'send',
  45. 'event',
  46. 'ab_tests',
  47. `${testName}:${bucket}`,
  48. `step-${newStep}`
  49. )
  50. }
  51. const _checkIfStepIsNext = function(cookieStep, newStep) {
  52. if (cookieStep == null && newStep !== 0) {
  53. return false
  54. } else if (newStep === 0) {
  55. return true
  56. } else if (cookieStep + 1 === newStep) {
  57. return true
  58. } else {
  59. return false
  60. }
  61. }
  62. const _getUsersHash = function(testName) {
  63. const sl_user_test_token = `sl_utt_${testName}`
  64. let user_uuid = ipCookie(sl_user_test_token)
  65. if (user_uuid == null) {
  66. user_uuid = Math.random()
  67. ipCookie(sl_user_test_token, user_uuid, { expires: 365, path: '/' })
  68. }
  69. const hash = CryptoJS(`${user_uuid}:${testName}`)
  70. return hash
  71. }
  72. return {
  73. processTestWithStep: (processTestWithStep = function(
  74. testName,
  75. bucket,
  76. newStep
  77. ) {
  78. const currentCookieStep = __guard__(
  79. _getTestCookie(testName, bucket),
  80. x => x.step
  81. )
  82. if (_checkIfStepIsNext(currentCookieStep, newStep)) {
  83. return _persistCookieStep(testName, bucket, newStep)
  84. }
  85. }),
  86. getABTestBucket: (getABTestBucket = function(test_name, buckets) {
  87. const hash = _getUsersHash(test_name)
  88. const bucketIndex =
  89. parseInt(hash.toString().slice(0, 2), 16) %
  90. ((buckets != null ? buckets.length : undefined) || 2)
  91. return buckets[bucketIndex]
  92. })
  93. }
  94. })
  95. return App.controller('AbTestController', function($scope, abTestManager) {
  96. const testKeys = _.keys(window.ab)
  97. return _.each(window.ab, event =>
  98. abTestManager.processTestWithStep(
  99. event.testName,
  100. event.bucket,
  101. event.step
  102. )
  103. )
  104. })
  105. })
  106. function __guard__(value, transform) {
  107. return typeof value !== 'undefined' && value !== null
  108. ? transform(value)
  109. : undefined
  110. }