GoogleAnalytics/Report.pm

filter: Statementstmt 17.5% 10 / 57 Branchbran 14.2% 2 / 14 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.7% 4 / 13 Podpod 100% 8 / 8 totaltotal 24.4% 24 / 98 CC 15 SCAR 28.2
CC
15
Cov
16%
rows_to_hash
42.0
rows_to_table
30.0
query
6.0
CRAP 16.8
↑ Summary GoogleAnalytics.pm »
1 package Mojo::GoogleAnalytics::Report;
2 5 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶sub count { shift->{data}{rowCount} || 0 }
Subroutine count: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"data"}{"rowCount"} or 0 2 0 2
5 2 ▶sub error { shift->{error} }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
6 1 ▶sub page_token { shift->{nextPageToken} || '' }
Subroutine page_token: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"nextPageToken"} or "" 1 0 1
7 0 ▶sub query { shift->{query} || {} }
Subroutine query: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"query"} or {} 0 0 0
8 0 ▶sub rows { shift->{data}{rows} || [] }
Subroutine rows: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"data"}{"rows"} or [] 0 0 0
9 0 ▶sub tx { shift->{tx} }
Subroutine tx: not called CC=1 SCAR=6.9
Pod documented
10  
11 0 ▶has maximums => sub { shift->_stats('maximums') };
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 0 ▶has minimums => sub { shift->_stats('minimums') };
Subroutine __ANON__: not called CC=1 SCAR=1.2
13 0 ▶has totals => sub { shift->_stats('totals') };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 0 ▶sub rows_to_hash {
Subroutine rows_to_hash: not called CC=6 SCAR=37.4
Pod documented
16 0 my $self = shift;
17 0 my $headers = $self->{columnHeader}{metricHeader}{metricHeaderEntries};
18 0 my $reduced = {};
19  
20 0 for my $row (@{$self->rows}) {
21 0 my $level = $reduced;
22 0 my $metrics = $row->{metrics}[0]{values};
23 0 my $prev;
24  
25 0 for my $dimension (@{$row->{dimensions}}) {
26 0 $prev = $level;
27 0 ▶ $level = $level->{$dimension} ||= {};
Condition0%0/2
$level->{$dimension} ||= {}
l!l
00
MC/DC0%0/1
$level->{$dimension} ||= {}
$level->{$dimension}
Truth table0%0/2
A
A= $level->{$dimension}
Aresult
11
00
28 }
29  
30 0 ▶ if (@$metrics == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$metrics == 1) { } 0 0 0
31 0 $prev->{$row->{dimensions}[-1]} = $metrics->[0];
32 }
33 else {
34 0 for my $i (0 .. @$headers - 1) {
35 0 $level->{$headers->[$i]{name}} = $metrics->[$i];
36 }
37 }
38 }
39  
40 0 return $reduced;
41 }
42  
43 0 ▶sub rows_to_table {
Subroutine rows_to_table: not called CC=5 SCAR=34.0
Pod documented
44 0 my ($self, %args) = @_;
45 0 my $headers = $self->{columnHeader};
46 0 my @rows;
47  
48 0 ▶ unless ($args{no_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"no_headers"}) 0 0 0
49 0 push @rows, [@{$headers->{dimensions}}, map { $_->{name} } @{$headers->{metricHeader}{metricHeaderEntries}}];
50 }
51  
52 0 for my $row (@{$self->rows}) {
53 0 push @rows, [@{$row->{dimensions}}, @{$row->{metrics}[0]{values}}];
54 }
55  
56 0 ▶ if (($args{as} || '') eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if (($args{"as"} || "") eq "text") 0 0 0
Condition0%0/2
$args{'as'} || ''
l!l
00
MC/DC0%0/1
$args{'as'} || ''
$args{'as'}
Truth table0%0/2
A
A= $args{'as'}
Aresult
11
00
57 0 return Mojo::Util::tablify(\@rows);
58 }
59  
60 0 return \@rows;
61 }
62  
63 0 ▶sub _stats {
Subroutine _stats: not called CC=2 SCAR=17.9
64 0 my ($self, $attr) = @_;
65 0 my $headers = $self->{columnHeader}{metricHeader}{metricHeaderEntries};
66 0 my $metrics = delete $self->{data}{$attr};
67 0 my %data;
68  
69 0 $metrics = $metrics->[0]{values};
70  
71 0 for my $i (0 .. @$headers - 1) {
72 0 $data{$headers->[$i]{name}} = $metrics->[$i];
73 }
74  
75 0 return \%data;
76 }
77  
78 5 1;
79  
80 =encoding utf8
81  
82 =head1 NAME
83  
84 Mojo::GoogleAnalytics::Report - Represents a Google Analytics report
85  
86 =head1 SYNOPSIS
87  
88 See L<Mojo::GoogleAnalytics>.
89  
90 =head1 DESCRIPTION
91  
92 L<Mojo::GoogleAnalytics::Report> represents a result from
93 L<Mojo::GoogleAnalytics/batch_get>.
94  
95 =head1 ATTRIBUTES
96  
97 =head2 count
98  
99 $int = $self->count;
100  
101 Returns the total count of rows that can be returned by Google Analytics.
102  
103 =head2 error
104  
105 $hash_ref = $self->error;
106  
107 Holds a hash ref if an error occurred and undef if not. Example data structure:
108  
109 {
110 code => 403,
111 message => "Something went wrong",
112 }
113  
114 =head2 maximums
115  
116 $hash_ref = $self->maximums;
117  
118 Holds a hash ref with the maximum metrics. Example:
119  
120 {
121 "ga:pageviews" => 349,
122 "ga:sessions" => 40,
123 }
124  
125 =head2 minimums
126  
127 $hash_ref = $self->minimums;
128  
129 See L</maximums>.
130  
131 =head2 page_token
132  
133 $str = $self->page_token;
134  
135 Holds a token that can be used to query Google Analytics for more data.
136  
137 =head2 query
138  
139 $hash_ref = $self->query;
140  
141 Holds the query passed on to L<Mojo::GoogleAnalytics/batch_get>.
142  
143 =head2 rows
144  
145 $array_ref = $self->rows;
146  
147 Holds the row data returned from Google Analytics. Example:
148  
149 [
150 {
151 dimensions => ["Norway", "Chrome"],
152 metrics => [{values => [349, 40]}],
153 },
154 ...
155 ]
156  
157 =head2 totals
158  
159 $hash_ref = $self->totals;
160  
161 See L</maximums>.
162  
163 =head2 tx
164  
165 $tx = $self->tx;
166  
167 Holds the raw L<Mojo::Transaction> object used in the request. Useful if you
168 need to extract raw data:
169  
170 my $raw_data = $tx->res->json;
171  
172 =head1 METHODS
173  
174 =head2 rows_to_hash
175  
176 $hash_ref = $self->rows_to_hash;
177  
178 Creates a multi dimensional hash, from the "dimensions" in the rows. Example
179 result:
180  
181 # Query dimensions: [{name => 'ga:country'}, {name => 'ga:browser'}]
182 # Result:
183 {
184 Norway => {
185 Chrome => {
186 "ga:pageviews" => 349,
187 "ga:sessions" => 40,
188 },
189 ...
190 },
191 ...
192 }
193  
194 =head2 rows_to_table
195  
196 $array_ref = $self->rows_to_table(no_headers => 1);
197 $str = $self->rows_to_table(as => "text", no_headers => 1);
198  
199 Converts L</rows> into tabular data. C<as> can be used to return the table as
200 either "text" or "hash" (default). Set C<no_headers> to a true value to avoid
201 getting the first item in the C<$array_ref> as header names. Example "text" output:
202  
203 ga:country ga:browser ga:pageviews ga:sessions
204 Sweden Chrome 472493 43340
205 Sweden Safari 413833 43242
206 Denmark Safari 127321 13975
207 Denmark Chrome 124904 12077
208 Norway Chrome 105998 10066
209  
210 =head1 AUTHOR
211  
212 Jan Henning Thorsen
213  
214 =head1 COPYRIGHT AND LICENSE
215  
216 L<Mojo::GoogleAnalytics>.
217  
218 =head1 SEE ALSO
219  
220 L<Mojo::GoogleAnalytics>.
221  
222 =cut
↑ Summary GoogleAnalytics.pm »