Browse Source

Added cacheing to the Config loading instead of file hash.

Reworked page view tracking to match link and download.
tags/3.0.0
Teknikode 1 year ago
parent
commit
86559669b7

+ 6
- 10
Teknik/Filters/TrackDownload.cs View File

@@ -21,15 +21,11 @@ namespace Teknik.Filters

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Config config = Config.Load();
HttpRequestBase request = filterContext.HttpContext.Request;

if (config.PiwikConfig.Enabled)
string doNotTrack = request.Headers["DNT"];
if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
{
HttpRequestBase request = filterContext.HttpContext.Request;
string doNotTrack = request.Headers["DNT"];
bool dnt = (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1");

string userAgent = request.UserAgent;

string ipAddress = request.ClientIPFromRequest(true);
@@ -41,15 +37,15 @@ namespace Teknik.Filters
url = request.Url.ToString();

// Fire and forget. Don't need to wait for it.
Task.Run(() => AsyncTrackDownload(dnt, config.PiwikConfig.SiteId, config.PiwikConfig.Url, userAgent, ipAddress, config.PiwikConfig.TokenAuth, url, urlReferrer));
Task.Run(() => AsyncTrackDownload(userAgent, ipAddress, url, urlReferrer));
}

base.OnActionExecuted(filterContext);
}

private void AsyncTrackDownload(bool dnt, int siteId, string siteUrl, string userAgent, string clientIp, string token, string url, string urlReferrer)
private static void AsyncTrackDownload(string userAgent, string clientIp, string url, string urlReferrer)
{
Tracking.TrackDownload(dnt, siteId, siteUrl, userAgent, clientIp, token, url, urlReferrer);
Tracking.TrackDownload(userAgent, clientIp, url, urlReferrer);
}
}
}

+ 6
- 10
Teknik/Filters/TrackLink.cs View File

@@ -20,15 +20,11 @@ namespace Teknik.Filters

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Config config = Config.Load();
HttpRequestBase request = filterContext.HttpContext.Request;

if (config.PiwikConfig.Enabled)
string doNotTrack = request.Headers["DNT"];
if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
{
HttpRequestBase request = filterContext.HttpContext.Request;

string doNotTrack = request.Headers["DNT"];
bool dnt = (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1");

string userAgent = request.UserAgent;

string ipAddress = request.ClientIPFromRequest(true);
@@ -40,15 +36,15 @@ namespace Teknik.Filters
url = request.Url.ToString();

// Fire and forget. Don't need to wait for it.
Task.Run(() => AsyncTrackLink(dnt, config.PiwikConfig.SiteId, config.PiwikConfig.Url, userAgent, ipAddress, config.PiwikConfig.TokenAuth, url, urlReferrer));
Task.Run(() => AsyncTrackLink(userAgent, ipAddress, url, urlReferrer));
}

base.OnActionExecuted(filterContext);
}

private void AsyncTrackLink(bool dnt, int siteId, string siteUrl, string userAgent, string clientIp, string token, string url, string urlReferrer)
private static void AsyncTrackLink(string userAgent, string clientIp, string url, string urlReferrer)
{
Tracking.TrackLink(dnt, siteId, siteUrl, userAgent, clientIp, token, url, urlReferrer);
Tracking.TrackLink(userAgent, clientIp, url, urlReferrer);
}
}
}

+ 31
- 6
Teknik/Filters/TrackPageView.cs View File

@@ -21,23 +21,48 @@ namespace Teknik.Filters

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Config config = Config.Load();
HttpRequestBase request = filterContext.HttpContext.Request;

