fix: google analytics not work, supported ga4 measurement protocol

This commit is contained in:
henry.chen
2023-09-25 18:12:57 +08:00
parent 2be53379e1
commit 9b918caccd
4 changed files with 19 additions and 13 deletions

View File

@@ -28,10 +28,9 @@ eiblogapp:
publickey: wdSgxRm9rdGAlLKFcFdToBe3GT4SibmV7Y8EjJQ0r4GWXeKtxpopMAeIeoI2dTEg publickey: wdSgxRm9rdGAlLKFcFdToBe3GT4SibmV7Y8EjJQ0r4GWXeKtxpopMAeIeoI2dTEg
accesstoken: 50023908f39f4607957e909b495326af accesstoken: 50023908f39f4607957e909b495326af
google: # 谷歌分析 google: # 谷歌分析
url: https://www.google-analytics.com/collect url: https://www.google-analytics.com/g/collect
tid: UA-xxxxxx-1 tid: G-S085VRC5PF
v: "1" v: "2"
t: pageview
adsense: <script data-ad-client="ca-pub-5384494508691406" async src=""></script> adsense: <script data-ad-client="ca-pub-5384494508691406" async src=""></script>
qiniu: # 七牛OSS qiniu: # 七牛OSS
bucket: eiblog bucket: eiblog

View File

@@ -69,7 +69,6 @@ type Google struct {
URL string `yaml:"url"` URL string `yaml:"url"`
Tid string `yaml:"tid"` Tid string `yaml:"tid"`
V string `yaml:"v"` V string `yaml:"v"`
T string `yaml:"t"`
AdSense string `yaml:"adsense"` AdSense string `yaml:"adsense"`
} }

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
htemplate "html/template" htemplate "html/template"
"io/ioutil" "io/ioutil"
"math/rand"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@@ -335,27 +336,34 @@ func handleDisqusCreate(c *gin.Context) {
} }
// handleBeaconPage 服务端推送谷歌统计 // handleBeaconPage 服务端推送谷歌统计
// https://www.thyngster.com/ga4-measurement-protocol-cheatsheet/
func handleBeaconPage(c *gin.Context) { func handleBeaconPage(c *gin.Context) {
ua := c.Request.UserAgent() ua := c.Request.UserAgent()
vals := c.Request.URL.Query() vals := c.Request.URL.Query()
vals.Set("v", config.Conf.EiBlogApp.Google.V) vals.Set("v", config.Conf.EiBlogApp.Google.V)
vals.Set("tid", config.Conf.EiBlogApp.Google.Tid) vals.Set("tid", config.Conf.EiBlogApp.Google.Tid)
vals.Set("t", config.Conf.EiBlogApp.Google.T)
cookie, _ := c.Cookie("u") cookie, _ := c.Cookie("u")
vals.Set("cid", cookie) vals.Set("cid", cookie)
vals.Set("dl", c.Request.Referer()) vals.Set("dl", c.Request.Referer()) // document location
vals.Set("uip", c.ClientIP()) 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() { go func() {
req, err := http.NewRequest("POST", config.Conf.EiBlogApp.Google.URL, url := config.Conf.EiBlogApp.Google.URL + "?" + vals.Encode()
strings.NewReader(vals.Encode())) req, err := http.NewRequest("POST", url, nil)
if err != nil { if err != nil {
logrus.Error("HandleBeaconPage.NewRequest: ", err) logrus.Error("HandleBeaconPage.NewRequest: ", err)
return return
} }
req.Header.Set("User-Agent", ua) 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) res, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
logrus.Error("HandleBeaconPage.Do: ", err) logrus.Error("HandleBeaconPage.Do: ", err)

View File

@@ -1,3 +1,3 @@
{{define "ana_js"}} {{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); !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}} {{end}}