mirror of
https://git.v0l.io/Kieran/void.cat.git
synced 2025-09-28 23:02:47 +02:00
add ga batching
This commit is contained in:
@@ -3,6 +3,11 @@
|
|||||||
include_once('config.php');
|
include_once('config.php');
|
||||||
include_once('ga.php');
|
include_once('ga.php');
|
||||||
|
|
||||||
|
$redis = new Redis();
|
||||||
|
$redis->pconnect(_REDIS_SERVER);
|
||||||
|
|
||||||
|
GAPageView($redis);
|
||||||
|
|
||||||
$hash = substr($_SERVER["REQUEST_URI"], 1);
|
$hash = substr($_SERVER["REQUEST_URI"], 1);
|
||||||
$hashKey = $_SERVER['REMOTE_ADDR'] . ':' . $hash;
|
$hashKey = $_SERVER['REMOTE_ADDR'] . ':' . $hash;
|
||||||
|
|
||||||
@@ -49,9 +54,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$redis = new Redis();
|
|
||||||
$redis->pconnect(_REDIS_SERVER);
|
|
||||||
|
|
||||||
$dlCounter = $redis->get($hashKey);
|
$dlCounter = $redis->get($hashKey);
|
||||||
if($dlCounter != FALSE) {
|
if($dlCounter != FALSE) {
|
||||||
if($dlCounter >= _DL_CAPTCHA * 2){
|
if($dlCounter >= _DL_CAPTCHA * 2){
|
||||||
@@ -84,7 +86,6 @@
|
|||||||
header('Content-Disposition: inline; filename="' . $filename . '"');
|
header('Content-Disposition: inline; filename="' . $filename . '"');
|
||||||
|
|
||||||
if(!$isCrawlBot && $range_start == 0){
|
if(!$isCrawlBot && $range_start == 0){
|
||||||
GAPageView();
|
|
||||||
$db->AddView($f->hash160);
|
$db->AddView($f->hash160);
|
||||||
}
|
}
|
||||||
$redis->incr($hashKey);
|
$redis->incr($hashKey);
|
||||||
|
@@ -18,15 +18,20 @@
|
|||||||
curl_close ($ch);
|
curl_close ($ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
function GAPageView(){
|
function GAPageView($redis){
|
||||||
GACollect(array(
|
$msg = http_build_query(array(
|
||||||
|
"v" => "1",
|
||||||
|
"tid" => _GA_SITE_CODE,
|
||||||
|
"cid" => session_id(),
|
||||||
"t" => "pageview",
|
"t" => "pageview",
|
||||||
"dh" => $_SERVER['HTTP_HOST'],
|
"dh" => $_SERVER['HTTP_HOST'],
|
||||||
"dp" => urlencode($_SERVER['REQUEST_URI']),
|
"dp" => urlencode($_SERVER['REQUEST_URI']),
|
||||||
"uip" => $_SERVER['REMOTE_ADDR'],
|
"uip" => isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'],
|
||||||
"ua" => urlencode(isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : ""),
|
"ua" => urlencode(isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : ""),
|
||||||
"dr" => urlencode(isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "")
|
"dr" => urlencode(isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "")
|
||||||
));
|
));
|
||||||
|
|
||||||
|
$redis->publish('ga-page-view', $msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
function GAEvent($cat, $act) {
|
function GAEvent($cat, $act) {
|
||||||
|
3
utils/ga_pageview/.gitignore
vendored
Normal file
3
utils/ga_pageview/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
/target/
|
||||||
|
**/*.rs.bk
|
7
utils/ga_pageview/Cargo.toml
Normal file
7
utils/ga_pageview/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "ga_pageview"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["v0l"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
redis = "0.8.0"
|
37
utils/ga_pageview/src/main.rs
Normal file
37
utils/ga_pageview/src/main.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
extern crate redis;
|
||||||
|
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let ch = "ga-page-view";
|
||||||
|
let mut q = Vec::new();
|
||||||
|
|
||||||
|
if let Ok(client) = redis::Client::open("redis://127.0.0.1/") {
|
||||||
|
if let Ok(mut pubsub) = client.get_pubsub() {
|
||||||
|
if let Ok(_) = pubsub.subscribe(ch) {
|
||||||
|
println!("Subscribed to {}", ch);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if let Ok(msg) = pubsub.get_message() {
|
||||||
|
if let Ok(payload) = msg.get_payload::<String>() {
|
||||||
|
//println!("channel '{}': {}", msg.get_channel_name(), payload);
|
||||||
|
|
||||||
|
if q.len() >= 20 {
|
||||||
|
//push the rows to ga /batch
|
||||||
|
Command::new("curl").arg("-X").arg("POST").arg("--data").arg(q.join(" \\\r\n")).arg("https://www.google-analytics.com/batch").output().expect("failed to execute process");
|
||||||
|
q.clear();
|
||||||
|
}
|
||||||
|
q.push(payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
println!("Failed to subscribe");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
println!("Failed to get pubsub");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
println!("Failed to connect to redis");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user