Skip to content

Commit

Permalink
添加 Google Analytics, 使用 gamID 设置统计 id.
Browse files Browse the repository at this point in the history
关联的 issue: #11
  • Loading branch information
excing committed Feb 17, 2023
1 parent 419fef2 commit 54cab2a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
13 changes: 12 additions & 1 deletion assets/global.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@
function fullpath(path) {
return '{{ .BaseURL }}' + path
}
</script>
</script>
{{ if .GamID }}
<!-- Google tag (gtag.js) -->
<script async src='https://www.googletagmanager.com/gtag/js?id={{ .GamID }}'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());

gtag('config', '{{ .GamID }}');
</script>
{{ end }}
26 changes: 17 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
Pages string `flag:"Your github pages repository name, If None, defaults to the repository where the discussion resides"`
Debug bool `flag:"Debug mode if true"`
BaseURL string `flag:"Web site base url"`
GamID string `flag:"Google Analytics Measurement id, Defaults to empty to not load the Google Analytics script"`
}

func main() {
Expand All @@ -42,15 +43,22 @@ func main() {
}

_render := func() error {
return render(&RenderSite{BaseURL: config.BaseURL}, githubData, "assets/theme", config.Debug, func(s string, b []byte) error {
fname := strings.ReplaceAll(s, ".gtpl", ".html")
htmlPath := filepath.Join(config.Pages, fname)
MkdirFileFolderIfNotExists(htmlPath)
if config.Debug {
fmt.Println(s, string(b), "\n=========================================")
}
return os.WriteFile(htmlPath, b, 0666)
})
return render(
&RenderSite{
BaseURL: config.BaseURL,
GamID: config.GamID,
},
githubData, "assets/theme",
config.Debug,
func(s string, b []byte) error {
fname := strings.ReplaceAll(s, ".gtpl", ".html")
htmlPath := filepath.Join(config.Pages, fname)
MkdirFileFolderIfNotExists(htmlPath)
if config.Debug {
fmt.Println(s, string(b), "\n=========================================")
}
return os.WriteFile(htmlPath, b, 0666)
})
}
if err = _getGithubData(); err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type RenderData struct {
// RenderSite 渲染的站点信息
type RenderSite struct {
BaseURL string
GamID string
}

// JsRenderLoader js 渲染加载器
Expand Down

0 comments on commit 54cab2a

Please sign in to comment.