Skip to content

Instantly share code, notes, and snippets.

@neoFelhz neoFelhz/ga.js

Last active Aug 31, 2018
Embed
What would you like to do?
Google Analytics 异步加载方案(PHP)
// ********************
// * Author: JerryQu
// * https://imququ.com
// *********************
(function(window, document, navigator, location) {
var screen = window.screen;
var encode = encodeURIComponent;
var data = [
'dt=' + encode(document.title),
'dr=' + encode(document.referrer),
'ul=' + (navigator.language || navigator.browserLanguage),
'sd=' + screen.colorDepth + '-bit',
'sr=' + screen.width + 'x' + screen.height,
'_=' + (+new Date)
];
var query = '?' + data.join('&');
window.__beacon_img = new Image();
window.__beacon_img.src = '/ga.php' + query;
})(window, document, navigator, location);
!function(e,n,o){var t=e.screen,a=encodeURIComponent,r=["dt="+a(n.title),"dr="+a(n.referrer),"ul="+(o.language||o.browserLanguage),"sd="+t.colorDepth+"-bit","sr="+t.width+"x"+t.height,"vp="+e.innerWidth+"x"+e.innerHeight,"z="+ +new Date],i="?"+r.join("&");e.__beacon_img=new Image,e.__beacon_img.src="/ga.php"+i}(window,document,navigator,location);
<?php
// ********************
// * Author: stneng
// * Date: 2016.12.11
// * Introduction: https://u.nu/ytq
// *********************
header("status: 204");
header("Cache-Control: no-cache, max-age=0");
header("Pragma: no-cache");
$tid=''; //在这里写Google Analytics给的tid,形如:UA-XXXX-Y
function create_uuid(){
$str = md5(uniqid(mt_rand(), true));
$uuid = substr($str,0,8) . '-';
$uuid .= substr($str,8,4) . '-';
$uuid .= substr($str,12,4) . '-';
$uuid .= substr($str,16,4) . '-';
$uuid .= substr($str,20,12);
return $uuid;
}
if (!isset($_COOKIE["uuid"])) {
$uuid=create_uuid();
setcookie("uuid", $uuid , time()+368400000);
}else{
$uuid=$_COOKIE["uuid"];
}
if (function_exists("fastcgi_finish_request")) {
fastcgi_finish_request(); //对于fastcgi会提前返回请求结果,提高响应速度。
}
$url='v=1&t=pageview&';
$url.='tid='.$tid.'&';
$url.='cid='.$uuid.'&';
$url.='dl='.rawurlencode(rawurldecode($_SERVER['HTTP_REFERER'])).'&';
$url.='uip='.rawurlencode(rawurldecode($_SERVER['REMOTE_ADDR'])).'&';
$url.='ua='.rawurlencode(rawurldecode($_SERVER['HTTP_USER_AGENT'])).'&';
$url.='dt='.rawurlencode(rawurldecode($_GET['dt'])).'&';
$url.='dr='.rawurlencode(rawurldecode($_GET['dr'])).'&';
$url.='ul='.rawurlencode(rawurldecode($_GET['ul'])).'&';
$url.='sd='.rawurlencode(rawurldecode($_GET['sd'])).'&';
$url.='sr='.rawurlencode(rawurldecode($_GET['sr'])).'&';
$url.='vp='.rawurlencode(rawurldecode($_GET['vp'])).'&';
$url.='z='.$_GET['z'];
$url='https://www.google-analytics.com/collect?'.$url;
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
@bsed

This comment has been minimized.

Copy link

bsed commented Aug 8, 2017

good~ thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.