Browse Source

Optimized tracking filters

pull/111/head
Teknikode 4 years ago
parent
commit
8827992935
  1. 16
      Teknik/Filters/TrackDownload.cs
  2. 17
      Teknik/Filters/TrackLink.cs
  3. 19
      Teknik/Filters/TrackPageView.cs
  4. 127
      Utilities/Piwik/Tracking.cs

16
Teknik/Filters/TrackDownload.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -21,16 +21,20 @@ namespace Teknik.Filters @@ -21,16 +21,20 @@ namespace Teknik.Filters
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
// Fire and forget. Don't need to wait for it.
Task.Run(() => AsyncTrackDownload(request, request.Url.ToString()));
Config config = Config.Load();
if (config.PiwikConfig.Enabled)
{
HttpRequestBase request = filterContext.HttpContext.Request;
// Fire and forget. Don't need to wait for it.
Task.Run(() => AsyncTrackDownload(request, config, request.Url?.ToString()));
}
base.OnActionExecuted(filterContext);
}
private void AsyncTrackDownload(HttpRequestBase request, string url)
private void AsyncTrackDownload(HttpRequestBase request, Config config, string url)
{
Config config = Config.Load();
Tracking.TrackDownload(request, config, url);
}
}

17
Teknik/Filters/TrackLink.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -19,16 +19,21 @@ namespace Teknik.Filters @@ -19,16 +19,21 @@ namespace Teknik.Filters
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
// Fire and forget. Don't need to wait for it.
Task.Run(() => AsyncTrackLink(request, request.Url.ToString()));
Config config = Config.Load();
if (config.PiwikConfig.Enabled)
{
HttpRequestBase request = filterContext.HttpContext.Request;
// Fire and forget. Don't need to wait for it.
Task.Run(() => AsyncTrackLink(request, config, request.Url?.ToString()));
}
base.OnActionExecuted(filterContext);
}
private void AsyncTrackLink(HttpRequestBase request, string url)
private void AsyncTrackLink(HttpRequestBase request, Config config, string url)
{
Config config = Config.Load();
Tracking.TrackLink(request, config, url);
}
}

19
Teknik/Filters/TrackPageView.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -21,21 +21,22 @@ namespace Teknik.Filters @@ -21,21 +21,22 @@ namespace Teknik.Filters
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
string title = string.Empty;
if (filterContext.Controller != null && filterContext.Controller.ViewBag != null && filterContext.Controller.ViewBag.Title != null)
Config config = Config.Load();
if (config.PiwikConfig.Enabled)
{
title = filterContext.Controller.ViewBag.Title;
string title = filterContext.Controller?.ViewBag?.Title;
HttpRequestBase request = filterContext.HttpContext.Request;
// Fire and forget. Don't need to wait for it.
Task.Run(() => AsyncTrackPageView(request, config, title));
}
HttpRequestBase request = filterContext.HttpContext.Request;
// Fire and forget. Don't need to wait for it.
Task.Run(() => AsyncTrackPageView(request, title));
base.OnActionExecuted(filterContext);
}
private void AsyncTrackPageView(HttpRequestBase request, string title)
private void AsyncTrackPageView(HttpRequestBase request, Config config, string title)
{
Config config = Config.Load();
Tracking.TrackPageView(request, config, title);
}
}

127
Utilities/Piwik/Tracking.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using Piwik.Tracker;
using Piwik.Tracker;
using System;
using System.Web;
using Teknik.Configuration;
@ -10,58 +10,55 @@ namespace Teknik.Piwik @@ -10,58 +10,55 @@ namespace Teknik.Piwik
{
public static void TrackPageView(HttpRequestBase request, Config config, string title)
{
// Handle Piwik Tracking if enabled
if (config.PiwikConfig.Enabled)
try
{
try
// Follow Do Not Track
string doNotTrack = request.Headers["DNT"];
if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
{
// 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))
{
string sub = request.RequestContext.RouteData.Values["sub"].ToString();
if (string.IsNullOrEmpty(sub))
{
sub = request.Url.AbsoluteUri.GetSubdomain();
}
if (config.DevEnvironment)
{
sub = "dev - " + sub;
}
PiwikTracker.URL = config.PiwikConfig.Url;
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId);
// Get Request Info
string ipAddress = request.ClientIPFromRequest(true);
tracker.setIp(ipAddress);
tracker.setTokenAuth(config.PiwikConfig.TokenAuth);
tracker.setUrl(request.Url.ToString());
tracker.setUserAgent(request.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);
// Get Referral
if (request.UrlReferrer != null)
tracker.setUrlReferrer(request.UrlReferrer.ToString());
// Send the tracking request
tracker.doTrackPageView(string.Format("{0}/{1}", sub, title));
sub = request.Url.AbsoluteUri.GetSubdomain();
}
}
catch (Exception ex)
{
if (config.DevEnvironment)
{
sub = "dev - " + sub;
}
PiwikTracker.URL = config.PiwikConfig.Url;
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId);
// Get Request Info
string ipAddress = request.ClientIPFromRequest(true);
tracker.setIp(ipAddress);
tracker.setTokenAuth(config.PiwikConfig.TokenAuth);
tracker.setUrl(request.Url.ToString());
tracker.setUserAgent(request.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);
// Get Referral
if (request.UrlReferrer != null)
tracker.setUrlReferrer(request.UrlReferrer.ToString());
// Send the tracking request
tracker.doTrackPageView(string.Format("{0}/{1}", sub, title));
}
}
catch (Exception ex)
{
}
}
public static void TrackDownload(HttpRequestBase request, Config config, string url)
@ -76,36 +73,32 @@ namespace Teknik.Piwik @@ -76,36 +73,32 @@ namespace Teknik.Piwik
private static void TrackAction(HttpRequestBase request, Config config, string url, PiwikTracker.ActionType type)
{
// Handle Piwik Tracking if enabled
if (config.PiwikConfig.Enabled)
try
{
try
// Follow Do Not Track
string doNotTrack = request.Headers["DNT"];
if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
{
// 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);
PiwikTracker.URL = config.PiwikConfig.Url;
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId);
tracker.setUserAgent(request.UserAgent);
tracker.setUserAgent(request.UserAgent);
string ipAddress = request.ClientIPFromRequest(true);
string ipAddress = request.ClientIPFromRequest(true);
tracker.setIp(ipAddress);
tracker.setTokenAuth(config.PiwikConfig.TokenAuth);
tracker.setIp(ipAddress);
tracker.setTokenAuth(config.PiwikConfig.TokenAuth);
// Get Referral
if (request.UrlReferrer != null)
tracker.setUrlReferrer(request.UrlReferrer.ToString());
// Get Referral
if (request.UrlReferrer != null)
tracker.setUrlReferrer(request.UrlReferrer.ToString());
tracker.doTrackAction(url, type);
}
tracker.doTrackAction(url, type);
}
catch (Exception ex)
{
}
catch (Exception ex)
{
}
}
}
}

Loading…
Cancel
Save