mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-04 05:42:27 +08:00
fix: google analytics not work, supported ga4 measurement protocol
This commit is contained in:
@@ -28,10 +28,9 @@ eiblogapp:
|
||||
publickey: wdSgxRm9rdGAlLKFcFdToBe3GT4SibmV7Y8EjJQ0r4GWXeKtxpopMAeIeoI2dTEg
|
||||
accesstoken: 50023908f39f4607957e909b495326af
|
||||
google: # 谷歌分析
|
||||
url: https://www.google-analytics.com/collect
|
||||
tid: UA-xxxxxx-1
|
||||
v: "1"
|
||||
t: pageview
|
||||
url: https://www.google-analytics.com/g/collect
|
||||
tid: G-S085VRC5PF
|
||||
v: "2"
|
||||
adsense: <script data-ad-client="ca-pub-5384494508691406" async src=""></script>
|
||||
qiniu: # 七牛OSS
|
||||
bucket: eiblog
|
||||
|
||||
@@ -69,7 +69,6 @@ type Google struct {
|
||||
URL string `yaml:"url"`
|
||||
Tid string `yaml:"tid"`
|
||||
V string `yaml:"v"`
|
||||
T string `yaml:"t"`
|
||||
AdSense string `yaml:"adsense"`
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
htemplate "html/template"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -335,27 +336,34 @@ func handleDisqusCreate(c *gin.Context) {
|
||||
}
|
||||
|
||||
// handleBeaconPage 服务端推送谷歌统计
|
||||
// https://www.thyngster.com/ga4-measurement-protocol-cheatsheet/
|
||||
func handleBeaconPage(c *gin.Context) {
|
||||
ua := c.Request.UserAgent()
|
||||
|
||||
vals := c.Request.URL.Query()
|
||||
vals.Set("v", config.Conf.EiBlogApp.Google.V)
|
||||
vals.Set("tid", config.Conf.EiBlogApp.Google.Tid)
|
||||
vals.Set("t", config.Conf.EiBlogApp.Google.T)
|
||||
cookie, _ := c.Cookie("u")
|
||||
vals.Set("cid", cookie)
|
||||
|
||||
vals.Set("dl", c.Request.Referer())
|
||||
vals.Set("uip", c.ClientIP())
|
||||
vals.Set("dl", c.Request.Referer()) // document location
|
||||
vals.Set("en", "page_view") // event name
|
||||
vals.Set("sct", "1") // Session Count
|
||||
vals.Set("seg", "1") // Session Engagment
|
||||
vals.Set("_uip", c.ClientIP()) // user ip
|
||||
vals.Set("_p", fmt.Sprint(201226219+rand.Intn(499999999))) // random page load hash
|
||||
vals.Set("_ee", "1") // external event
|
||||
go func() {
|
||||
req, err := http.NewRequest("POST", config.Conf.EiBlogApp.Google.URL,
|
||||
strings.NewReader(vals.Encode()))
|
||||
url := config.Conf.EiBlogApp.Google.URL + "?" + vals.Encode()
|
||||
req, err := http.NewRequest("POST", url, nil)
|
||||
if err != nil {
|
||||
logrus.Error("HandleBeaconPage.NewRequest: ", err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("User-Agent", ua)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("Sec-Ch-Ua", c.GetHeader("Sec-Ch-Ua"))
|
||||
req.Header.Set("Sec-Ch-Ua-Platform", c.GetHeader("Sec-Ch-Ua-Platform"))
|
||||
req.Header.Set("Sec-Ch-Ua-Mobile", c.GetHeader("Sec-Ch-Ua-Mobile"))
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
logrus.Error("HandleBeaconPage.Do: ", err)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{{define "ana_js"}}
|
||||
!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,"_="+ +new Date],i="?"+r.join("&");e.__beacon_img=new Image,e.__beacon_img.src="/beacon.html"+i}(window,document,navigator,location);
|
||||
{{end}}
|
||||
!function(e,n,o){var t=e.screen,a=encodeURIComponent,r=["dt="+a(n.title),"dr="+a(n.referrer),"ul="+(o.language||o.browserLanguage).toLowerCase(),"sd="+t.colorDepth+"-bit","sr="+t.width+"x"+t.height,"_="+ +new Date],i="?"+r.join("&");e.__beacon_img=new Image,e.__beacon_img.src="/beacon.html"+i}(window,document,navigator,location);
|
||||
{{end}}
|
||||
|
||||
Reference in New Issue
Block a user