Skip to content

Commit f4cb8b8

Browse files
author
Paul Lhussiez
committed
Analytics support
1 parent 36f77ac commit f4cb8b8

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ server:
8787
host: 127.0.0.1
8888
port: 8006
8989
debug: true
90+
analytics:
91+
enabled: true
92+
tag: xx-xxxxx-xxx
9093
blog:
9194
title: Depado's Blog
9295
description: A simple blog from a developer who does things. Powered by smallblog.
@@ -158,6 +161,16 @@ In this section you can customize the global author. If you fill this, smallblog
158161
will know that every article that doesn't have author information was written
159162
by this author. This will also be displayed on the front page (article list).
160163

164+
### analytics
165+
166+
Configuration for Google Analytics. Other types or platforms will be supported
167+
but for now only GA can be enabled.
168+
169+
| Field | Description | Default |
170+
|-----------|--------------------------------------------------------|-----------|
171+
| `enabled` | Whether or not the Google Analytics feature is enabled | - |
172+
| `tag` | Google Analytics tag to use | - |
173+
161174
### Gitalk
162175

163176
Please see the documentation of [gitalk](https://github.com/gitalk/gitalk) to

cmd/serve.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func init() {
2424
serve.Flags().Bool("server.tls", false, "whether https is activated for the domain")
2525

2626
// Gitalk
27+
serve.Flags().Bool("analytics.enabled", false, "enable Google Analytics feature")
28+
serve.Flags().String("analytics.tag", "", "tag for the Google Analytics feature")
2729
serve.Flags().Bool("gitalk.enabled", false, "enable the gitalk feature")
2830
serve.Flags().String("gitalk.client_id", "", "client ID of the gitalk app")
2931
serve.Flags().String("gitalk.client_secret", "", "client secret of the gitalk app")

templates/analytics.tmpl

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{ define "analytics" }}
2+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ .tag }}"></script>
3+
<script>
4+
window.dataLayer = window.dataLayer || [];
5+
function gtag(){dataLayer.push(arguments);}
6+
gtag('js', new Date());
7+
gtag('config', '{{ .tag }}');
8+
</script>
9+
{{ end }}

templates/index.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<link rel="icon" href="/static/favicon.gif">
1111
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
1212
<meta name="viewport" content="width=device-width, initial-scale=1">
13+
{{ if .analytics.enabled }}{{ template "analytics" .analytics }}{{ end }}
1314
</head>
1415

1516
<body>

templates/post.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<link rel="icon" href="/static/favicon.gif">
1616
<meta name="viewport" content="width=device-width, initial-scale=1">
1717
{{ if .extra_style }}<style>{{ .extra_style }}</style>{{ end }}
18+
{{ if .analytics.enabled }}{{ template "analytics" .analytics }}{{ end }}
1819
</head>
1920

2021
<body>

views/views.go

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func PostsByTag(c *gin.Context) {
3838
"title": viper.GetString("blog.title"),
3939
"description": viper.GetString("blog.description"),
4040
"extra": template.HTML(fmt.Sprintf(`Posts tagged with <span class="home-sm-tag">%s</span>`, tag)),
41+
"analytics": gin.H{"tag": viper.GetString("analytics.tag"), "enabled": viper.GetBool("analytics.enabled")},
4142
"author": models.GetGlobalAuthor(),
4243
}
4344
c.HTML(http.StatusOK, "index.tmpl", data)
@@ -53,6 +54,7 @@ func Post(c *gin.Context) {
5354
"post": page,
5455
"gitalk": models.GetGitalk(),
5556
"extra_style": models.GlobCSS,
57+
"analytics": gin.H{"tag": viper.GetString("analytics.tag"), "enabled": viper.GetBool("analytics.enabled")},
5658
"share": viper.GetBool("blog.share"),
5759
"share_url": page.GetShare(),
5860
}
@@ -78,6 +80,7 @@ func Index(c *gin.Context) {
7880
"posts": models.SPages,
7981
"title": viper.GetString("blog.title"),
8082
"description": viper.GetString("blog.description"),
83+
"analytics": gin.H{"tag": viper.GetString("analytics.tag"), "enabled": viper.GetBool("analytics.enabled")},
8184
"author": models.GetGlobalAuthor(),
8285
}
8386
c.HTML(http.StatusOK, "index.tmpl", data)

0 commit comments

Comments
 (0)