Просмотр исходного кода

added basic new a/b test funnely framework

Henry Oswald 12 лет назад
Родитель
Сommit
62f207c368

+ 6 - 2
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}
+
 

+ 42 - 0
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

+ 1 - 0
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"