2016-02-26 01:41:31 +01:00
|
|
|
|
using Piwik.Tracker;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using Teknik.Configuration;
|
2017-01-18 09:12:22 +01:00
|
|
|
|
using Teknik.Utilities;
|
2016-02-26 01:41:31 +01:00
|
|
|
|
|
2017-01-18 09:12:22 +01:00
|
|
|
|
namespace Teknik.Piwik
|
2016-02-26 01:41:31 +01:00
|
|
|
|
{
|
|
|
|
|
public static class Tracking
|
|
|
|
|
{
|
2016-02-28 10:03:00 +01:00
|
|
|
|
public static void TrackPageView(HttpRequestBase request, Config config, string title)
|
2016-02-26 01:41:31 +01:00
|
|
|
|
{
|
|
|
|
|
// Handle Piwik Tracking if enabled
|
|
|
|
|
if (config.PiwikConfig.Enabled)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-05-27 02:06:37 +02:00
|
|
|
|
// Follow Do Not Track
|
|
|
|
|
string doNotTrack = request.Headers["DNT"];
|
2016-05-27 23:23:08 +02:00
|
|
|
|
if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
|
2016-02-27 00:50:34 +01:00
|
|
|
|
{
|
2016-05-27 02:06:37 +02:00
|
|
|
|
string sub = request.RequestContext.RouteData.Values["sub"].ToString();
|
|
|
|
|
if (string.IsNullOrEmpty(sub))
|
|
|
|
|
{
|
|
|
|
|
sub = request.Url.AbsoluteUri.GetSubdomain();
|
|
|
|
|
}
|
|
|
|
|
if (config.DevEnvironment)
|
|
|
|
|
{
|
|
|
|
|
sub = "dev - " + sub;
|
|
|
|
|
}
|
2016-02-27 00:50:34 +01:00
|
|
|
|
|
2016-05-27 02:06:37 +02:00
|
|
|
|
PiwikTracker.URL = config.PiwikConfig.Url;
|
|
|
|
|
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId);
|
2016-05-26 23:40:36 +02:00
|
|
|
|
|
2016-05-27 02:06:37 +02:00
|
|
|
|
// Get Request Info
|
|
|
|
|
string ipAddress = request.ClientIPFromRequest(true);
|
|
|
|
|
tracker.setIp(ipAddress);
|
|
|
|
|
tracker.setTokenAuth(config.PiwikConfig.TokenAuth);
|
|
|
|
|
tracker.setUrl(request.Url.ToString());
|
2016-02-26 01:41:31 +01:00
|
|
|
|
|
2016-05-27 02:06:37 +02:00
|
|
|
|
tracker.setUserAgent(request.UserAgent);
|
2016-02-26 01:41:31 +01:00
|
|
|
|
|
2016-05-27 02:06:37 +02:00
|
|
|
|
// Get browser info
|
|
|
|
|
tracker.setResolution(request.Browser.ScreenPixelsWidth, request.Browser.ScreenPixelsHeight);
|
|
|
|
|
tracker.setBrowserHasCookies(request.Browser.Cookies);
|
|
|
|
|
if (!string.IsNullOrEmpty(request.Headers["Accept-Language"]))
|
|
|
|
|
tracker.setBrowserLanguage(request.Headers["Accept-Language"]);
|
|
|
|
|
BrowserPlugins plugins = new BrowserPlugins();
|
|
|
|
|
plugins.java = request.Browser.JavaApplets;
|
|
|
|
|
tracker.setPlugins(plugins);
|
2016-02-26 01:41:31 +01:00
|
|
|
|
|
2016-05-27 02:06:37 +02:00
|
|
|
|
// Get Referral
|
|
|
|
|
if (request.UrlReferrer != null)
|
|
|
|
|
tracker.setUrlReferrer(request.UrlReferrer.ToString());
|
|
|
|
|
|
|
|
|
|
// Send the tracking request
|
|
|
|
|
tracker.doTrackPageView(string.Format("{0}/{1}", sub, title));
|
|
|
|
|
}
|
2016-02-26 01:41:31 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-26 08:41:50 +01:00
|
|
|
|
|
2016-05-27 23:23:08 +02:00
|
|
|
|
public static void TrackDownload(HttpRequestBase request, Config config, string url)
|
|
|
|
|
{
|
|
|
|
|
TrackAction(request, config, url, PiwikTracker.ActionType.download);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void TrackLink(HttpRequestBase request, Config config, string url)
|
|
|
|
|
{
|
|
|
|
|
TrackAction(request, config, url, PiwikTracker.ActionType.link);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void TrackAction(HttpRequestBase request, Config config, string url, PiwikTracker.ActionType type)
|
2016-02-26 08:41:50 +01:00
|
|
|
|
{
|
|
|
|
|
// Handle Piwik Tracking if enabled
|
|
|
|
|
if (config.PiwikConfig.Enabled)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-05-27 23:23:08 +02:00
|
|
|
|
// Follow Do Not Track
|
|
|
|
|
string doNotTrack = request.Headers["DNT"];
|
|
|
|
|
if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
|
|
|
|
|
{
|
|
|
|
|
PiwikTracker.URL = config.PiwikConfig.Url;
|
|
|
|
|
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId);
|
2016-02-26 08:41:50 +01:00
|
|
|
|
|
2016-05-27 23:23:08 +02:00
|
|
|
|
tracker.setUserAgent(request.UserAgent);
|
2016-02-27 00:50:34 +01:00
|
|
|
|
|
2016-05-27 23:23:08 +02:00
|
|
|
|
string ipAddress = request.ClientIPFromRequest(true);
|
2016-02-27 00:50:34 +01:00
|
|
|
|
|
2016-05-27 23:23:08 +02:00
|
|
|
|
tracker.setIp(ipAddress);
|
|
|
|
|
tracker.setTokenAuth(config.PiwikConfig.TokenAuth);
|
2016-02-26 08:41:50 +01:00
|
|
|
|
|
2016-06-16 22:15:33 +02:00
|
|
|
|
// Get Referral
|
|
|
|
|
if (request.UrlReferrer != null)
|
|
|
|
|
tracker.setUrlReferrer(request.UrlReferrer.ToString());
|
|
|
|
|
|
2016-05-27 23:23:08 +02:00
|
|
|
|
tracker.doTrackAction(url, type);
|
|
|
|
|
}
|
2016-02-26 08:41:50 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-26 01:41:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|