| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Net::Google::Analytics; |
|
2
|
|
|
|
|
|
|
$Net::Google::Analytics::VERSION = '3.04'; |
|
3
|
2
|
|
|
2
|
|
1412
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
45
|
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
55
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Simple interface to the Google Analytics Core Reporting API |
|
7
|
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
9
|
use JSON; |
|
|
2
|
|
|
|
|
9
|
|
|
|
2
|
|
|
|
|
12
|
|
|
9
|
2
|
|
|
2
|
|
31897
|
use LWP::UserAgent; |
|
|
2
|
|
|
|
|
209773
|
|
|
|
2
|
|
|
|
|
74
|
|
|
10
|
2
|
|
|
2
|
|
1458
|
use Net::Google::Analytics::Request; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
59
|
|
|
11
|
2
|
|
|
2
|
|
1354
|
use Net::Google::Analytics::Response; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
57
|
|
|
12
|
2
|
|
|
2
|
|
1201
|
use Net::Google::Analytics::Row; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
72
|
|
|
13
|
2
|
|
|
2
|
|
12
|
use URI; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
1638
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
|
16
|
2
|
|
|
2
|
1
|
1704
|
my $package = shift; |
|
17
|
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
5
|
my $self = bless({}, $package); |
|
19
|
|
|
|
|
|
|
|
|
20
|
2
|
|
|
|
|
9
|
return $self; |
|
21
|
|
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub auth_params { |
|
24
|
4
|
|
|
4
|
1
|
14
|
my $self = shift; |
|
25
|
|
|
|
|
|
|
|
|
26
|
4
|
|
100
|
|
|
28
|
my $auth_params = $self->{auth_params} || []; |
|
27
|
|
|
|
|
|
|
|
|
28
|
4
|
100
|
|
|
|
17
|
if (@_) { |
|
29
|
2
|
|
|
|
|
6
|
$self->{auth_params} = [ @_ ]; |
|
30
|
|
|
|
|
|
|
} |
|
31
|
|
|
|
|
|
|
|
|
32
|
4
|
|
|
|
|
14
|
return @$auth_params; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub token { |
|
36
|
0
|
|
|
0
|
1
|
0
|
my ($self, $token) = @_; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$self->{auth_params} = [ |
|
39
|
0
|
|
|
|
|
0
|
Authorization => "$token->{token_type} $token->{access_token}", |
|
40
|
|
|
|
|
|
|
]; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub user_agent { |
|
44
|
6
|
|
|
6
|
1
|
1931
|
my $self = $_[0]; |
|
45
|
|
|
|
|
|
|
|
|
46
|
6
|
|
|
|
|
11
|
my $ua = $self->{user_agent}; |
|
47
|
|
|
|
|
|
|
|
|
48
|
6
|
100
|
|
|
|
29
|
if (@_ > 1) { |
|
|
|
100
|
|
|
|
|
|
|
49
|
2
|
|
|
|
|
5
|
$self->{user_agent} = $_[1]; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
elsif (!defined($ua)) { |
|
52
|
2
|
|
|
|
|
35
|
$ua = LWP::UserAgent->new(); |
|
53
|
2
|
|
|
|
|
22227
|
$self->{user_agent} = $ua; |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
6
|
|
|
|
|
33
|
return $ua; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub new_request { |
|
60
|
2
|
|
|
2
|
1
|
11
|
my $self = shift; |
|
61
|
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
25
|
return Net::Google::Analytics::Request->new(@_); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _uri { |
|
66
|
2
|
|
|
2
|
|
10
|
my ($self, $req, $start_index, $max_results) = @_; |
|
67
|
|
|
|
|
|
|
|
|
68
|
2
|
100
|
|
|
|
10
|
my $service = $req->realtime ? 'realtime' : 'ga'; |
|
69
|
2
|
|
|
|
|
16
|
my $uri = URI->new( |
|
70
|
|
|
|
|
|
|
"https://www.googleapis.com/analytics/v3/data/$service" |
|
71
|
|
|
|
|
|
|
); |
|
72
|
2
|
|
|
|
|
23878
|
my @params; |
|
73
|
2
|
100
|
|
|
|
13
|
push(@params, 'start-index' => $start_index) if defined($start_index); |
|
74
|
2
|
50
|
|
|
|
10
|
push(@params, 'max-results' => $max_results) if defined($max_results); |
|
75
|
|
|
|
|
|
|
|
|
76
|
2
|
|
|
|
|
18
|
$uri->query_form( |
|
77
|
|
|
|
|
|
|
$req->_params, |
|
78
|
|
|
|
|
|
|
@params, |
|
79
|
|
|
|
|
|
|
); |
|
80
|
|
|
|
|
|
|
|
|
81
|
2
|
|
|
|
|
767
|
return $uri; |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub uri { |
|
85
|
0
|
|
|
0
|
1
|
0
|
my ($self, $req) = @_; |
|
86
|
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
0
|
return $self->_uri($req, $req->start_index, $req->max_results); |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _retrieve_http { |
|
91
|
2
|
|
|
2
|
|
5
|
my ($self, $req, $start_index, $max_results) = @_; |
|
92
|
|
|
|
|
|
|
|
|
93
|
2
|
|
|
|
|
7
|
my $uri = $self->_uri($req, $start_index, $max_results); |
|
94
|
2
|
|
|
|
|
11
|
my @auth_params = $self->auth_params; |
|
95
|
|
|
|
|
|
|
|
|
96
|
2
|
|
|
|
|
10
|
return $self->user_agent->get($uri->as_string, @auth_params); |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub retrieve_http { |
|
100
|
0
|
|
|
0
|
1
|
0
|
my ($self, $req) = @_; |
|
101
|
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
0
|
return $self->_retrieve_http($req, $req->start_index, $req->max_results); |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub _retrieve { |
|
106
|
2
|
|
|
2
|
|
6
|
my ($self, $req, $start_index, $max_results) = @_; |
|
107
|
|
|
|
|
|
|
|
|
108
|
2
|
|
|
|
|
6
|
my $http_res = $self->_retrieve_http($req, $start_index, $max_results); |
|
109
|
2
|
|
|
|
|
3233
|
my $res = Net::Google::Analytics::Response->new; |
|
110
|
2
|
|
|
|
|
20
|
$res->code($http_res->code); |
|
111
|
2
|
|
|
|
|
23
|
$res->message($http_res->message); |
|
112
|
|
|
|
|
|
|
|
|
113
|
2
|
50
|
|
|
|
25
|
if (!$http_res->is_success) { |
|
114
|
0
|
|
|
|
|
0
|
$res->content($http_res->decoded_content); |
|
115
|
0
|
|
|
|
|
0
|
return $res; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
2
|
|
|
|
|
23
|
my $json = from_json($http_res->decoded_content); |
|
119
|
2
|
|
|
|
|
251
|
$res->_parse_json($json); |
|
120
|
|
|
|
|
|
|
|
|
121
|
2
|
|
|
|
|
17
|
$res->start_index($start_index); |
|
122
|
2
|
|
|
|
|
9
|
$res->is_success(1); |
|
123
|
|
|
|
|
|
|
|
|
124
|
2
|
|
|
|
|
23
|
return $res; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub retrieve { |
|
128
|
2
|
|
|
2
|
1
|
53
|
my ($self, $req) = @_; |
|
129
|
|
|
|
|
|
|
|
|
130
|
2
|
|
|
|
|
16
|
return $self->_retrieve($req, $req->start_index, $req->max_results); |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub retrieve_paged { |
|
134
|
0
|
|
|
0
|
1
|
|
my ($self, $req) = @_; |
|
135
|
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $start_index = $req->start_index; |
|
137
|
0
|
0
|
|
|
|
|
$start_index = 1 if !defined($start_index); |
|
138
|
0
|
|
|
|
|
|
my $remaining_items = $req->max_results; |
|
139
|
0
|
|
|
|
|
|
my $max_items_per_page = 10_000; |
|
140
|
0
|
|
|
|
|
|
my $res; |
|
141
|
|
|
|
|
|
|
|
|
142
|
0
|
|
0
|
|
|
|
while (!defined($remaining_items) || $remaining_items > 0) { |
|
143
|
0
|
0
|
0
|
|
|
|
my $max_results = |
|
144
|
|
|
|
|
|
|
defined($remaining_items) && |
|
145
|
|
|
|
|
|
|
$remaining_items < $max_items_per_page ? |
|
146
|
|
|
|
|
|
|
$remaining_items : $max_items_per_page; |
|
147
|
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
my $page = $self->_retrieve($req, $start_index, $max_results); |
|
149
|
0
|
0
|
|
|
|
|
return $page if !$page->is_success; |
|
150
|
|
|
|
|
|
|
|
|
151
|
0
|
0
|
|
|
|
|
if (!defined($res)) { |
|
152
|
0
|
|
|
|
|
|
$res = $page; |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
else { |
|
155
|
0
|
|
|
|
|
|
push(@{ $res->rows }, @{ $page->rows }); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
} |
|
157
|
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $num_items = @{ $page->rows }; |
|
|
0
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
|
last if $num_items < $max_results; |
|
160
|
|
|
|
|
|
|
|
|
161
|
0
|
0
|
|
|
|
|
$remaining_items -= $num_items if defined($remaining_items); |
|
162
|
0
|
|
|
|
|
|
$start_index += $num_items; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
my $total_items = @{ $res->rows }; |
|
|
0
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
$res->items_per_page($total_items); |
|
167
|
|
|
|
|
|
|
# The total result count of the first page isn't always accurate |
|
168
|
0
|
|
|
|
|
|
$res->total_results($total_items); |
|
169
|
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
return $res; |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
1; |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
__END__ |