forked from colefichter/ABTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ABUser.cs
141 lines (121 loc) · 2.99 KB
/
ABUser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace ABTesting
{
public class ABUser
{
public int ID { get; set; }
public string Key
{
get { return ID.ToString(); }
}
public List<string> Tests { get; set; }
public List<string> Conversions { get; set; }
public ABUser()
{
ID = new Random().Next();
Tests = new List<string>();
Conversions = new List<string>();
}
public static ABUser LoadFromCookie()
{
ABUser user = new ABUser();
HttpCookie inCookie = HttpContext.Current.Request.Cookies["ab"];
if (inCookie != null)
{
try
{
user.LoadFromINI(inCookie.Value);
}
catch
{
// user messed with his cookie. No worries for us.
}
user.SaveToCookie();
}
return user;
}
public void SaveToCookie()
{
HttpCookie cookie = new HttpCookie("ab", ToINI());
cookie.Expires = DateTime.Now.AddYears(1);
// first remove, then add, in case we've already added this cookie as part of a previous save during this page load.
HttpContext.Current.Response.Cookies.Remove("ab");
HttpContext.Current.Response.Cookies.Add(cookie);
// fix up the incoming cookie so that it will load correctly if we need it again during this page load.
HttpContext.Current.Request.Cookies.Remove("ab");
HttpContext.Current.Request.Cookies.Add(cookie);
}
public void LoadFromINI(string ini)
{
string[] lines = ini.Split('|');
foreach (string line in lines)
{
if (String.IsNullOrEmpty(line))
{
continue;
}
string[] tokens = line.Split('=');
if (tokens.Length == 2)
{
string key = tokens[0].Trim().ToLower();
string value = tokens[1].Trim();
switch (key)
{
case "id":
ID = Helpers.StringConvert.ToInt32(value, ID);
break;
case "tests":
Tests = ParseCSV(value);
break;
case "conversions":
Conversions = ParseCSV(value);
break;
}
}
}
}
private List<string> ParseCSV(string csv)
{
string[] tokens = csv.Split(',');
List<string> list = new List<string>();
foreach (string token in tokens)
{
string value = token.Trim();
if (!String.IsNullOrEmpty(value))
{
if (!list.Contains(value))
{
list.Add(value);
}
}
}
return list;
}
private string StringListToCSV(List<string> list)
{
StringBuilder builder = new StringBuilder();
foreach (string item in list)
{
if (builder.Length != 0)
{
builder.Append(",");
}
builder.Append(String.Format("{0}"
, item.Replace(",", " ").Replace("=", " ").Replace("|", " ")
));
}
return builder.ToString();
}
public string ToINI()
{
return String.Format(@"ID={0}|Tests={1}|Conversions={2}"
, ID
, StringListToCSV(Tests)
, StringListToCSV(Conversions)
);
}
}
}