diff --git a/conf/app.yml b/conf/app.yml index e6e9d4e..b2d581c 100644 --- a/conf/app.yml +++ b/conf/app.yml @@ -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: qiniu: # 七牛OSS bucket: eiblog diff --git a/pkg/config/config.go b/pkg/config/config.go index 0967f7e..cc18e56 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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"` } diff --git a/pkg/core/eiblog/page/fe.go b/pkg/core/eiblog/page/fe.go index 202099c..9db6447 100644 --- a/pkg/core/eiblog/page/fe.go +++ b/pkg/core/eiblog/page/fe.go @@ -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) diff --git a/website/st_ana.js b/website/st_ana.js index 7d1adb8..e8de9d8 100644 --- a/website/st_ana.js +++ b/website/st_ana.js @@ -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}} \ No newline at end of file +!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}}