if (config.PiwikConfig.Enabled)
string doNotTrack = request.Headers["DNT"];
if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
{
string title = filterContext.Controller?.ViewBag?.Title;

HttpRequestBase request = filterContext.HttpContext.Request;
string sub = request.RequestContext.RouteData.Values["sub"].ToString();
if (string.IsNullOrEmpty(sub))
{
sub = request.Url?.AbsoluteUri.GetSubdomain();
}

string ipAddress = request.ClientIPFromRequest(true);

string url = string.Empty;
if (request.Url != null)
url = request.Url.ToString();

string urlReferrer = request.UrlReferrer?.ToString();

string userAgent = request.UserAgent;

int pixelWidth = request.Browser.ScreenPixelsWidth;
int pixelHeight = request.Browser.ScreenPixelsHeight;

bool hasCookies = request.Browser.Cookies;

string acceptLang = request.Headers["Accept-Language"];

bool hasJava = request.Browser.JavaApplets;

// Fire and forget. Don't need to wait for it.
Task.Run(() => AsyncTrackPageView(request, config, title));
Task.Run(() => AsyncTrackPageView(title, sub, ipAddress, url, urlReferrer, userAgent,pixelWidth, pixelHeight, hasCookies, acceptLang, hasJava));
}

base.OnActionExecuted(filterContext);
}

private void AsyncTrackPageView(HttpRequestBase request, Config config, string title)
private static void AsyncTrackPageView(string title, string sub, string clientIp, string url, string urlReferrer, string userAgent, int pixelWidth, int pixelHeight, bool hasCookies, string acceptLang, bool hasJava)
{
Tracking.TrackPageView(request, config, title);
Tracking.TrackPageView(title, sub, clientIp, url, urlReferrer, userAgent, pixelWidth, pixelHeight, hasCookies, acceptLang, hasJava);
}
}
}

+ 1
- 1
Teknik/Web.config View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880

+ 10
- 14
Utilities/Configuration/Config.cs View File

