前言
今天开始尝试把博客丢到群里去看友链了,因为之前用的是宝塔,自带访客统计,现在1panel,暂时没这个功能,也不想自建用umamai,就想到了google analytics。然后想到国内似乎也没镜像,就找资料发现有workers做代理,还能改变提交的path,有一定程度上做规避(虽然测试了还是被ublock毙了,cnmd
部署
google analytics
这个就没啥好说的了,记录下网址:
analytics.google.com
然后网站是属于数据流的。
workers
开通workers比较简单,之前也是这样的,没啥问题感觉。在配置中并没有用自己的域名,等哪天出问题了,再把自己的域名做个路由把,反正在cloudflare上
在cf上创建一个workers,不需要修改workers.js
addEventListener('fetch', (event) => {
// 这里可以加 filter(其实我也不知道这个怎么弄,没用上
return event.respondWith(handleRequest(event));
});
// worker应用的路由地址,末尾不加 '/',比如:ga.wangtwothree.workers.dev(即你workers的域名
const DOMAIN = 'xxx.workers.dev';
// 插入的js地址文件名,可自定义(下面这个不要写url,而是写你后面请求时候,请求的文件名,旁边的文件也不需要重命名
const JS_FILE = '自定义ga.js'
// 响应上报的接口路径,可自定义,规避广告屏蔽插件拦截
const COLLECT_PATH = 'collect_path';
// 原 gtag 地址,将G-XXX改为你的id
const JS_URL = 'https://www.googletagmanager.com/gtag/js?id=G-XXX'
// 下面不需要改
const G_DOMAIN = 'google-analytics.com';
const G_COLLECT_PATH = 'g\/collect';
async function handleRequest(event) {
const url = event.request.url;
if (url.match(`${DOMAIN}/${JS_FILE}`)) {
const requestJs = await (await fetch(JS_URL)).text();
const jsText = requestJs.replaceAll('\"www\"', '\"\"').replaceAll('.' + G_DOMAIN, DOMAIN).replaceAll(G_COLLECT_PATH, COLLECT_PATH);
return new Response(jsText, {
status: 200,
statusText: 'OK',
headers: {
'Content-Type': 'application/javascript',
},
});
} else if (url.match(`${DOMAIN}/${COLLECT_PATH}`)) {
const newReq = await readRequest(event.request);
event.waitUntil(fetch(newReq));
}
return new Response(null, {
status: 204,
statusText: 'No Content',
});
}
async function readRequest(request) {
const { url, headers } = request;
const body = await request.text();
const ga_url = url.replace(`${DOMAIN}/${COLLECT_PATH}`, `www.${G_DOMAIN}/${G_COLLECT_PATH}`);
const nq = {
method: 'POST',
headers: {
Host: 'www.google-analytics.com',
Origin: headers.get('origin'),
'Cache-Control': 'max-age=0',
'User-Agent': headers.get('user-agent'),
Accept: headers.get('accept'),
'Accept-Language': headers.get('accept-language'),
'Content-Type': headers.get('content-type') || 'text/plain',
Referer: headers.get('referer'),
},
body: body,
};
return new Request(ga_url, nq);
}
建议处理完后,不要用无痕测试,不知道为啥会出现问题,直接用正常模式关掉adblock之类的,测试一下,按道理google analytics 就可以看到统计数据了。
然后可以在zibll里插入统计代码了
这是原版的:
<!-- Google tag (gtag.js) -->
<script async src="https://xxx.workers.dev/自定义ga.js"></script> // 对应worker里的DOMAIN 和 JS_FILE,需要保持一致
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXX'); // 将G-XXX改为你的id
</script>
这是精简版的,图片说的是,只有1.5KB,草啊。(minimal-google-analytics-4-snippet),注意修改下面的 G-XXXXXX
和 cloudflare workers
的地址即可。
<script>
enScroll=!1,enFdl=!1,extCurrent=void 0,filename=void 0,targetText=void 0,splitOrigin=void 0;const lStor=localStorage,sStor=sessionStorage,doc=document,docEl=document.documentElement,docBody=document.body,docLoc=document.location,w=window,s=screen,nav=navigator||{},extensions=["pdf","xls","xlsx","doc","docx","txt","rtf","csv","exe","key","pps","ppt","pptx","7z","pkg","rar","gz","zip","avi","mov","mp4","mpe","mpeg","wmv","mid","midi","mp3","wav","wma"];function a(e,t,n,o){const j="G-XXXXXXXX",r=()=>Math.floor(Math.random()*1e9)+1,c=()=>Math.floor(Date.now()/1e3),F=()=>(sStor._p||(sStor._p=r()),sStor._p),E=()=>r()+"."+c(),_=()=>(lStor.cid_v4||(lStor.cid_v4=E()),lStor.cid_v4),m=lStor.getItem("cid_v4"),v=()=>m?void 0:enScroll==!0?void 0:"1",p=()=>(sStor.sid||(sStor.sid=c()),sStor.sid),O=()=>{if(!sStor._ss)return sStor._ss="1",sStor._ss;if(sStor.getItem("_ss")=="1")return void 0},a="1",g=()=>{if(sStor.sct)if(enScroll==!0)return sStor.sct;else x=+sStor.getItem("sct")+ +a,sStor.sct=x;else sStor.sct=a;return sStor.sct},i=docLoc.search,b=new URLSearchParams(i),h=["q","s","search","query","keyword"],y=h.some(e=>i.includes("&"+e+"=")||i.includes("?"+e+"=")),u=()=>y==!0?"view_search_results":enScroll==!0?"scroll":enFdl==!0?"file_download":"page_view",f=()=>enScroll==!0?"90":void 0,C=()=>{if(u()=="view_search_results"){for(let e of b)if(h.includes(e[0]))return e[1]}else return void 0},d=encodeURIComponent,k=e=>{let t=[];for(let n in e)e.hasOwnProperty(n)&&e[n]!==void 0&&t.push(d(n)+"="+d(e[n]));return t.join("&")},A=!1,S="https://[workers地址]/[collect PATH,之前设定的]",M=k({v:"2",tid:j,_p:F(),sr:(s.width*w.devicePixelRatio+"x"+s.height*w.devicePixelRatio).toString(),ul:(nav.language||void 0).toLowerCase(),cid:_(),_fv:v(),_s:"1",dl:docLoc.origin+docLoc.pathname+i,dt:doc.title||void 0,dr:doc.referrer||void 0,sid:p(),sct:g(),seg:"1",en:u(),"epn.percent_scrolled":f(),"ep.search_term":C(),"ep.file_extension":e||void 0,"ep.file_name":t||void 0,"ep.link_text":n||void 0,"ep.link_url":o||void 0,_ss:O(),_dbg:A?1:void 0}),l=S+"?"+M;if(nav.sendBeacon)nav.sendBeacon(l);else{let e=new XMLHttpRequest;e.open("POST",l,!0)}}a();function sPr(){return(docEl.scrollTop||docBody.scrollTop)/((docEl.scrollHeight||docBody.scrollHeight)-docEl.clientHeight)*100}doc.addEventListener("scroll",sEv,{passive:!0});function sEv(){const e=sPr();if(e<90)return;enScroll=!0,a(),doc.removeEventListener("scroll",sEv,{passive:!0}),enScroll=!1}document.addEventListener("DOMContentLoaded",function(){let e=document.getElementsByTagName("a");for(let t=0;t<e.length;t++)if(e[t].getAttribute("href")!=null){const n=e[t].getAttribute("href"),s=n.substring(n.lastIndexOf("/")+1),o=s.split(".").pop();(e[t].hasAttribute("download")||extensions.includes(o))&&e[t].addEventListener("click",fDl,{passive:!0})}});function fDl(e){enFdl=!0;const t=e.currentTarget.getAttribute("href"),n=t.substring(t.lastIndexOf("/")+1),s=n.split(".").pop(),o=n.replace("."+s,""),i=e.currentTarget.text,r=t.replace(docLoc.origin,"");a(s,o,i,r),enFdl=!1}
</script>
处理完后看后台应该就没问题了。
结语
弄起来简单,感觉没啥问题,而且确实弄完了比直接去谷歌要方便点,但是后面来了ping的字眼,还是会被规则毙掉,原本的minial的脚本改用了post,但是workers没有转发post的数据,下次有时间修改下更新吧
部分内容来自于:
· 借助Cloudflare Worker实现Google Analytics反代加速,规避广告屏蔽插件的拦截 https://wangtwothree.com/code/Cloudflare-Worker-Google-Analytics.html
暂无评论内容