google: add google analytics
[ikiwiki] / IkiWiki / Plugin / google.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::google;
3
4 # Google services plugin. Currently allows the inclusion of a search form
5 # and Google Analytics.
6
7 use warnings;
8 use strict;
9 use IkiWiki 3.00;
10 use URI;
11
12 sub import {
13         hook(type => "getsetup", id => "google", call => \&getsetup);
14         hook(type => "checkconfig", id => "google", call => \&checkconfig);
15         hook(type => "pagetemplate", id => "google", call => \&pagetemplate);
16         hook(type => "format", id => "google", call => \&format);
17 }
18
19 sub getsetup () {
20         return
21                 plugin => {
22                         safe => 1,
23                         rebuild => 1,
24                         section => "web",
25                 },
26                 google_sitesearch => {
27                         type => "boolean",
28                         example => 1,
29                         description => "Enable or disable the inclusion of a search form in all pages (default)",
30                         advanced => 0,
31                         safe => 1,
32                         rebuild => 1,
33                 },
34                 google_analytics_account => {
35                         type => "string",
36                         example => 'UA-00000-0',
37                         description => "Account/Web Property ID to use by default for Google Analytics code",
38                         advanced => 0,
39                         safe => 1,
40                         rebuild => 1,
41                 },
42 }
43
44 sub sitesearch_enabled() {
45         return 1 unless defined $config{google_sitesearch};
46         return $config{google_sitesearch};
47 }
48
49 sub checkconfig () {
50         # nothing to do if sitesearch is not enabled
51         return unless sitesearch_enabled();
52
53         if (! length $config{url}) {
54                 error(sprintf(gettext("Must specify %s when using the %s plugin"), "url", 'google'));
55         }
56
57         # This is a mass dependency, so if the search form template
58         # changes, every page is rebuilt.
59         add_depends("", "templates/googleform.tmpl");
60 }
61
62 my $form;
63 sub pagetemplate (@) {
64         # nothing to do if sitesearch is not enabled
65         return unless sitesearch_enabled();
66
67         my %params=@_;
68         my $page=$params{page};
69         my $template=$params{template};
70
71         # Add search box to page header.
72         if ($template->query(name => "searchform")) {
73                 if (! defined $form) {
74                         my $searchform = template("googleform.tmpl", blind_cache => 1);
75                         $searchform->param(url => $config{url});
76                         $searchform->param(html5 => $config{html5});
77                         $form=$searchform->output;
78                 }
79
80                 $template->param(searchform => $form);
81         }
82 }
83
84 sub format (@) {
85         my %params=@_;
86
87         # Add Google Analytics' javascript to all pages, if an account is defined
88         if (defined $config{google_analytics_account}) {
89                 if (! ($params{content}=~s!^(<body[^>]*>)!$1.ga_js()!em)) {
90                         # no <body> tag, probably in preview mode
91                         $params{content}=ga_js().$params{content};
92                 }
93         }
94         return $params{content};
95 }
96
97 my $ga_js_cached;
98 sub ga_js {
99         return $ga_js_cached if defined $ga_js_cached;
100
101         return unless defined $config{google_analytics_account};
102
103         my $account = $config{google_analytics_account};
104
105         # This is Google Analytics' standard javascript snippet to include their
106         # external javascript file, asynchronously.
107         return $ga_js_cached=<<"EOF";
108 <script type="text/javascript">
109 <!--//--><![CDATA[//><!--
110   var _gaq = _gaq || [];
111   _gaq.push(['_setAccount', '$account']);
112   _gaq.push(['_trackPageview']);
113
114   (function() {
115     var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
116     ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
117     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
118   })();//--><!]]>
119 </script>
120 EOF
121 }
122
123 1