Add yandex metrica tag

develop
Nikita Tokarchuk 3 years ago
parent 3f10f7b912
commit 26512c2b29
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 2
      cmd/renderer/main.go
  2. 7
      pkg/config/config.go
  3. 18
      pkg/templates/analytics.go.tmpl
  4. 8
      pkg/templates/funcs.go
  5. 2
      pkg/templates/head.go.tmpl
  6. 1
      pkg/templates/templates.go

@ -52,7 +52,7 @@ func main() {
logrus.Fatal(err) logrus.Fatal(err)
} }
templateFuncs := &templates.Funcs{Version: Version} templateFuncs := &templates.Funcs{Version: Version, Site: config.Site}
err = templates.Load(templateFuncs) err = templates.Load(templateFuncs)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)

@ -23,6 +23,10 @@ type Content struct {
PostsPerPage int `mapstructure:"postsPerPage"` PostsPerPage int `mapstructure:"postsPerPage"`
} }
type Site struct {
YandexKey string `mapstructure:"yandexKey"`
}
// Config contains application configuration // Config contains application configuration
type Config struct { type Config struct {
Verbose string `mapstructure:"verbose"` Verbose string `mapstructure:"verbose"`
@ -30,6 +34,7 @@ type Config struct {
Addr string `mapstructure:"addr"` Addr string `mapstructure:"addr"`
Unix string `mapstructure:"unix"` Unix string `mapstructure:"unix"`
Content Content `mapstructure:"content"` Content Content `mapstructure:"content"`
Site Site `mapstructure:"site"`
} }
// initialize default values on app-start // initialize default values on app-start
@ -50,6 +55,8 @@ func init() {
pflag.String("content.pinned", "contact", "pinned page slug") pflag.String("content.pinned", "contact", "pinned page slug")
pflag.Int("content.postsPerPage", 5, "amount of posts per page") pflag.Int("content.postsPerPage", 5, "amount of posts per page")
pflag.String("site.yandexKey", "", "yandex analytics key")
pflag.Parse() pflag.Parse()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

@ -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 }}

@ -4,10 +4,13 @@ import (
"html/template" "html/template"
"net/url" "net/url"
"sync" "sync"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config"
) )
type Funcs struct { type Funcs struct {
Version string Version string
Site config.Site
compiledJSAppURL string compiledJSAppURL string
@ -54,6 +57,10 @@ func (f *Funcs) getBlogURL() string {
return URLBlog return URLBlog
} }
func (f *Funcs) getYaKey() string {
return f.Site.YandexKey
}
// Use returns a func map with template helpers functions // Use returns a func map with template helpers functions
func (f *Funcs) Use() template.FuncMap { func (f *Funcs) Use() template.FuncMap {
return template.FuncMap{ return template.FuncMap{
@ -62,5 +69,6 @@ func (f *Funcs) Use() template.FuncMap {
"getJSAppURL": f.getJSAppURL, "getJSAppURL": f.getJSAppURL,
"getIndexURL": f.getIndexURL, "getIndexURL": f.getIndexURL,
"getBlogURL": f.getBlogURL, "getBlogURL": f.getBlogURL,
"getYaKey": f.getYaKey,
} }
} }

@ -6,4 +6,6 @@
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<script type="text/javascript" src="{{ getJSAppURL }}" async></script> <script type="text/javascript" src="{{ getJSAppURL }}" async></script>
{{ template "analytics.go.tmpl" . }}
</head> </head>

@ -18,6 +18,7 @@ import (
//go:embed index.go.tmpl //go:embed index.go.tmpl
//go:embed menu.go.tmpl //go:embed menu.go.tmpl
//go:embed post.go.tmpl //go:embed post.go.tmpl
//go:embed analytics.go.tmpl
var content embed.FS var content embed.FS
// List of compiled go-templates // List of compiled go-templates

Loading…
Cancel
Save