-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathVisitorTracker.cs
More file actions
222 lines (191 loc) · 8.85 KB
/
VisitorTracker.cs
File metadata and controls
222 lines (191 loc) · 8.85 KB
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;
using System.Web;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Instrumentation;
using DotNetNuke.Services.Exceptions;
namespace Dnn.WebAnalytics
{
public class VisitorTracker : IHttpModule
{
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(VisitorTracker));
private System.Text.RegularExpressions.Regex UserAgentFilter = new System.Text.RegularExpressions.Regex(VisitController.UserAgentFilter, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
VisitController visitController = new VisitController();
private VisitorInfoRepo visitorRepo = new VisitorInfoRepo();
public string ModuleName
{
get { return "VisitorTracker"; }
}
public void Init(HttpApplication application)
{
application.EndRequest += this.OnEndRequest;
}
public void OnEndRequest(object s, EventArgs e)
{
try
{
HttpContext Context = ((HttpApplication)s).Context;
HttpRequest Request = Context.Request;
HttpResponse Response = Context.Response;
HttpCookie cookie_visitor = null;
HttpCookie cookie_session = null;
HttpCookie cookie_request = null;
int visitor_id = 0;
Nullable<int> user_id = null;
Guid session_id = Guid.Empty;
Guid request_id = Guid.Empty;
Guid last_request_id = Guid.Empty;
PortalSettings _portalSettings = (PortalSettings)Context.Items["PortalSettings"];
// get/set cookie if visitor tracking is enabled
cookie_visitor = Request.Cookies["DNNVISITOR"];
if (cookie_visitor != null)
{
visitor_id = Convert.ToInt32(cookie_visitor.Value);
}
// update/create visitor
var visitor = visitorRepo.GetItem(visitor_id, _portalSettings.PortalId);
if (visitor == null)
{ // create Visitor record
visitor = new VisitorInfo()
{
created_on_date = DateTime.Now
};
visitorRepo.CreateItem(visitor);
}
// get User if authenticated
if (Request.IsAuthenticated)
{
UserInfo user = UserController.Instance.GetCurrentUserInfo();
if (user != null)
{
user_id = user.UserID;
}
}
// update the user_id if not set yet
if (!visitor.user_id.HasValue && user_id.GetValueOrDefault() > 0)
{
visitor.user_id = user_id;
}
visitorRepo.UpdateItem(visitor);
// only process requests for content pages
if (_portalSettings != null && Request.Url.LocalPath.ToLower().EndsWith("default.aspx"))
{
// filter web crawlers and other bots
if (String.IsNullOrEmpty(Request.UserAgent) == false && UserAgentFilter.Match(Request.UserAgent).Success == false)
{
// get last request cookie value
cookie_request = Request.Cookies["DNNREQUEST"];
if (cookie_request != null)
{
last_request_id = new Guid(cookie_request.Value);
}
// create new request cookie
request_id = Guid.NewGuid();
cookie_request = new HttpCookie("DNNREQUEST");
cookie_request.Value = request_id.ToString();
Response.Cookies.Add(cookie_request);
// get last session cookie value
cookie_session = Request.Cookies["DNNSESSION"];
if (cookie_session != null)
{
session_id = new Guid(cookie_session.Value);
}
else
{
// create a new session id
session_id = Guid.NewGuid();
cookie_session = new HttpCookie("DNNSESSION");
cookie_session.Value = session_id.ToString();
cookie_session.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.Add(cookie_session);
}
// campaign
string campaign = string.Empty;
if (Request.QueryString["campaign"] != null)
{
campaign = Request.QueryString["campaign"];
}
// create Visitor cookie
cookie_visitor = new HttpCookie("DNNVISITOR");
cookie_visitor.Value = visitor.id.ToString();
cookie_visitor.Expires = DateTime.MaxValue;
Response.Cookies.Add(cookie_visitor);
string domain = Request.Url.Host + Request.ApplicationPath;
if (domain.EndsWith("/"))
{
domain = domain.Substring(0, domain.Length - 1);
}
// get referrer URL
string url_referrer = string.Empty;
if (Request.UrlReferrer != null)
{
url_referrer = Request.UrlReferrer.ToString();
}
string domain_referrer = string.Empty;
if (!string.IsNullOrEmpty(url_referrer))
{
Uri Uri = new Uri(url_referrer);
domain_referrer = Uri.Host;
}
// get browser language
string language = string.Empty;
if (Request.UserLanguages != null)
{
if (Request.UserLanguages.Length != 0)
{
language = Request.UserLanguages[0].ToLowerInvariant().Trim();
}
}
// ip address
string ip = Request.UserHostAddress;
// url
string url = Request.RawUrl;
//user agenet
string user_agent = Request.UserAgent;
// create visit object
var visitDto = new VisitDTO()
{
date = DateTime.Now,
visitor_id = visitor.id,
tab_id = _portalSettings.ActiveTab.TabID,
ip = ip,
country = string.Empty,
region = string.Empty,
city = string.Empty,
latitude = string.Empty,
longitude = string.Empty,
language = language,
domain = domain,
url = url,
user_agent = user_agent,
device_type = "Desktop",
device = string.Empty,
platform = string.Empty,
browser = string.Empty,
referrer_domain = domain_referrer,
referrer_url = url_referrer,
server = string.Empty,
activity = "click",
campaign = campaign,
session_id = session_id,
request_id = request_id,
last_request_id = last_request_id
};
visitDto = visitController.ProcessVisit(visitDto);
VisitInfo visit = visitController.ConvertDtoToItem(null, visitDto);
var repo = new VisitInfoRepo();
repo.CreateItem(visit);
}
}
}
catch (Exception ex)
{
Logger.Error(ex.Message, ex);
Exceptions.LogException(ex);
}
}
public void Dispose()
{
}
}
}