File Coverage

blib/lib/Plack/Middleware/GoogleAnalytics.pm
Criterion Covered Total %
statement 29 29 100.0
branch 2 2 100.0
condition 3 6 50.0
subroutine 8 8 100.0
pod 1 1 100.0
total 43 46 93.4


line stmt bran cond sub pod time code
1             package Plack::Middleware::GoogleAnalytics;
2 2     2   28808 use Moo;
  2         35330  
  2         13  
3 2     2   4600 use Plack::Util;
  2         16414  
  2         71  
4 2     2   1765 use Text::MicroTemplate qw(:all);
  2         6312  
  2         366  
5 2     2   46 use 5.008008;
  2         6  
  2         790  
6              
7             extends 'Plack::Middleware';
8              
9             our $VERSION = '0.01';
10              
11             has 'ga_id' => (
12             is => 'ro',
13             );
14              
15             has 'ga_template' => (
16             is => 'ro',
17             default => sub {<<'EOF'}
18            
31             EOF
32             );
33            
34             sub call {
35 2     2 1 37737 my ($self, $env) = @_;
36              
37 2         19 my $response = $self->app->($env);
38 2     2   44 $self->response_cb($response, sub { $self->_handle_response(shift) });
  2         41  
39             }
40              
41             sub _handle_response {
42 2     2   5 my ($self, $response) = @_;
43 2         10 my $header = Plack::Util::headers($response->[1]);
44 2         76 my $content_type = $header->get('Content-Type');
45 2         78 my $ga_id = $self->ga_id;
46            
47 2 100 33     49 return unless defined $content_type && $content_type =~ qr[text/html] && $ga_id;
      66        
48            
49 1         4 my $body = [];
50 1     1   9 Plack::Util::foreach( $response->[2], sub { push @$body, $_[0] });
  1         12  
51 1         7 $body = join '', @$body;
52              
53 1         10 $body .= render_mt($self->ga_template, $ga_id)->as_string;
54            
55 1         1308 $response->[2] = [$body];
56 1         8 $header->set('Content-Length', length $body);
57              
58 1         86 return;
59             }
60              
61             1;
62             =head1 NAME
63              
64             Plack::Middleware::GoogleAnalytics - Middleware to apply Google Anlytics javascript code.
65              
66             =head1 SYNOPSIS
67              
68             use Plack::Builder;
69              
70             builder {
71             enable "Plack::Middleware::GoogleAnalytics", ga_id => 'UA-214112-11';
72             $app;
73             };
74              
75             =head1 DESCRIPTION
76              
77             L will insert the standard google analytics
78             Javascript at the end of a text/html document. It places this boilerplate code at the
79             end of the document.
80              
81             =head1 ARGUMENTS
82              
83             This middleware accepts the following arguments.
84              
85             =head2 ga_id
86              
87             This is the id that is supplied by Google. This is a required argument.
88              
89             =head1 SEE ALSO
90              
91             L, L, L
92              
93             =head1 AUTHOR
94              
95             Logan Bell, C<< >>
96              
97             =head1 COPYRIGHT & LICENSE
98              
99             Copyright 2012, Logan Bell
100              
101             This program is free software; you can redistribute it and/or modify
102             it under the same terms as Perl itself.
103              
104             =cut
105