add ga batching

This commit is contained in:
Kieran 2018-01-12 18:57:57 +08:00
parent 8b214f5170
commit 37a6a22dfc
5 changed files with 60 additions and 7 deletions

View File

@ -3,6 +3,11 @@
include_once('config.php');
include_once('ga.php');
$redis = new Redis();
$redis->pconnect(_REDIS_SERVER);
GAPageView($redis);
$hash = substr($_SERVER["REQUEST_URI"], 1);
$hashKey = $_SERVER['REMOTE_ADDR'] . ':' . $hash;
@ -49,9 +54,6 @@
}
}
$redis = new Redis();
$redis->pconnect(_REDIS_SERVER);
$dlCounter = $redis->get($hashKey);
if($dlCounter != FALSE) {
if($dlCounter >= _DL_CAPTCHA * 2){
@ -84,7 +86,6 @@
header('Content-Disposition: inline; filename="' . $filename . '"');
if(!$isCrawlBot && $range_start == 0){
GAPageView();
$db->AddView($f->hash160);
}
$redis->incr($hashKey);

View File

@ -18,15 +18,20 @@
curl_close ($ch);
}
function GAPageView(){
GACollect(array(
function GAPageView($redis){
$msg = http_build_query(array(
"v" => "1",
"tid" => _GA_SITE_CODE,
"cid" => session_id(),
"t" => "pageview",
"dh" => $_SERVER['HTTP_HOST'],
"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"] : ""),
"dr" => urlencode(isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "")
));
$redis->publish('ga-page-view', $msg);
}
function GAEvent($cat, $act) {

3
utils/ga_pageview/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target/
**/*.rs.bk

View File

@ -0,0 +1,7 @@
[package]
name = "ga_pageview"
version = "0.1.0"
authors = ["v0l"]
[dependencies]
redis = "0.8.0"

View 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");
}
}