From 245055d8add5af716cece5bea9d47ffc162c7f9f Mon Sep 17 00:00:00 2001 From: Kieran Date: Thu, 27 Sep 2018 19:19:59 +0800 Subject: [PATCH] track stats with matomo --- .gitignore | 6 +- src/php/download.php | 6 +- src/php/functions.php | 18 +++++ utils/ga-page-view/ga-page-view/Program.cs | 73 ++++++++++++------- .../PublishProfiles/FolderProfile.pubxml | 16 ++++ .../PublishProfiles/FolderProfile.pubxml.user | 6 ++ .../ga-page-view/ga-page-view.csproj | 6 +- 7 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 utils/ga-page-view/ga-page-view/Properties/PublishProfiles/FolderProfile.pubxml create mode 100644 utils/ga-page-view/ga-page-view/Properties/PublishProfiles/FolderProfile.pubxml.user diff --git a/.gitignore b/.gitignore index 9b52137..33891fd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,8 @@ out/ *.xml src/php/config.php google*.html -bower_components/ \ No newline at end of file +bower_components/ +.vs +bin/ +obj/ +*.csproj.user \ No newline at end of file diff --git a/src/php/download.php b/src/php/download.php index 61e7b5b..416db98 100644 --- a/src/php/download.php +++ b/src/php/download.php @@ -83,13 +83,11 @@ $dlCounter = $redis->get($hashKey); if($dlCounter != FALSE) { if($dlCounter >= _DL_CAPTCHA * 2){ - /*$cfbk = 'VC:CF:BLOCK'; + $cfbk = 'VC:CF:BLOCK'; if(_CLOUDFLARE_API_KEY != 'API_KEY' && $redis->sIsMember($cfbk, _UIP) == False){ $redis->sadd($cfbk, _UIP); - include_once('cloudflare.php'); AddFirewallRule(_UIP); - }*/ - header('location: /'); + } exit(); }else if($dlCounter >= _DL_CAPTCHA){ //redirect for captcha check diff --git a/src/php/functions.php b/src/php/functions.php index 8908d03..00fe1e9 100644 --- a/src/php/functions.php +++ b/src/php/functions.php @@ -200,6 +200,9 @@ } function ga_page_view($redis){ + matomo_page_view($redis); + return; + $msg = http_build_query(array( "v" => "1", "tid" => _GA_SITE_CODE, @@ -215,6 +218,21 @@ $redis->publish('ga-page-view', $msg); } + function matomo_page_view($redis){ + $msg = "?" . http_build_query(array( + "idsite" => 1, + "rec" => 1, + "apiv" => 1, + "_id" => isset($_COOKIE["VC:UID"]) ? $_COOKIE["VC:UID"] : uniqid(), + "url" => "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", + "cip" => _UIP, + "ua" => isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : "", + "urlref" => isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "" + )); + + $redis->publish('ga-page-view-matomo', $msg); + } + function ga_event($cat, $act) { ga_collect(array( "t" => "event", diff --git a/utils/ga-page-view/ga-page-view/Program.cs b/utils/ga-page-view/ga-page-view/Program.cs index b3da33e..f341c77 100644 --- a/utils/ga-page-view/ga-page-view/Program.cs +++ b/utils/ga-page-view/ga-page-view/Program.cs @@ -1,4 +1,5 @@ -using StackExchange.Redis; +using Newtonsoft.Json; +using StackExchange.Redis; using System; using System.IO; using System.Net; @@ -9,61 +10,73 @@ namespace ga_page_view { class Program { + static ConnectionMultiplexer c { get; set; } static BatchBlock _queue = new BatchBlock(20); + static string Token { get; set; } - static void Main(string[] args) + static Task Main(string[] args) { - var mt = startSvc(); - mt.Wait(); + if (args.Length != 1) + { + Console.WriteLine("Required args: token_auth"); + return Task.CompletedTask; + } + + Token = args[0]; + Console.WriteLine($"Token is: {Token}"); + return startSvc(); } private static async Task startSvc() { - var c = await ConnectionMultiplexer.ConnectAsync("localhost"); - await c.GetSubscriber().SubscribeAsync("ga-page-view", queueMsg); + c = await ConnectionMultiplexer.ConnectAsync("localhost"); + await c.GetSubscriber().SubscribeAsync("ga-page-view-matomo", queueMsg); Console.WriteLine("Connected to redis"); - await sendStats(); + + _queue.LinkTo(new ActionBlock(async (r) => + { + Console.WriteLine("Sending stats"); + await SendData(r); + })); + + await _queue.Completion; } private static void queueMsg(RedisChannel a, RedisValue b) { - _queue.Post(b.ToString()); - } - - private static async Task sendStats() - { - while (true) + try { - var r = await _queue.ReceiveAsync(); - if (r != null) - { - Console.WriteLine("Sending stats.."); - await SendData(r); - } + Console.Write("."); //tick + _queue.Post(b.ToString()); + } + catch (Exception ex) + { + Console.WriteLine($"Queue msg failed.. {ex.ToString()}"); } } - private static async Task SendData(string[] payload) { try { - var req = (HttpWebRequest)WebRequest.Create("https://www.google-analytics.com/batch"); + var req = (HttpWebRequest)WebRequest.Create("https://matomo.trash.lol/piwik.php"); req.Method = "POST"; using (StreamWriter sw = new StreamWriter(await req.GetRequestStreamAsync())) { - await sw.WriteAsync(string.Join("\r\n", payload)); + await sw.WriteAsync(JsonConvert.SerializeObject(new BulkStats + { + requests = payload, + token_auth = Token + })); } using (var rsp = (HttpWebResponse)await req.GetResponseAsync()) { - if (rsp.StatusCode != HttpStatusCode.OK) + using (StreamReader sr = new StreamReader(rsp.GetResponseStream())) { - using (StreamReader sr = new StreamReader(rsp.GetResponseStream())) - { - Console.WriteLine($"Got error reponse from analytics: {await sr.ReadToEndAsync()}"); - } + var rsp_json = await sr.ReadToEndAsync(); + Console.WriteLine($"Got reponse from analytics: {rsp_json}"); } } } @@ -73,4 +86,10 @@ namespace ga_page_view } } } + + internal class BulkStats + { + public string[] requests { get; set; } + public string token_auth { get; set; } + } } \ No newline at end of file diff --git a/utils/ga-page-view/ga-page-view/Properties/PublishProfiles/FolderProfile.pubxml b/utils/ga-page-view/ga-page-view/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..8989bd8 --- /dev/null +++ b/utils/ga-page-view/ga-page-view/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,16 @@ + + + + + FileSystem + Release + Any CPU + netcoreapp2.1 + bin\Release\netcoreapp2.1\publish\ + linux-x64 + false + <_IsPortable>true + + \ No newline at end of file diff --git a/utils/ga-page-view/ga-page-view/Properties/PublishProfiles/FolderProfile.pubxml.user b/utils/ga-page-view/ga-page-view/Properties/PublishProfiles/FolderProfile.pubxml.user new file mode 100644 index 0000000..312c6e3 --- /dev/null +++ b/utils/ga-page-view/ga-page-view/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/utils/ga-page-view/ga-page-view/ga-page-view.csproj b/utils/ga-page-view/ga-page-view/ga-page-view.csproj index dff66c8..d21fd5f 100644 --- a/utils/ga-page-view/ga-page-view/ga-page-view.csproj +++ b/utils/ga-page-view/ga-page-view/ga-page-view.csproj @@ -2,11 +2,13 @@ Exe - netcoreapp2.0 + netcoreapp2.1 + latest - + + \ No newline at end of file