AbTestingManager.js 3.4 KB

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