From 62f207c3684892b51462dc1a5aaee4cb2bd40fb0 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Mon, 1 Sep 2014 15:14:08 +0100 Subject: [PATCH] added basic new a/b test funnely framework --- services/web/app/views/layout.jade | 8 +++- .../coffee/analytics/AbTestingManager.coffee | 42 +++++++++++++++++++ services/web/public/coffee/main.coffee | 1 + 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 services/web/public/coffee/analytics/AbTestingManager.coffee diff --git a/services/web/app/views/layout.jade b/services/web/app/views/layout.jade index d2cf5129a0..a0186f72db 100644 --- a/services/web/app/views/layout.jade +++ b/services/web/app/views/layout.jade @@ -43,7 +43,7 @@ html(itemscope, itemtype='http://schema.org/Product') script(type="text/javascript"). window.csrfToken = "#{csrfToken}"; - + block scripts script(src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js") script(src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.14/angular.min.js") @@ -54,6 +54,7 @@ html(itemscope, itemtype='http://schema.org/Product') jsPath: '#{jsPath}' }; window.systemMessages = !{JSON.stringify(systemMessages).replace(/\//g, '\\/')}; + window.ab = {} - if (typeof(settings.algolia) != "undefined") script. @@ -125,5 +126,8 @@ html(itemscope, itemtype='http://schema.org/Product') category: "questions" }; - + span(ng-controller="AbTestController") + script. + window.ab.plan14 = {step:1} + diff --git a/services/web/public/coffee/analytics/AbTestingManager.coffee b/services/web/public/coffee/analytics/AbTestingManager.coffee new file mode 100644 index 0000000000..e948e928cd --- /dev/null +++ b/services/web/public/coffee/analytics/AbTestingManager.coffee @@ -0,0 +1,42 @@ +define [ + "base" + "libs/md5" +], (App) -> + + App.factory "abTestManager", ($http, ipCookie) -> + + _buildCookieKey = (testName)-> "sl_abt_#{testName}" + + _getTestCookie = (testName)-> + cookieKey = _buildCookieKey(testName) + return ipCookie(cookieKey) + + _persistCookieStep = (testName, newStep)-> + ipCookie(_buildCookieKey(testName), step:newStep, {expires:10}) + ga('send', 'event', 'ab_tests', testName, {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 + + processTestWithStep: processTestWithStep = (testName, newStep)-> + currentCookieStep = _getTestCookie(testName)?.step + if _checkIfStepIsNext(currentCookieStep, newStep) + _persistCookieStep(testName, newStep) + + getABTestBucket: getABTestBucket = (user_id, test_name, buckets) -> + hash = CryptoJS.MD5("#{user_id}:#{test_name}") + bucketIndex = parseInt(hash.toString().slice(0,2), 16) % (buckets?.length or 2) + return buckets[bucketIndex] + + App.controller "AbTestController", ($scope, abTestManager)-> + testKeys = _.keys(window.ab) + + _.each testKeys, (testName)-> + abTestManager.processTestWithStep testName, window.ab[testName]?.step \ No newline at end of file diff --git a/services/web/public/coffee/main.coffee b/services/web/public/coffee/main.coffee index fa71db56fc..2cf4bf4f8d 100644 --- a/services/web/public/coffee/main.coffee +++ b/services/web/public/coffee/main.coffee @@ -12,6 +12,7 @@ define [ "main/translations" "main/subscription-dashboard" "main/annual-upgrade" + "analytics/AbTestingmanager" "directives/asyncForm" "directives/stopPropagation" "directives/focus"