2018-06-18 06:57:44 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Piwik.Tracker;
|
2018-06-15 02:57:03 +02:00
|
|
|
using System;
|
|
|
|
using System.Web;
|
|
|
|
using Teknik.Configuration;
|
|
|
|
using Teknik.Utilities;
|
|
|
|
|
2018-06-17 08:02:26 +02:00
|
|
|
namespace Teknik.Tracking
|
2018-06-15 02:57:03 +02:00
|
|
|
{
|
|
|
|
public static class Tracking
|
|
|
|
{
|
2018-06-18 06:57:44 +02:00
|
|
|
public static void TrackPageView(HttpContext context, Config config, string title, string sub, string clientIp, string url, string urlReferrer, string userAgent, int pixelWidth, int pixelHeight, bool hasCookies, string acceptLang, bool hasJava)
|
2018-06-15 02:57:03 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (config.PiwikConfig.Enabled)
|
|
|
|
{
|
|
|
|
if (config.DevEnvironment)
|
|
|
|
{
|
|
|
|
sub = "dev - " + sub;
|
|
|
|
}
|
2018-06-18 06:57:44 +02:00
|
|
|
|
|
|
|
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId, config.PiwikConfig.Url, context);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
// Set Request Info
|
|
|
|
tracker.SetIp(clientIp);
|
|
|
|
tracker.SetTokenAuth(config.PiwikConfig.TokenAuth);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
tracker.SetUserAgent(userAgent);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
// Set browser info
|
|
|
|
tracker.SetResolution(pixelWidth, pixelHeight);
|
|
|
|
tracker.SetBrowserHasCookies(hasCookies);
|
|
|
|
if (!string.IsNullOrEmpty(acceptLang))
|
|
|
|
tracker.SetBrowserLanguage(acceptLang);
|
|
|
|
tracker.SetPlugins(new BrowserPlugins { Java = hasJava });
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
// Get Referral
|
|
|
|
if (!string.IsNullOrEmpty(urlReferrer))
|
|
|
|
tracker.SetUrlReferrer(urlReferrer);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
|
|
tracker.SetUrl(url);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
// Send the tracking request
|
|
|
|
tracker.DoTrackPageView(string.Format("{0}/{1}", sub, title));
|
2018-06-15 02:57:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-17 02:27:06 +02:00
|
|
|
catch (Exception)
|
2018-06-15 02:57:03 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
public static void TrackDownload(HttpContext context, Config config, string userAgent, string clientIp, string url, string urlReferrer)
|
2018-06-15 02:57:03 +02:00
|
|
|
{
|
2018-06-18 06:57:44 +02:00
|
|
|
TrackAction(context, config, ActionType.Download, userAgent, clientIp, url, urlReferrer);
|
2018-06-15 02:57:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
public static void TrackLink(HttpContext context, Config config, string userAgent, string clientIp, string url, string urlReferrer)
|
2018-06-15 02:57:03 +02:00
|
|
|
{
|
2018-06-18 06:57:44 +02:00
|
|
|
TrackAction(context, config, ActionType.Link, userAgent, clientIp, url, urlReferrer);
|
2018-06-15 02:57:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
private static void TrackAction(HttpContext context, Config config, ActionType type, string userAgent, string clientIp, string url, string urlReferrer)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (config.PiwikConfig.Enabled)
|
|
|
|
{
|
|
|
|
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId, config.PiwikConfig.Url, context);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
tracker.SetUserAgent(userAgent);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
tracker.SetIp(clientIp);
|
|
|
|
tracker.SetTokenAuth(config.PiwikConfig.TokenAuth);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
// Get Referral
|
|
|
|
if (!string.IsNullOrEmpty(urlReferrer))
|
|
|
|
tracker.SetUrlReferrer(urlReferrer);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
|
|
tracker.SetUrl(url);
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
tracker.DoTrackAction(url, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-06-15 02:57:03 +02:00
|
|
|
|
2018-06-18 06:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 02:57:03 +02:00
|
|
|
}
|
|
|
|
}
|