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