1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added Piwik tracking to the server side.

This commit is contained in:
Uncled1023 2016-02-25 16:41:31 -08:00
parent 4cd88265b7
commit 26b9284f77
12 changed files with 235 additions and 19 deletions

View File

@ -29,7 +29,9 @@ namespace Teknik.Areas.API.Controllers
[AllowAnonymous]
public ActionResult Upload(HttpPostedFileWrapper file, string contentType = null, bool encrypt = false, bool saveKey = false, string key = null, int keySize = 0, string iv = null, int blockSize = 0, bool genDeletionKey = false)
{
try {
try
{
Tracking.TrackPageView(Request, "Upload", Subdomain);
if (file != null)
{
if (file.ContentLength <= Config.UploadConfig.MaxUploadSize)
@ -129,6 +131,7 @@ namespace Teknik.Areas.API.Controllers
{
try
{
Tracking.TrackPageView(Request, "Paste", Subdomain);
Paste.Models.Paste paste = PasteHelper.CreatePaste(code, title, syntax, expireUnit, expireLength, password, hide);
db.Pastes.Add(paste);
@ -155,29 +158,37 @@ namespace Teknik.Areas.API.Controllers
public ActionResult Shorten(string url)
{
if (url.IsValidUrl())
try
{
ShortenedUrl newUrl = Shortener.Shortener.ShortenUrl(url, Config.ShortenerConfig.UrlLength);
db.ShortenedUrls.Add(newUrl);
db.SaveChanges();
string shortUrl = string.Format("{0}://{1}/{2}", HttpContext.Request.Url.Scheme, Config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
if (Config.DevEnvironment)
Tracking.TrackPageView(Request, "Shorten", Subdomain);
if (url.IsValidUrl())
{
shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
}
ShortenedUrl newUrl = Shortener.Shortener.ShortenUrl(url, Config.ShortenerConfig.UrlLength);
return Json(new
{
result = new
db.ShortenedUrls.Add(newUrl);
db.SaveChanges();
string shortUrl = string.Format("{0}://{1}/{2}", HttpContext.Request.Url.Scheme, Config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
if (Config.DevEnvironment)
{
shortUrl = shortUrl,
originalUrl = url
shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
}
});
return Json(new
{
result = new
{
shortUrl = shortUrl,
originalUrl = url
}
});
}
return Json(new { error = new { message = "Must be a valid Url" } });
}
catch (Exception ex)
{
return Json(new { error = new { message = "Exception: " + ex.Message } });
}
return Json(new { error = new { message = "Must be a valid Url" } });
}
}
}

View File

@ -45,6 +45,11 @@
<ul>
<li><var>TeknikAuth</var> - Used for user authentication.</li>
</ul>
<h2>Analytics</h2>
<p>
We use <a href="http://piwik.org/">Piwik</a> to track user interaction with the site. This will store the first 2 bytes of your IP Address (e.g. 192.168.xxx.xxx) as an identifier.
</p>
<iframe style="border: 0; height: 200px; width: 600px;" src="https://stats.teknik.io/index.php?module=CoreAdminHome&action=optOut&language=en"></iframe>
</div>
</div>
</div>

View File

@ -1,4 +1,5 @@
using System;
using Piwik.Tracker;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
@ -145,6 +146,9 @@ namespace Teknik.Areas.Upload.Controllers
Response.AppendHeader("Content-Disposition", cd.ToString());
// Handle Piwik Tracking if enabled
Tracking.TrackPageView(Request, ViewBag.Title, Subdomain);
return File(data, upload.ContentType);
}
}

View File

@ -34,6 +34,7 @@ namespace Teknik.Configuration
private StreamConfig _StreamConfig;
private ShortenerConfig _ShortenerConfig;
private DatabaseConfig _DatabaseConfig;
private PiwikConfig _PiwikConfig;
public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } }
public bool Migrate { get { return _Migrate; } set { _Migrate = value; } }
@ -84,6 +85,9 @@ namespace Teknik.Configuration
// Database Configuration
public DatabaseConfig DatabaseConfig { get { return _DatabaseConfig; } set { _DatabaseConfig = value; } }
// Piwik Configuration
public PiwikConfig PiwikConfig { get { return _PiwikConfig; } set { _PiwikConfig = value; } }
public Config()
{
_ConfigRWLock = new ReaderWriterLockSlim();
@ -118,6 +122,7 @@ namespace Teknik.Configuration
StreamConfig = new StreamConfig();
ShortenerConfig = new ShortenerConfig();
DatabaseConfig = new DatabaseConfig();
PiwikConfig = new PiwikConfig();
}
public static Config Deserialize(string text)

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teknik.Configuration
{
public class PiwikConfig
{
public bool Enabled { get; set; }
public string Url { get; set; }
public int SiteId { get; set; }
public PiwikConfig()
{
Enabled = false;
Url = string.Empty;
SiteId = 1;
}
}
}

View File

@ -8,8 +8,12 @@ using Teknik.Areas.Error.Controllers;
using Teknik.Areas.Error.ViewModels;
using Teknik.Configuration;
using Piwik.Tracker;
using Teknik.Filters;
namespace Teknik.Controllers
{
[TrackingFilter]
public class DefaultController : Controller
{
private Config _config;

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using Teknik.Configuration;
using Teknik.Helpers;
namespace Teknik.Filters
{
public class TrackingFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Config config = Config.Load();
if (config.PiwikConfig.Enabled)
{
try
{
string sub = filterContext.HttpContext.Request.RequestContext.RouteData.Values["sub"].ToString();
if (string.IsNullOrEmpty(sub))
{
sub = filterContext.HttpContext.Request.Url.AbsoluteUri.GetSubdomain();
}
string title = config.Title;
Page page = filterContext.HttpContext.Handler as Page;
if (page != null)
{
title = page.Title;
}
Tracking.TrackPageView(filterContext.HttpContext.Request, title, sub);
}
catch (Exception ex)
{
}
}
base.OnActionExecuted(filterContext);
}
}
}

View File

@ -16,6 +16,10 @@ using Teknik.Areas.Error.Controllers;
using System.Web.Helpers;
using System.Diagnostics;
using System.Collections.Specialized;
using Teknik.Configuration;
using Piwik.Tracker;
using System.Web.UI;
using Teknik.Helpers;
namespace Teknik
{
@ -62,6 +66,33 @@ namespace Teknik
{
context.Response.AppendHeader("Access-Control-Allow-Origin", origin);
}
//// Handle Piwik Tracking if enabled
//Config config = Config.Load();
//if (config.PiwikConfig.Enabled)
//{
// try
// {
// string sub = context.Request.RequestContext.RouteData.Values["sub"].ToString();
// if (string.IsNullOrEmpty(sub))
// {
// sub = context.Request.Url.AbsoluteUri.GetSubdomain();
// }
// string title = config.Title;
// Page page = HttpContext.Current.Handler as Page;
// if (page != null)
// {
// title = page.Title;
// }
// var newContext = ((HttpApplication)sender).Context;
// Tracking.TrackPageView(new HttpRequestWrapper(newContext.Request), title, sub);
// }
// catch (Exception ex)
// {
// }
//}
}
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Teknik.Helpers
{
public static class HttpRequestExtensions
{
public static string GetClientIpAddress(this HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return IPAddress.Parse(((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress).ToString();
}
return null;
}
}
}

View File

@ -0,0 +1,47 @@
using Piwik.Tracker;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Teknik.Configuration;
namespace Teknik.Helpers
{
public static class Tracking
{
public static void TrackPageView(HttpRequestBase request, string title, string sub)
{
Config config = Config.Load();
// Handle Piwik Tracking if enabled
if (config.PiwikConfig.Enabled)
{
try
{
PiwikTracker.URL = config.PiwikConfig.Url;
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId);
tracker.setForceVisitDateTime(DateTime.Now);
tracker.setUserAgent(request.UserAgent);
tracker.setResolution(request.Browser.ScreenPixelsWidth, request.Browser.ScreenPixelsHeight);
tracker.setBrowserHasCookies(request.Browser.Cookies);
string ipAddress = request.UserHostAddress;
tracker.setIp(ipAddress);
tracker.setUrl(request.Url.ToString());
tracker.setUrlReferrer(request.UrlReferrer.ToString());
tracker.doTrackPageView(string.Format("{0} / {1}", sub, title));
}
catch (Exception ex)
{
}
}
}
}
}

View File

@ -80,6 +80,10 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Piwik.Tracker, Version=2.8.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Piwik.Tracker.2.8.0.0\lib\net40\Piwik.Tracker.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationFramework" />
<Reference Include="RouteDebugger, Version=2.1.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\routedebugger.2.1.4.0\lib\net40\RouteDebugger.dll</HintPath>
@ -231,6 +235,7 @@
<Compile Include="Areas\Upload\ViewModels\DeleteViewModel.cs" />
<Compile Include="Areas\Upload\ViewModels\DownloadViewModel.cs" />
<Compile Include="Areas\Upload\ViewModels\UploadViewModel.cs" />
<Compile Include="Configuration\PiwikConfig.cs" />
<Compile Include="Configuration\ShortenerConfig.cs" />
<Compile Include="Configuration\StreamConfig.cs" />
<Compile Include="Configuration\ApiConfig.cs" />
@ -247,6 +252,7 @@
<Compile Include="Configuration\UserConfig.cs" />
<Compile Include="Controllers\DefaultController.cs" />
<Compile Include="Areas\Dev\Controllers\DevController.cs" />
<Compile Include="Filters\TrackingFilterAttribute.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
@ -257,9 +263,11 @@
<Compile Include="Areas\Profile\Models\PermissionType.cs" />
<Compile Include="Areas\Blog\Models\BlogPost.cs" />
<Compile Include="Areas\Profile\Models\Role.cs" />
<Compile Include="Helpers\HttpRequestExtensions.cs" />
<Compile Include="Helpers\MysqlDatabase.cs" />
<Compile Include="Helpers\MarkdownHelper.cs" />
<Compile Include="Helpers\RSSFeedResult.cs" />
<Compile Include="Helpers\Tracking.cs" />
<Compile Include="Helpers\UrlExtensions.cs" />
<Compile Include="Helpers\Utility.cs" />
<Compile Include="Helpers\WebClientExtension.cs">

View File

@ -25,6 +25,7 @@
<package id="Modernizr" version="2.8.3" targetFramework="net452" userInstalled="true" />
<package id="MySql.Data" version="6.9.8" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" userInstalled="true" />
<package id="Piwik.Tracker" version="2.8.0.0" targetFramework="net452" />
<package id="Respond" version="1.4.2" targetFramework="net452" userInstalled="true" />
<package id="routedebugger" version="2.1.4" targetFramework="net452" userInstalled="true" />
<package id="WebGrease" version="1.6.0" targetFramework="net46" userInstalled="true" />