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

75 lines
2.6 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Http;
using Piwik.Tracker;
using System;
using System.Web;
using Teknik.Configuration;
using Teknik.Utilities;
namespace Teknik.Tracking
{
public static class Tracking
{
2022-05-24 08:45:40 +02:00
public static void TrackPageView(string token, int siteId, string apiUrl, string title, string sub, string clientIp, string url, string urlReferrer, string userAgent, int pixelWidth, int pixelHeight, bool hasCookies, string acceptLang, bool hasJava, bool isDev)
{
2022-05-24 08:45:40 +02:00
if (isDev)
{
2022-05-24 08:45:40 +02:00
sub = "dev - " + sub;
}
2022-05-24 08:45:40 +02:00
PiwikTracker tracker = new PiwikTracker(siteId, apiUrl);
// Set Request Info
tracker.SetIp(clientIp);
tracker.SetTokenAuth(token);
tracker.SetUserAgent(userAgent);
// 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 (!string.IsNullOrEmpty(urlReferrer))
tracker.SetUrlReferrer(urlReferrer);
if (!string.IsNullOrEmpty(url))
tracker.SetUrl(url);
// Send the tracking request
tracker.DoTrackPageView(string.Format("{0}/{1}", sub, title));
}
2022-05-24 08:45:40 +02:00
public static void TrackDownload(string token, int siteId, string apiUrl, string userAgent, string clientIp, string url, string urlReferrer)
{
2022-05-24 08:45:40 +02:00
TrackAction(token, siteId, apiUrl, ActionType.Download, userAgent, clientIp, url, urlReferrer);
}
2022-05-24 08:45:40 +02:00
public static void TrackLink(string token, int siteId, string apiUrl, string userAgent, string clientIp, string url, string urlReferrer)
{
2022-05-24 08:45:40 +02:00
TrackAction(token, siteId, apiUrl, ActionType.Link, userAgent, clientIp, url, urlReferrer);
}
2022-05-24 08:45:40 +02:00
private static void TrackAction(string token, int siteId, string apiUrl, ActionType type, string userAgent, string clientIp, string url, string urlReferrer)
{
2022-05-24 08:45:40 +02:00
PiwikTracker tracker = new PiwikTracker(siteId, apiUrl);
2022-05-24 08:45:40 +02:00
tracker.SetUserAgent(userAgent);
2022-05-24 08:45:40 +02:00
tracker.SetIp(clientIp);
tracker.SetTokenAuth(token);
2022-05-24 08:45:40 +02:00
// Get Referral
if (!string.IsNullOrEmpty(urlReferrer))
tracker.SetUrlReferrer(urlReferrer);
2022-05-24 08:45:40 +02:00
if (!string.IsNullOrEmpty(url))
tracker.SetUrl(url);
2022-05-24 08:45:40 +02:00
tracker.DoTrackAction(url, type);
}
}
}