Add yandex metrica tag

This commit is contained in:
2022-01-05 01:04:27 +01:00
parent 3f10f7b912
commit 26512c2b29
6 changed files with 37 additions and 1 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ func main() {
logrus.Fatal(err)
}
templateFuncs := &templates.Funcs{Version: Version}
templateFuncs := &templates.Funcs{Version: Version, Site: config.Site}
err = templates.Load(templateFuncs)
if err != nil {
logrus.Fatal(err)
+7
View File
@@ -23,6 +23,10 @@ type Content struct {
PostsPerPage int `mapstructure:"postsPerPage"`
}
type Site struct {
YandexKey string `mapstructure:"yandexKey"`
}
// Config contains application configuration
type Config struct {
Verbose string `mapstructure:"verbose"`
@@ -30,6 +34,7 @@ type Config struct {
Addr string `mapstructure:"addr"`
Unix string `mapstructure:"unix"`
Content Content `mapstructure:"content"`
Site Site `mapstructure:"site"`
}
// initialize default values on app-start
@@ -50,6 +55,8 @@ func init() {
pflag.String("content.pinned", "contact", "pinned page slug")
pflag.Int("content.postsPerPage", 5, "amount of posts per page")
pflag.String("site.yandexKey", "", "yandex analytics key")
pflag.Parse()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
+18
View File
@@ -0,0 +1,18 @@
{{ with $yk := getYaKey }}
{{ if $yk }}
<!-- Yandex.Metrika counter -->
<script type="text/javascript" >
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");
ym({{ $yk }}, "init", {
clickmap:true,
trackLinks:true,
accurateTrackBounce:true
});
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/{{ $yk }}" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->
{{ end }}
{{ end }}
+8
View File
@@ -4,10 +4,13 @@ import (
"html/template"
"net/url"
"sync"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config"
)
type Funcs struct {
Version string
Site config.Site
compiledJSAppURL string
@@ -54,6 +57,10 @@ func (f *Funcs) getBlogURL() string {
return URLBlog
}
func (f *Funcs) getYaKey() string {
return f.Site.YandexKey
}
// Use returns a func map with template helpers functions
func (f *Funcs) Use() template.FuncMap {
return template.FuncMap{
@@ -62,5 +69,6 @@ func (f *Funcs) Use() template.FuncMap {
"getJSAppURL": f.getJSAppURL,
"getIndexURL": f.getIndexURL,
"getBlogURL": f.getBlogURL,
"getYaKey": f.getYaKey,
}
}
+2
View File
@@ -6,4 +6,6 @@
<title>{{ .Title }}</title>
<script type="text/javascript" src="{{ getJSAppURL }}" async></script>
{{ template "analytics.go.tmpl" . }}
</head>
+1
View File
@@ -18,6 +18,7 @@ import (
//go:embed index.go.tmpl
//go:embed menu.go.tmpl
//go:embed post.go.tmpl
//go:embed analytics.go.tmpl
var content embed.FS
// List of compiled go-templates