| 1 | package Jifty::Plugin::GoogleAnalytics; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use base qw/Jifty::Plugin/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Jifty::Plugin::GoogleAnalytics - Replace "</body>" by Google Analytics Javascript code dynamically. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 14 | 2 | our $JS; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Text::OutputFilter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head2 init | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 0 | my ($self, %arg) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | my $orig_out_method = \&Jifty::Dispatcher::render_template; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | $JS = $arg{javascript} || ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg{'javascript'} || ''
MC/DC0%0/1
$arg{'javascript'} || ''
$arg{'javascript'}
Truth table0%0/2
A
A= $arg{'javascript'}
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | unless ($JS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | warn 'Google Analytics js code is null. Check Jifty::Plugin::GoogleAnalytics configuration.'; | |||||||||||||||||||||||||||
| 31 | 0 | return 1; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | no warnings qw/redefine/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | *Jifty::Dispatcher::render_template = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | unless (defined Jifty->web->request->argument('use_google_analytics')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | Jifty->web->request->argument(use_google_analytics=>1); # default | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | tie *STDOUT, "Text::OutputFilter", 0, *STDOUT, \&_replace; | |||||||||||||||||||||||||||
| 41 | 0 | $orig_out_method->(@_); | |||||||||||||||||||||||||||
| 42 | 0 | untie *STDOUT; | |||||||||||||||||||||||||||
| 43 | 0 | }; | |||||||||||||||||||||||||||
| 44 | 0 | Jifty->log->info("Jifty::Dispatcher::render_template() was redefined by Jifty::Plugin::GoogleAnalytics."); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | 1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub _replace { | ||||||||||||||||||||||||||
|
Subroutine
_replace: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ($_[0] =~ m|</body>|o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | my $html = Jifty->handler->apache->content_type =~ m/html/o; | |||||||||||||||||||||||||||
| 53 | 0 | my $enable = Jifty->web->request->argument("use_google_analytics"); | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($html and $enable) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$html and $enable
MC/DC0%0/2
$html and $enable
$html
$enable
Truth table0%0/3
A and B
A= $htmlB= $enable
| |||||||||||||||||||||||||||||
| 55 | 0 | $_[0] =~ s|</body>|$JS</body>|o; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 0 | $_[0]; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 USAGE | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Add the following to your site_config.yml | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | framework: | ||||||||||||||||||||||||||||
| 66 | Plugins: | ||||||||||||||||||||||||||||
| 67 | - GoogleAnalytics: | ||||||||||||||||||||||||||||
| 68 | javascript: | | ||||||||||||||||||||||||||||
| 69 | <script type="text/javascript"> | ||||||||||||||||||||||||||||
| 70 | var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); | ||||||||||||||||||||||||||||
| 71 | document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); | ||||||||||||||||||||||||||||
| 72 | </script> | ||||||||||||||||||||||||||||
| 73 | <script type="text/javascript"> | ||||||||||||||||||||||||||||
| 74 | var pageTracker = _gat._getTracker("UA-*******-*"); | ||||||||||||||||||||||||||||
| 75 | pageTracker._initData(); | ||||||||||||||||||||||||||||
| 76 | pageTracker._trackPageview(); | ||||||||||||||||||||||||||||
| 77 | </script> | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | If you need disable it in some cases, then | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # in Mason template | ||||||||||||||||||||||||||||
| 82 | ... | ||||||||||||||||||||||||||||
| 83 | <%init> | ||||||||||||||||||||||||||||
| 84 | Jifty->web->request->argument( use_google_analytics => 0 ); | ||||||||||||||||||||||||||||
| 85 | </%init> | ||||||||||||||||||||||||||||
| 86 | ... | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # in Template::Declare | ||||||||||||||||||||||||||||
| 89 | ... | ||||||||||||||||||||||||||||
| 90 | template "example" => page { | ||||||||||||||||||||||||||||
| 91 | set use_google_analytics => 0; | ||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # in YourApp::Dispatcher (Mason template only) | ||||||||||||||||||||||||||||
| 95 | ... | ||||||||||||||||||||||||||||
| 96 | on qr{^/download} => [ | ||||||||||||||||||||||||||||
| 97 | run => { | ||||||||||||||||||||||||||||
| 98 | set use_google_analytics => 0; | ||||||||||||||||||||||||||||
| 99 | }, | ||||||||||||||||||||||||||||
| 100 | ]; | ||||||||||||||||||||||||||||
| 101 | ... | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | L<Jifty>, L<Text::OutputFilter> | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | bokutin, C<< <bokutin at cpan.org> >> | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Copyright 2008 bokutin, all rights reserved. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 116 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | 1; # End of Jifty::Plugin::GoogleAnalytics | |||||||||||||||||||||||||||