commit 9dbb6dc50dd68117a809a86b09dd647c02d49b65
parent ca55ea505a2b87fe7c89a56d9f3f6b835f68aefc
Author: digitalcraftsman <digitalcraftsman@users.noreply.github.com>
Date: Mon, 19 Oct 2015 13:31:03 +0200
Add internal Google Analytics template
This commits also adds the GoogleAnalytics
variable to insert the tracking code.
Fixes #1424.
Diffstat:
4 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/docs/content/overview/introduction.md b/docs/content/overview/introduction.md
@@ -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/)
-
diff --git a/docs/content/templates/variables.md b/docs/content/templates/variables.md
@@ -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>
diff --git a/hugolib/site.go b/hugolib/site.go
@@ -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"),
diff --git a/tpl/template_embedded.go b/tpl/template_embedded.go
@@ -204,4 +204,16 @@ func (t *GoHTMLTemplate) EmbedTemplates() {
<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 }}`)
+
}