track stats with matomo

This commit is contained in:
Kieran
2018-09-27 19:19:59 +08:00
parent 057e64fc44
commit 245055d8ad
7 changed files with 97 additions and 34 deletions

6
.gitignore vendored
View File

@@ -2,4 +2,8 @@ out/
*.xml *.xml
src/php/config.php src/php/config.php
google*.html google*.html
bower_components/ bower_components/
.vs
bin/
obj/
*.csproj.user

View File

@@ -83,13 +83,11 @@
$dlCounter = $redis->get($hashKey); $dlCounter = $redis->get($hashKey);
if($dlCounter != FALSE) { if($dlCounter != FALSE) {
if($dlCounter >= _DL_CAPTCHA * 2){ if($dlCounter >= _DL_CAPTCHA * 2){
/*$cfbk = 'VC:CF:BLOCK'; $cfbk = 'VC:CF:BLOCK';
if(_CLOUDFLARE_API_KEY != 'API_KEY' && $redis->sIsMember($cfbk, _UIP) == False){ if(_CLOUDFLARE_API_KEY != 'API_KEY' && $redis->sIsMember($cfbk, _UIP) == False){
$redis->sadd($cfbk, _UIP); $redis->sadd($cfbk, _UIP);
include_once('cloudflare.php');
AddFirewallRule(_UIP); AddFirewallRule(_UIP);
}*/ }
header('location: /');
exit(); exit();
}else if($dlCounter >= _DL_CAPTCHA){ }else if($dlCounter >= _DL_CAPTCHA){
//redirect for captcha check //redirect for captcha check

View File

@@ -200,6 +200,9 @@
} }
function ga_page_view($redis){ function ga_page_view($redis){
matomo_page_view($redis);
return;
$msg = http_build_query(array( $msg = http_build_query(array(
"v" => "1", "v" => "1",
"tid" => _GA_SITE_CODE, "tid" => _GA_SITE_CODE,
@@ -215,6 +218,21 @@
$redis->publish('ga-page-view', $msg); $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) { function ga_event($cat, $act) {
ga_collect(array( ga_collect(array(
"t" => "event", "t" => "event",

View File

@@ -1,4 +1,5 @@
using StackExchange.Redis; using Newtonsoft.Json;
using StackExchange.Redis;
using System; using System;
using System.IO; using System.IO;
using System.Net; using System.Net;
@@ -9,61 +10,73 @@ namespace ga_page_view
{ {
class Program class Program
{ {
static ConnectionMultiplexer c { get; set; }
static BatchBlock<string> _queue = new BatchBlock<string>(20); static BatchBlock<string> _queue = new BatchBlock<string>(20);
static string Token { get; set; }
static void Main(string[] args) static Task Main(string[] args)
{ {
var mt = startSvc(); if (args.Length != 1)
mt.Wait(); {
Console.WriteLine("Required args: token_auth");
return Task.CompletedTask;
}
Token = args[0];
Console.WriteLine($"Token is: {Token}");
return startSvc();
} }
private static async Task startSvc() private static async Task startSvc()
{ {
var c = await ConnectionMultiplexer.ConnectAsync("localhost"); c = await ConnectionMultiplexer.ConnectAsync("localhost");
await c.GetSubscriber().SubscribeAsync("ga-page-view", queueMsg); await c.GetSubscriber().SubscribeAsync("ga-page-view-matomo", queueMsg);
Console.WriteLine("Connected to redis"); Console.WriteLine("Connected to redis");
await sendStats();
_queue.LinkTo(new ActionBlock<string[]>(async (r) =>
{
Console.WriteLine("Sending stats");
await SendData(r);
}));
await _queue.Completion;
} }
private static void queueMsg(RedisChannel a, RedisValue b) private static void queueMsg(RedisChannel a, RedisValue b)
{ {
_queue.Post(b.ToString()); try
}
private static async Task sendStats()
{
while (true)
{ {
var r = await _queue.ReceiveAsync(); Console.Write("."); //tick
if (r != null) _queue.Post(b.ToString());
{ }
Console.WriteLine("Sending stats.."); catch (Exception ex)
await SendData(r); {
} Console.WriteLine($"Queue msg failed.. {ex.ToString()}");
} }
} }
private static async Task SendData(string[] payload) private static async Task SendData(string[] payload)
{ {
try 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"; req.Method = "POST";
using (StreamWriter sw = new StreamWriter(await req.GetRequestStreamAsync())) 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()) 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())) var rsp_json = await sr.ReadToEndAsync();
{ Console.WriteLine($"Got reponse from analytics: {rsp_json}");
Console.WriteLine($"Got error reponse from analytics: {await sr.ReadToEndAsync()}");
}
} }
} }
} }
@@ -73,4 +86,10 @@ namespace ga_page_view
} }
} }
} }
internal class BulkStats
{
public string[] requests { get; set; }
public string token_auth { get; set; }
}
} }

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp2.1</TargetFramework>
<PublishDir>bin\Release\netcoreapp2.1\publish\</PublishDir>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<_IsPortable>true</_IsPortable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>

View File

@@ -2,11 +2,13 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="StackExchange.Redis" Version="1.2.6" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="StackExchange.Redis" Version="2.0.510" />
</ItemGroup> </ItemGroup>
</Project> </Project>