-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOdpServerSideTracker.cs
222 lines (195 loc) · 6.83 KB
/
OdpServerSideTracker.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System.Net;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using OdpTracking.Configuration;
using OdpTracking.Dto;
using OdpTracking.Extensions;
using OdpTracking.Http;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace OdpTracking
{
/// <summary>
/// Main ODP server tracker implementation. Use the IOdpServerSideTracker through
/// dependency injection to get the correct implementation.
/// </summary>
public class OdpServerSideTracker : IOdpServerSideTracker
{
private const string ApiProfilesUrlSegment = "/v3/profiles";
private const string ApiProductsUrlSegment = "/v3/objects/products";
private const string ApiEventsUrlSegment = "/v3/events";
private readonly ILogger<OdpServerSideTracker> _logger;
private readonly IOdpHttpClientHelper _httpClientHelper;
private readonly IOdpTrackerOptions _odpTrackerOptions;
public OdpServerSideTracker(
ILogger<OdpServerSideTracker> logger,
IOdpHttpClientHelper httpClientHelper,
IOdpTrackerOptions odpTrackerOptions)
{
_logger = logger;
_httpClientHelper = httpClientHelper;
_odpTrackerOptions = odpTrackerOptions;
}
public void TrackLogin(string email, string vuid)
{
TrackSingleEvent(email, vuid, "account", "login");
}
public void TrackLogout(string email, string vuid)
{
TrackSingleEvent(email, vuid, "account", "logout");
}
public void TrackSingleEvent(string email, string vuid, string type, string action)
{
if (_odpTrackerOptions.IsConfigured() == false)
return;
if (HasEmailAndVuid(email, vuid))
{
/*
{
"type": "account",
"action": "login",
"identifiers": {
"email": "[email protected]",
"vuid": "6067f061d0f04b76812dc0308270a6e2"
}
}
*/
var data = new OdpDtoEvent
{
Action = action,
Type = type,
Identifiers = new OdpDtoIdentifiers
{
Email = EncodeEmail(email),
Vuid = vuid
}
};
TrackEvent(data);
}
}
public void TrackOrder(string email, string vuid, OdpDtoOrder order)
{
if (_odpTrackerOptions.IsConfigured() == false)
return;
if (HasEmailAndVuid(email, vuid))
{
/*
{
"type": "account",
"action": "login",
"identifiers": {
"email": "[email protected]",
"vuid": "6067f061d0f04b76812dc0308270a6e2"
}
}
*/
var data = new OdpDtoOrderEvent
{
Action = "purchase",
Type = "order",
Identifiers = new OdpDtoIdentifiers
{
Email = EncodeEmail(email),
Vuid = vuid
},
Data = new { Order = order }
};
string payLoad = SerializeToJson(data);
_logger.LogDebug("Tracking order: " + order.OrderId);
_logger.LogDebug(payLoad);
PostPayload(payLoad, ApiEventsUrlSegment);
}
}
public void TrackEvent(OdpDtoEvent odpEvent)
{
if (_odpTrackerOptions.IsConfigured() == false)
return;
if (string.IsNullOrEmpty(odpEvent.Identifiers.Vuid) == false)
{
string payLoad = SerializeToJson(odpEvent);
PostPayload(payLoad, ApiEventsUrlSegment);
}
}
/// <summary>
/// Connects Vuid, email and other fields. Useful when you do not have
/// any other information about the profile, or you want
/// to connect a new vuid to an email.
/// </summary>
/// <param name="email"></param>
/// <param name="vuid"></param>
/// <param name="firstName"></param>
/// <param name="lastName"></param>
public void UpdateProfile(string vuid, string email, string firstName = null, string lastName = null)
{
var profile = new OdpDtoProfile
{
Vuid = vuid,
Email = EncodeEmail(email),
FirstName = firstName,
LastName = lastName
};
UpdateProfile(profile);
}
public void UpdateProfile(OdpDtoProfile profile)
{
if (_odpTrackerOptions.IsConfigured() == false)
return;
if (HasEmailAndVuid(profile.Email, profile.Vuid))
{
var payLoad = SerializeToJson(new { Attributes = profile });
// {
// attributes:
// {
// email: " + email + @",
// vuid: " + vuid + @"
// }
// }
PostPayload(payLoad, ApiProfilesUrlSegment);
}
}
public void UploadProducts(IEnumerable<OdpDtoProduct> products)
{
var payLoad = SerializeToJson(products);
PostPayload(payLoad, ApiProductsUrlSegment);
}
private void PostPayload(string payLoad, string apiMethod)
{
var statusCode = _httpClientHelper.PostJson(apiMethod, payLoad);
if (statusCode.IsSuccessStatusCode() == false)
{
_logger.LogError("Posting payload to {apiMethod} failed with status code: {statusCode}", apiMethod,
statusCode);
}
}
public bool HasEmailAndVuid(string email, string vuid)
{
if (string.IsNullOrEmpty(vuid) == false &&
IsValidEmail(email))
{
return true;
}
return false;
}
public bool IsValidEmail(string email)
{
if (string.IsNullOrEmpty(email))
return false;
return email.Contains('@');
}
private string SerializeToJson(object obj)
{
var settings = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
var json = JsonSerializer.Serialize(obj, settings);
return json;
}
private string EncodeEmail(string email)
{
// Product Recs decodes the email address in the query string
// and replaces "+" with " ". Let's add that back
return email.Replace(" ", "+");
}
}
}