From e7401c673aae1406031b277da359a1d1b4b344c9 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Tue, 4 Jan 2011 13:19:35 +0100 Subject: [PATCH] google: add google analytics The code is based primarily on the flattr plugin. --- IkiWiki/Plugin/google.pm | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/IkiWiki/Plugin/google.pm b/IkiWiki/Plugin/google.pm index 910e79231..736bdfb8c 100644 --- a/IkiWiki/Plugin/google.pm +++ b/IkiWiki/Plugin/google.pm @@ -2,6 +2,7 @@ package IkiWiki::Plugin::google; # Google services plugin. Currently allows the inclusion of a search form +# and Google Analytics. use warnings; use strict; @@ -12,6 +13,7 @@ sub import { hook(type => "getsetup", id => "google", call => \&getsetup); hook(type => "checkconfig", id => "google", call => \&checkconfig); hook(type => "pagetemplate", id => "google", call => \&pagetemplate); + hook(type => "format", id => "google", call => \&format); } sub getsetup () { @@ -29,6 +31,14 @@ sub getsetup () { safe => 1, rebuild => 1, }, + google_analytics_account => { + type => "string", + example => 'UA-00000-0', + description => "Account/Web Property ID to use by default for Google Analytics code", + advanced => 0, + safe => 1, + rebuild => 1, + }, } sub sitesearch_enabled() { @@ -71,4 +81,43 @@ sub pagetemplate (@) { } } +sub format (@) { + my %params=@_; + + # Add Google Analytics' javascript to all pages, if an account is defined + if (defined $config{google_analytics_account}) { + if (! ($params{content}=~s!^(]*>)!$1.ga_js()!em)) { + # no tag, probably in preview mode + $params{content}=ga_js().$params{content}; + } + } + return $params{content}; +} + +my $ga_js_cached; +sub ga_js { + return $ga_js_cached if defined $ga_js_cached; + + return unless defined $config{google_analytics_account}; + + my $account = $config{google_analytics_account}; + + # This is Google Analytics' standard javascript snippet to include their + # external javascript file, asynchronously. + return $ga_js_cached=<<"EOF"; + +EOF +} + 1 -- 2.32.0.93.g670b81a890