Add internal Google Analytics template

This commits also adds the GoogleAnalytics
variable to insert the tracking code.

Fixes #1424.
This commit is contained in:
digitalcraftsman 2015-10-19 13:31:03 +02:00 committed by spf13
parent ca55ea505a
commit 9dbb6dc50d
4 changed files with 16 additions and 1 deletions

View file

@ -127,6 +127,7 @@ Hugo boasts the following features:
### Additional Features
* Integrated [Disqus](https://disqus.com/) comment support
* Integrated [Google Analytics](https://google-analytics.com/) support
* Automatic [RSS](/layout/rss/) creation
* Support for [Go](http://golang.org/pkg/html/template/), [Amber](https://github.com/eknkc/amber) and [Ace](http://ace.yoss.si/) HTML templates
* Syntax [highlighting](/extras/highlighting/) powered by [Pygments](http://pygments.org/)
@ -183,4 +184,3 @@ as I have writing it.
* [Join the Mailing List](/community/mailing-list/)
* [Star us on GitHub](https://github.com/spf13/hugo)
* [Discussion Forum](http://discuss.gohugo.io/)

View file

@ -127,6 +127,7 @@ Also available is `.Site` which has the following:
**.Site.Author** A map of the authors as defined in the site configuration.<br>
**.Site.LanguageCode** A string representing the language as defined in the site configuration.<br>
**.Site.DisqusShortname** A string representing the shortname of the Disqus shortcode as defined in the site configuration.<br>
**.Site.GoogleAnalytics** A string representing your tracking code for Google Analytics as defined in the site configuration.<br>
**.Site.Copyright** A string representing the copyright of your web site as defined in the site configuration.<br>
**.Site.LastChange** A string representing the date/time of the most recent change to your site, based on the [`date` variable]({{< ref "content/front-matter.md#required-variables" >}}) in the front matter of your content pages.<br>
**.Site.Permalinks** A string to override the default permalink format. Defined in the site configuration.<br>

View file

@ -106,6 +106,7 @@ type SiteInfo struct {
Author map[string]interface{}
LanguageCode string
DisqusShortname string
GoogleAnalytics string
Copyright string
LastChange time.Time
Permalinks PermalinkOverrides
@ -456,6 +457,7 @@ func (s *Site) initializeSiteInfo() {
LanguageCode: viper.GetString("languagecode"),
Copyright: viper.GetString("copyright"),
DisqusShortname: viper.GetString("DisqusShortname"),
GoogleAnalytics: viper.GetString("GoogleAnalytics"),
BuildDrafts: viper.GetBool("BuildDrafts"),
canonifyURLs: viper.GetBool("CanonifyURLs"),
preserveTaxonomyNames: viper.GetBool("PreserveTaxonomyNames"),

View file

@ -202,6 +202,18 @@ func (t *GoHTMLTemplate) EmbedTemplates() {
<!-- Output all taxonomies as schema.org keywords -->
<meta itemprop="keywords" content="{{ range $plural, $terms := .Site.Taxonomies }}{{ range $term, $val := $terms }}{{ printf "%s," $term }}{{ end }}{{ end }}" />
{{ end }}`)
t.AddInternalTemplate("", "google_analytics.html", `{{ with .Site.GoogleAnalytics }}
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '{{ . }}', 'auto');
ga('send', 'pageview');
</script>
{{ end }}`)
}