@@ -1,6 +1,8 @@
using System;
using System.IO;
using System.Threading;
using System.Web;
using System.Web.Caching;
using Newtonsoft.Json;
using Teknik.Utilities;
using Teknik.Utilities.Cryptography;
@@ -9,11 +11,10 @@ namespace Teknik.Configuration
{
public class Config
{
private static Config _Config { get; set; }
private static string _FileHash { get; set; }
private const string _ConfigCacheKey = "ConfigCache";

private ReaderWriterLockSlim _ConfigRWLock;
private ReaderWriterLockSlim _ConfigFileRWLock;
private static Config _Config { get; set; }
private JsonSerializerSettings _JsonSettings;

private bool _DevEnvironment;
@@ -116,8 +117,6 @@ namespace Teknik.Configuration

public Config()
{
_ConfigRWLock = new ReaderWriterLockSlim();
_ConfigFileRWLock = new ReaderWriterLockSlim();
_JsonSettings = new JsonSerializerSettings();
_JsonSettings.Formatting = Formatting.Indented;

@@ -171,16 +170,13 @@ namespace Teknik.Configuration

public static Config Load()
{
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
string newHash = string.Empty;
if (File.Exists(Path.Combine(path, "Config.json")))
{
newHash = MD5.FileHash(Path.Combine(path, "Config.json"));
}
if (_Config == null || _FileHash == null || newHash != _FileHash)
HttpContext context = HttpContext.Current;
_Config = (Config)context.Cache[_ConfigCacheKey];
if (_Config == null)
{
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
_Config = Load(path);
_FileHash = newHash;
context.Cache.Insert(_ConfigCacheKey, _Config, new CacheDependency(path));
}
return _Config;
}

+ 1
- 0
Utilities/Configuration/Configuration.csproj View File

@@ -37,6 +37,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

+ 1
- 1
Utilities/Logging/Logger.cs View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

+ 1
- 0
Utilities/Logging/Logging.csproj View File

@@ -35,6 +35,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

+ 27
- 36
Utilities/Piwik/Tracking.cs View File

@@ -8,20 +8,14 @@ namespace Teknik.Piwik
{
public static class Tracking
{
public static void TrackPageView(HttpRequestBase request, Config config, string title)
public static void TrackPageView(string title, string sub, string clientIp, string url, string urlReferrer, string userAgent, int pixelWidth, int pixelHeight, bool hasCookies, string acceptLang, bool hasJava)
{
try
{
// Follow Do Not Track
string doNotTrack = request.Headers["DNT"];
if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
{
string sub = request.RequestContext.RouteData.Values["sub"].ToString();
if (string.IsNullOrEmpty(sub))
{
sub = request.Url.AbsoluteUri.GetSubdomain();
}
Config config = Config.Load();

if (config.PiwikConfig.Enabled)
{
if (config.DevEnvironment)
{
sub = "dev - " + sub;
@@ -30,29 +24,25 @@ namespace Teknik.Piwik
//PiwikTracker.URL = config.PiwikConfig.Url;
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId, config.PiwikConfig.Url);

// Get Request Info
string ipAddress = request.ClientIPFromRequest(true);
tracker.SetIp(ipAddress);
// Set Request Info
tracker.SetIp(clientIp);
tracker.SetTokenAuth(config.PiwikConfig.TokenAuth);
tracker.SetUrl(request.Url.ToString());

tracker.SetUserAgent(request.UserAgent);
tracker.SetUserAgent(userAgent);

// 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);
// Set browser info
tracker.SetResolution(pixelWidth, pixelHeight);
tracker.SetBrowserHasCookies(hasCookies);
if (!string.IsNullOrEmpty(acceptLang))
tracker.SetBrowserLanguage(acceptLang);
tracker.SetPlugins(new BrowserPlugins {Java = hasJava});

// Get Referral
if (request.UrlReferrer != null)
tracker.SetUrlReferrer(request.UrlReferrer.ToString());
if (!string.IsNullOrEmpty(urlReferrer))
tracker.SetUrlReferrer(urlReferrer);

if (request.Url != null)
tracker.SetUrl(request.Url.ToString());
if (!string.IsNullOrEmpty(url))
tracker.SetUrl(url);

// Send the tracking request
tracker.DoTrackPageView(string.Format("{0}/{1}", sub, title));
@@ -64,29 +54,30 @@ namespace Teknik.Piwik
}
}

public static void TrackDownload(bool dnt, int siteId, string siteUrl, string userAgent, string clientIp, string token, string url, string urlReferrer)
public static void TrackDownload(string userAgent, string clientIp, string url, string urlReferrer)
{
TrackAction(ActionType.Download, dnt, siteId, siteUrl, userAgent, clientIp, token, url, urlReferrer);
TrackAction(ActionType.Download, userAgent, clientIp, url, urlReferrer);
}

public static void TrackLink(bool dnt, int siteId, string siteUrl, string userAgent, string clientIp, string token, string url, string urlReferrer)
public static void TrackLink(string userAgent, string clientIp, string url, string urlReferrer)
{
TrackAction(ActionType.Link, dnt, siteId, siteUrl, userAgent, clientIp, token, url, urlReferrer);
TrackAction(ActionType.Link, userAgent, clientIp, url, urlReferrer);
}

private static void TrackAction(ActionType type, bool dnt, int siteId, string siteUrl, string userAgent, string clientIp, string token, string url, string urlReferrer)
private static void TrackAction(ActionType type, string userAgent, string clientIp, string url, string urlReferrer)
{
try
{
// Follow Do Not Track
if (dnt)
Config config = Config.Load();

if (config.PiwikConfig.Enabled)
{
PiwikTracker tracker = new PiwikTracker(siteId, siteUrl);
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId, config.PiwikConfig.Url);

tracker.SetUserAgent(userAgent);

tracker.SetIp(clientIp);
tracker.SetTokenAuth(token);
tracker.SetTokenAuth(config.PiwikConfig.TokenAuth);

// Get Referral
if (!string.IsNullOrEmpty(urlReferrer))

Loading…
Cancel
Save