line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Google::Analytics::Response; |
2
|
|
|
|
|
|
|
$Net::Google::Analytics::Response::VERSION = '3.05'; |
3
|
2
|
|
|
2
|
|
7
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
46
|
|
4
|
2
|
|
|
2
|
|
5
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
75
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Google Analytics API response |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Class::XSAccessor |
9
|
2
|
|
|
|
|
13
|
accessors => [ qw( |
10
|
|
|
|
|
|
|
is_success |
11
|
|
|
|
|
|
|
code message content |
12
|
|
|
|
|
|
|
total_results start_index items_per_page |
13
|
|
|
|
|
|
|
contains_sampled_data |
14
|
|
|
|
|
|
|
profile_info |
15
|
|
|
|
|
|
|
rows |
16
|
|
|
|
|
|
|
_column_headers |
17
|
|
|
|
|
|
|
_totals |
18
|
|
|
|
|
|
|
) ], |
19
|
2
|
|
|
2
|
|
6
|
constructor => 'new'; |
|
2
|
|
|
|
|
3
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub error_message { |
22
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
0
|
return join(' ', $self->code, $self->message, $self->content); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _parse_json { |
28
|
3
|
|
|
3
|
|
6
|
my ($self, $json) = @_; |
29
|
|
|
|
|
|
|
|
30
|
3
|
|
|
|
|
9
|
$self->items_per_page($json->{itemsPerPage}); |
31
|
3
|
|
|
|
|
8
|
$self->total_results($json->{totalResults}); |
32
|
3
|
|
|
|
|
8
|
$self->contains_sampled_data($json->{containsSampledData}); |
33
|
3
|
|
|
|
|
8
|
$self->profile_info($json->{profileInfo}); |
34
|
|
|
|
|
|
|
|
35
|
3
|
|
|
|
|
5
|
my $json_totals = $json->{totalsForAllResults}; |
36
|
3
|
|
|
|
|
4
|
my %totals; |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
14
|
while (my ($json_name, $total) = each(%$json_totals)) { |
39
|
5
|
|
|
|
|
10
|
my $column_name = _parse_column_name($json_name); |
40
|
5
|
|
|
|
|
21
|
$totals{$column_name} = $total; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
3
|
|
|
|
|
9
|
$self->_totals(\%totals); |
44
|
|
|
|
|
|
|
|
45
|
3
|
|
|
|
|
3
|
my @column_headers; |
46
|
|
|
|
|
|
|
|
47
|
3
|
|
|
|
|
5
|
for my $column_header (@{ $json->{columnHeaders} }) { |
|
3
|
|
|
|
|
9
|
|
48
|
|
|
|
|
|
|
push(@column_headers, { |
49
|
|
|
|
|
|
|
name => _parse_column_name($column_header->{name}), |
50
|
|
|
|
|
|
|
column_type => $column_header->{columnType}, |
51
|
|
|
|
|
|
|
data_type => $column_header->{dataType}, |
52
|
10
|
|
|
|
|
18
|
}); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
10
|
$self->_column_headers(\@column_headers); |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
24
|
my $class = Net::Google::Analytics::Row->_gen_class(\@column_headers); |
58
|
|
|
|
|
|
|
|
59
|
3
|
|
|
|
|
6
|
my @rows = map { $class->new($_) } @{ $json->{rows} }; |
|
15
|
|
|
|
|
37
|
|
|
3
|
|
|
|
|
6
|
|
60
|
3
|
|
|
|
|
14
|
$self->rows(\@rows); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _parse_column_name { |
64
|
15
|
|
|
15
|
|
18
|
my $name = shift; |
65
|
|
|
|
|
|
|
|
66
|
15
|
50
|
|
|
|
60
|
my ($res) = $name =~ /^(?:ga|rt):(\w{1,64})\z/ |
67
|
|
|
|
|
|
|
or die("invalid column name: $name"); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# convert camel case |
70
|
15
|
|
|
|
|
56
|
$res =~ s{([^A-Z]?)([A-Z]+)}{ |
71
|
9
|
|
|
|
|
18
|
my ($prev, $upper) = ($1, $2); |
72
|
9
|
100
|
|
|
|
54
|
$prev . ($prev =~ /[a-z]/ ? '_' : '') . lc($upper); |
73
|
|
|
|
|
|
|
}ge; |
74
|
|
|
|
|
|
|
|
75
|
15
|
|
|
|
|
59
|
return $res; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub num_rows { |
79
|
3
|
|
|
3
|
1
|
1548
|
my $self = shift; |
80
|
|
|
|
|
|
|
|
81
|
3
|
|
|
|
|
4
|
return scalar(@{ $self->rows }); |
|
3
|
|
|
|
|
25
|
|
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub metrics { |
85
|
3
|
|
|
3
|
1
|
1699
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
3
|
|
|
|
|
11
|
return $self->_columns('METRIC'); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub dimensions { |
91
|
3
|
|
|
3
|
1
|
1495
|
my $self = shift; |
92
|
|
|
|
|
|
|
|
93
|
3
|
|
|
|
|
11
|
return $self->_columns('DIMENSION'); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub totals { |
97
|
4
|
|
|
4
|
1
|
2637
|
my ($self, $metric) = @_; |
98
|
|
|
|
|
|
|
|
99
|
4
|
|
|
|
|
25
|
return $self->_totals->{$metric}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _columns { |
103
|
6
|
|
|
6
|
|
9
|
my ($self, $type) = @_; |
104
|
|
|
|
|
|
|
|
105
|
6
|
|
|
|
|
16
|
my $column_headers = $self->_column_headers; |
106
|
6
|
|
|
|
|
9
|
my @results; |
107
|
|
|
|
|
|
|
|
108
|
6
|
|
|
|
|
13
|
for my $column_header (@$column_headers) { |
109
|
18
|
100
|
|
|
|
44
|
if ($column_header->{column_type} eq $type) { |
110
|
9
|
|
|
|
|
21
|
push(@results, $column_header->{name}); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
6
|
|
|
|
|
23
|
return @results; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub project { |
118
|
1
|
|
|
1
|
1
|
424
|
my ($self, $proj_dim_names, $projection) = @_; |
119
|
|
|
|
|
|
|
|
120
|
1
|
|
|
|
|
2
|
my (@metric_indices, @proj_column_headers); |
121
|
1
|
|
|
|
|
4
|
my $column_headers = $self->_column_headers; |
122
|
|
|
|
|
|
|
|
123
|
1
|
|
|
|
|
6
|
for (my $i = 0; $i < @$column_headers; ++$i) { |
124
|
4
|
|
|
|
|
7
|
my $column_header = $column_headers->[$i]; |
125
|
|
|
|
|
|
|
|
126
|
4
|
100
|
|
|
|
12
|
if ($column_header->{column_type} eq 'METRIC') { |
127
|
2
|
|
|
|
|
4
|
push(@metric_indices, $i); |
128
|
2
|
|
|
|
|
12
|
push(@proj_column_headers, { %$column_header }); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
1
|
|
|
|
|
3
|
for my $name (@$proj_dim_names) { |
133
|
1
|
|
|
|
|
5
|
push(@proj_column_headers, { |
134
|
|
|
|
|
|
|
name => $name, |
135
|
|
|
|
|
|
|
column_type => 'DIMENSION', |
136
|
|
|
|
|
|
|
}); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
1
|
|
|
|
|
5
|
my $class = Net::Google::Analytics::Row->_gen_class(\@proj_column_headers); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Projected rows are collected in hash %proj_rows. The keys of the hash |
142
|
|
|
|
|
|
|
# are the the projected dimension values joined with zero bytes. |
143
|
|
|
|
|
|
|
|
144
|
1
|
|
|
|
|
3
|
my %proj_rows; |
145
|
|
|
|
|
|
|
|
146
|
1
|
|
|
|
|
2
|
for my $row (@{ $self->rows }) { |
|
1
|
|
|
|
|
5
|
|
147
|
5
|
|
|
|
|
9
|
my @proj_dim_values = $projection->($row); |
148
|
5
|
|
|
|
|
68
|
my $key = join("\0", @proj_dim_values); |
149
|
|
|
|
|
|
|
|
150
|
5
|
|
|
|
|
6
|
my $proj_row = $proj_rows{$key}; |
151
|
|
|
|
|
|
|
|
152
|
5
|
100
|
|
|
|
15
|
if (!$proj_row) { |
153
|
2
|
|
|
|
|
4
|
my @proj_metric_values = map { $row->[$_] } @metric_indices; |
|
4
|
|
|
|
|
16
|
|
154
|
2
|
|
|
|
|
16
|
$proj_rows{$key} = $class->new( |
155
|
|
|
|
|
|
|
[ @proj_metric_values, @proj_dim_values ], |
156
|
|
|
|
|
|
|
); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
else { |
159
|
3
|
|
|
|
|
9
|
for (my $i = 0; $i < @metric_indices; ++$i) { |
160
|
6
|
|
|
|
|
6
|
my $mi = $metric_indices[$i]; |
161
|
6
|
|
|
|
|
20
|
$proj_row->[$i] += $row->[$mi]; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
1
|
|
|
|
|
4
|
my @rows = values(%proj_rows); |
167
|
|
|
|
|
|
|
|
168
|
1
|
|
|
|
|
15
|
return Net::Google::Analytics::Response->new( |
169
|
|
|
|
|
|
|
is_success => 1, |
170
|
|
|
|
|
|
|
total_results => scalar(@rows), |
171
|
|
|
|
|
|
|
start_index => 1, |
172
|
|
|
|
|
|
|
items_per_page => scalar(@rows), |
173
|
|
|
|
|
|
|
rows => \@rows, |
174
|
|
|
|
|
|
|
_totals => $self->_totals, |
175
|
|
|
|
|
|
|
_column_headers => \@proj_column_headers, |
176
|
|
|
|
|
|
|
); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
1; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
__END__ |