forked from AmitKuma-04/PowerBIPushIntegration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataExportService.cs
256 lines (230 loc) · 10.4 KB
/
DataExportService.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.XConnect.Collection.Model;
using Serilog;
using CTModel = Sitecore.XConnect.ServicePlugins.InteractionsTracker.Models;
namespace Sitecore.XConnect.ServicePlugins.InteractionsTracker
{
public class DataExportService
{
public static void SendContactInteraction(string contactId)
{
try
{
var contact = new Models.Contact()
{
Id = contactId
};
using (var adapter = new PowerBIAdapter())
{
adapter.PushRow(contact, "Contact");
}
}
catch (Exception e)
{
Log.Error("Error during Export data to Power BI. Error: {0}", e.Message);
}
}
public static void SendContactInteraction(Interaction entity)
{
try
{
var contact = new Models.Contact()
{
Id = entity.Contact.Id.Value.ToString()
};
var interaction = new Models.Interaction()
{
Id = entity.Id.Value.ToString(),
CampaignId = entity.CampaignId?.ToString(),
ChannelId = entity.ChannelId.ToString(),
ConcurrencyToken = entity.ConcurrencyToken?.ToString(),
ContactId = entity.Contact.Id?.ToString(),
Duration = (int)entity.Duration.TotalMilliseconds,
EndDataTime = entity.EndDateTime,
StartDataTime = entity.StartDateTime,
EngagementValue = entity.EngagementValue,
VenueId = entity.VenueId?.ToString()
};
var eventList = entity.Events.Select(x => new Models.Event()
{
Id = x.Id.ToString(),
DefenitionId = x.DefinitionId.ToString(),
InteractionId = entity.Id.Value.ToString(),
ItemId = x.ItemId.ToString(),
EngagementValue = x.EngagementValue,
Text = x.Text,
Duration = (int)x.Duration.TotalMilliseconds,
Timestamp = x.Timestamp
});
using (var adapter = new PowerBIAdapter())
{
adapter.PushRow(interaction, "Interaction");
adapter.PushRow(contact, "Contact");
adapter.PushRows(eventList, "Event");
}
}
catch (Exception e)
{
Log.Error("Error during Export data to Power BI. Error: {0}", e.Message);
}
}
public static void SendFacet(Facet facet, string interactionId, string entityId)
{
try
{
var contact = new Models.Contact()
{
Id = entityId
};
if (facet is PersonalInformation)
{
var _personalInfoFacet = facet as PersonalInformation;
var personalInfoFacet = new CTModel.PersonalInformation()
{
FirstName = _personalInfoFacet.FirstName,
LastName = _personalInfoFacet.LastName,
JobTitle = _personalInfoFacet.JobTitle,
InteractionId = interactionId,
ContactId = entityId
};
using (var adapter = new PowerBIAdapter())
{
adapter.PushRow(personalInfoFacet, "PersonalInformation");
}
}
if (facet is EmailAddressList)
{
var _emailAddressFacet = facet as EmailAddressList;
var emailAddressFacet = new CTModel.EmailAddressList()
{
Email = _emailAddressFacet.PreferredEmail.SmtpAddress,
InteractionId = interactionId,
ContactId = entityId
};
using (var adapter = new PowerBIAdapter())
{
adapter.PushRow(emailAddressFacet, "EmailAddressList");
}
}
if (string.Equals(facet.XObject.XdbType.LocalName, "EmployerFacetData", StringComparison.InvariantCultureIgnoreCase))
{
var employerDataFacet = new CTModel.EmployerFacetData()
{
SubTitle = Convert.ToString(facet.XObject["SubTitle"]),
Company = Convert.ToString(facet.XObject["Company"]),
Consent = Convert.ToString(facet.XObject["Consent"]),
PreferredOfficeLocation = Convert.ToString(facet.XObject["PreferredOfficeLocation"]),
InteractionId = interactionId,
ContactId = entityId
};
using (var adapter = new PowerBIAdapter())
{
adapter.PushRow(employerDataFacet, "EmployerFacetData");
}
}
if (facet is IpInfo)
{
var _ipFacet = facet as IpInfo;
//Faking GEO IP data for local environment for testing purposes
var geoIpData = FakeGeoIpService.GetGeoIp(_ipFacet.IpAddress);
var ipFacet = new Models.IpFacet()
{
InteractionId = interactionId,
AreaCode = _ipFacet.AreaCode,
BusinessName = _ipFacet.BusinessName,
City = geoIpData.City,
Country = geoIpData.Country,
Dns = _ipFacet.Dns,
IpAddress = geoIpData.IpAddress,
Isp = _ipFacet.Isp,
Latitude = geoIpData.Latitude,
LocationId = _ipFacet.LocationId?.ToString(),
Longitude = geoIpData.Longitude,
MetroCode = _ipFacet.MetroCode,
PostalCode = _ipFacet.PostalCode,
Region = _ipFacet.Region,
Url = _ipFacet.Url
};
using (var adapter = new PowerBIAdapter())
{
adapter.PushRow(ipFacet, "IpFacet");
}
}
if (facet is ContactBehaviorProfile)
{
var _profileScoresFacet = facet as ContactBehaviorProfile;
var profieScoreList = new List<Models.ProfileScoresFacet>();
foreach (var score in _profileScoresFacet.Scores)
{
foreach (var value in score.Value.Values)
{
profieScoreList.Add(new Models.ProfileScoresFacet()
{
InteractionId = interactionId,
ScoreId = score.Key.ToString(),
MatchedPatternId = score.Value.MatchedPatternId?.ToString(),
ProfileDefinitionId = score.Value.ProfileDefinitionId.ToString(),
Score = score.Value.Score,
ScoreCount = score.Value.ScoreCount,
ValueId = value.Key.ToString(),
Value = value.Value
});
}
}
using (var adapter = new PowerBIAdapter())
{
adapter.PushRows(profieScoreList, "ProfileScoresFacet");
}
}
if (facet is UserAgentInfo)
{
var _userAgentFacet = facet as UserAgentInfo;
var userAgentFacet = new Models.UserAgentFacet()
{
InteractionId = interactionId,
DeviceType = _userAgentFacet.DeviceType,
DeviceVendor = _userAgentFacet.DeviceVendor,
DeviceVendorHardwareModel = _userAgentFacet.DeviceVendorHardwareModel
};
using (var adapter = new PowerBIAdapter())
{
adapter.PushRow(userAgentFacet, "UserAgentFacet");
}
}
if (facet is WebVisit)
{
var _webVisitFacet = facet as WebVisit;
var webVisitFacet = new Models.WebVisitFacet()
{
InteractionId = interactionId,
BrowserMajorName = _webVisitFacet.Browser.BrowserMajorName,
BrowserMinorName = _webVisitFacet.Browser.BrowserMinorName,
BrowserVersion = _webVisitFacet.Browser.BrowserVersion,
Language = _webVisitFacet.Language,
MajorVersion = _webVisitFacet.OperatingSystem.MajorVersion,
MinorVersion = _webVisitFacet.OperatingSystem.MinorVersion,
Name = _webVisitFacet.OperatingSystem.Name,
Referrer = _webVisitFacet.Referrer,
#pragma warning disable CS0618 // Type or member is obsolete
ReferringSite = _webVisitFacet.ReferringSite,
#pragma warning restore CS0618 // Type or member is obsolete
ScreenHeight = _webVisitFacet.Screen.ScreenHeight,
ScreenWidth = _webVisitFacet.Screen.ScreenWidth,
SearchKeywords = _webVisitFacet.SearchKeywords,
SiteName = _webVisitFacet.SiteName
};
using (var adapter = new PowerBIAdapter())
{
adapter.PushRow(webVisitFacet, "WebVisitFacet");
}
}
}
catch (Exception e)
{
Log.Error("Error during Export data to Power BI. Error: {0}", e.Message);
}
}
}
}