blib/lib/Plack/Middleware/GoogleAnalytics.pm

filter: Statementstmt 100% 34 / 34 Branchbran 100% 2 / 2 Conditioncond 50.0% 3 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 90.9% 50 / 55 CC 4 SCAR 7.0
CC
4
Cov
93%
_handle_response
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.0
↑ Summary
1 package Plack::Middleware::GoogleAnalytics;
2 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Text::MicroTemplate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 extends 'Plack::Middleware';
8  
9 2 our $VERSION = '0.01';
10  
11 2 has 'ga_id' => (
12 is => 'ro',
13 );
14  
15 has 'ga_template' => (
16 is => 'ro',
17 2 ▶ default => sub {<<'EOF'}
Subroutine __ANON__: called CC=1 SCAR=0.0
18 <script type="text/javascript">
19  
20 var _gaq = _gaq || [];
21 _gaq.push(['_setAccount', '<?= $_[0] ?>']);
22 _gaq.push(['_trackPageview']);
23  
24 (function() {
25 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
26 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
27 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
28 })();
29  
30 </script>
31 EOF
32 2 );
33
34 2 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
35 2 my ($self, $env) = @_;
36  
37 2 my $response = $self->app->($env);
38 2 ▶ $self->response_cb($response, sub { $self->_handle_response(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
39 }
40  
41 2 ▶sub _handle_response {
Subroutine _handle_response: called CC=4 SCAR=14.0
42 2 my ($self, $response) = @_;
43 2 my $header = Plack::Util::headers($response->[1]);
44 2 my $content_type = $header->get('Content-Type');
45 2 my $ga_id = $self->ga_id;
46
47 2 ▶ return unless defined $content_type && $content_type =~ qr[text/html] && $ga_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content_type and $content_type =~ qr"text/html" and $ga_id 1 1 2
Condition50%3/6
defined $content_type and $content_type =~ qr"text/html" and $ga_id
!ll&&!rl&&r
011
defined $content_type and $content_type =~ qr"text/html"
!ll&&!rl&&r
002
MC/DC33%1/3
defined $content_type and $content_type =~ qr"text/html" and $ga_id
defined $content_type $content_type =~ qr"text/html" $ga_id
Truth table50%2/4
A and B and C
A= defined $content_typeB= $content_type =~ qr"text/html"C= $ga_id
ABCresult
0XX0
10X0
1100
1111
48
49 1 my $body = [];
50 1 ▶ Plack::Util::foreach( $response->[2], sub { push @$body, $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
51 1 $body = join '', @$body;
52  
53 1 $body .= render_mt($self->ga_template, $ga_id)->as_string;
54
55 1 $response->[2] = [$body];
56 1 $header->set('Content-Length', length $body);
57  
58 1 return;
59 }
60  
61 2 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<Plack::Middleware::GoogleAnalytics> 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<Plack>, L<Plack::Middleware>, L<Moo>
92  
93 =head1 AUTHOR
94  
95 Logan Bell, C<< <logie@cpan.org> >>
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  
↑ Summary