Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internal Google Analytics template #1505

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/overview/introduction.md
Expand Up @@ -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/)
Expand Down Expand Up @@ -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/)

3 changes: 2 additions & 1 deletion docs/content/templates/variables.md
Expand Up @@ -66,7 +66,7 @@ Take for example I'm using *tags* and *categories* as my taxonomies. The followi
**All Params are only accessible using all lowercase characters.**

### Param method
In Hugo you can declare params both for the site and the individual page. A common use case is to have a general value for the site and a more specificy value for some of the pages (i.e. an image).
In Hugo you can declare params both for the site and the individual page. A common use case is to have a general value for the site and a more specificy value for some of the pages (i.e. an image).

With the `Param` method the most specific value will be selected for you, and it is safe to use it in any template (it's defined on both Page and Node):

Expand Down Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions hugolib/site.go
Expand Up @@ -106,6 +106,7 @@ type SiteInfo struct {
Author map[string]interface{}
LanguageCode string
DisqusShortname string
GoogleAnalytics string
Copyright string
LastChange time.Time
Permalinks PermalinkOverrides
Expand Down Expand Up @@ -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"),
Expand Down
12 changes: 12 additions & 0 deletions tpl/template_embedded.go
Expand Up @@ -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 }}`)

}