line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::GoogleAnalytics::Report; |
2
|
5
|
|
|
5
|
|
32
|
use Mojo::Base -base; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
36
|
|
3
|
|
|
|
|
|
|
|
4
|
2
|
50
|
|
2
|
1
|
270
|
sub count { shift->{data}{rowCount} || 0 } |
5
|
2
|
|
|
2
|
1
|
11
|
sub error { shift->{error} } |
6
|
1
|
50
|
|
1
|
1
|
7
|
sub page_token { shift->{nextPageToken} || '' } |
7
|
0
|
0
|
|
0
|
1
|
|
sub query { shift->{query} || {} } |
8
|
0
|
0
|
|
0
|
1
|
|
sub rows { shift->{data}{rows} || [] } |
9
|
0
|
|
|
0
|
1
|
|
sub tx { shift->{tx} } |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has maximums => sub { shift->_stats('maximums') }; |
12
|
|
|
|
|
|
|
has minimums => sub { shift->_stats('minimums') }; |
13
|
|
|
|
|
|
|
has totals => sub { shift->_stats('totals') }; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub rows_to_hash { |
16
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
17
|
0
|
|
|
|
|
|
my $headers = $self->{columnHeader}{metricHeader}{metricHeaderEntries}; |
18
|
0
|
|
|
|
|
|
my $reduced = {}; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
for my $row (@{$self->rows}) { |
|
0
|
|
|
|
|
|
|
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}}) { |
|
0
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
$prev = $level; |
27
|
0
|
|
0
|
|
|
|
$level = $level->{$dimension} ||= {}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
0
|
0
|
|
|
|
|
if (@$metrics == 1) { |
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
|
|
|
|
|
|
|
sub rows_to_table { |
44
|
0
|
|
|
0
|
1
|
|
my ($self, %args) = @_; |
45
|
0
|
|
|
|
|
|
my $headers = $self->{columnHeader}; |
46
|
0
|
|
|
|
|
|
my @rows; |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
unless ($args{no_headers}) { |
49
|
0
|
|
|
|
|
|
push @rows, [@{$headers->{dimensions}}, map { $_->{name} } @{$headers->{metricHeader}{metricHeaderEntries}}]; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
for my $row (@{$self->rows}) { |
|
0
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
push @rows, [@{$row->{dimensions}}, @{$row->{metrics}[0]{values}}]; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
0
|
|
|
|
if (($args{as} || '') eq 'text') { |
57
|
0
|
|
|
|
|
|
return Mojo::Util::tablify(\@rows); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
return \@rows; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _stats { |
64
|
0
|
|
|
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
|
|
|
|
|
|
|
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. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 DESCRIPTION |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
L represents a result from |
93
|
|
|
|
|
|
|
L. |
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. |
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. |
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. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 tx |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
$tx = $self->tx; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Holds the raw L 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 into tabular data. C can be used to return the table as |
200
|
|
|
|
|
|
|
either "text" or "hash" (default). Set C 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. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 SEE ALSO |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
L. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=cut |