File Coverage

blib/lib/Bootylicious/Plugin/GoogleAnalytics.pm
Criterion Covered Total %
statement 27 27 100.0
branch 3 6 50.0
condition 2 5 40.0
subroutine 6 6 100.0
pod 1 1 100.0
total 39 45 86.6


line stmt bran cond sub pod time code
1             package Bootylicious::Plugin::GoogleAnalytics;
2              
3 1     1   520 use strict;
  1         1  
  1         23  
4 1     1   3 use warnings;
  1         1  
  1         20  
5              
6 1     1   3 use base 'Mojolicious::Plugin';
  1         1  
  1         81  
7              
8 1     1   3 use Mojo::ByteStream 'b';
  1         2  
  1         214  
9              
10             sub register {
11 1     1 1 34 my ($self, $app, $conf) = @_;
12              
13 1   50     3 $conf ||= {};
14              
15 1 50       2 return unless $conf->{urchin};
16 1         1 push @{$app->renderer->classes}, __PACKAGE__;
  1         6  
17              
18             $app->plugins->on(
19             after_dispatch => sub {
20 1     1   10477 my ($c) = @_;
21              
22 1 50 33     3 return unless $c->res->code && $c->res->code == 200;
23              
24 1         22 my $body = $c->res->body;
25 1 50       19 return unless $body;
26              
27 1         4 $c->stash(urchin => $conf->{urchin});
28              
29 1         12 my $ga_script = $c->render_to_string(
30             'template',
31             format => 'html'
32             );
33              
34 1         1188 $ga_script = b($ga_script)->encode('UTF-8');
35              
36 1         54 $body =~ s{}{$ga_script};
37 1         7 $c->res->body($body);
38             }
39 1         12 );
40             }
41              
42             1;
43             __DATA__