Browse Source

Modified tracking to not pass in any volatile objects.

tags/3.0.0
Teknikode 2 years ago
parent
commit
782aba9d22
3 changed files with 52 additions and 21 deletions
  1. 17
    3
      Teknik/Filters/TrackDownload.cs
  2. 17
    3
      Teknik/Filters/TrackLink.cs
  3. 18
    15
      Utilities/Piwik/Tracking.cs

+ 17
- 3
Teknik/Filters/TrackDownload.cs View File

@@ -26,16 +26,30 @@ namespace Teknik.Filters
if (config.PiwikConfig.Enabled)
{
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);

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

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

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

base.OnActionExecuted(filterContext);
}

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

+ 17
- 3
Teknik/Filters/TrackLink.cs View File

@@ -7,6 +7,7 @@ using System.Web;
using System.Web.Mvc;
using Teknik.Configuration;
using Teknik.Piwik;
using Teknik.Utilities;

namespace Teknik.Filters
{
@@ -25,16 +26,29 @@ namespace Teknik.Filters
{
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);

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

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

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

base.OnActionExecuted(filterContext);
}

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

+ 18
- 15
Utilities/Piwik/Tracking.cs View File

@@ -51,6 +51,9 @@ namespace Teknik.Piwik
if (request.UrlReferrer != null)
tracker.SetUrlReferrer(request.UrlReferrer.ToString());

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

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

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

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

private static void TrackAction(HttpRequestBase request, Config config, string url, ActionType type)
private static void TrackAction(ActionType type, bool dnt, int siteId, string siteUrl, string userAgent, string clientIp, string token, string url, string urlReferrer)
{
try
{
// Follow Do Not Track
string doNotTrack = request.Headers["DNT"];
if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
if (dnt)
{
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId, config.PiwikConfig.Url);

tracker.SetUserAgent(request.UserAgent);
PiwikTracker tracker = new PiwikTracker(siteId, siteUrl);

string ipAddress = request.ClientIPFromRequest(true);
tracker.SetUserAgent(userAgent);

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

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

if (!string.IsNullOrEmpty(url))
tracker.SetUrl(url);

tracker.DoTrackAction(url, type);
}

Loading…
Cancel
Save