line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Google::Analytics; |
2
|
|
|
|
|
|
|
$Net::Google::Analytics::VERSION = '3.03'; |
3
|
2
|
|
|
2
|
|
1210
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
63
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
56
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Simple interface to the Google Analytics Core Reporting API |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
7
|
use JSON; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
9
|
|
9
|
2
|
|
|
2
|
|
1306
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
90532
|
|
|
2
|
|
|
|
|
60
|
|
10
|
2
|
|
|
2
|
|
1104
|
use Net::Google::Analytics::Request; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
55
|
|
11
|
2
|
|
|
2
|
|
956
|
use Net::Google::Analytics::Response; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
48
|
|
12
|
2
|
|
|
2
|
|
811
|
use Net::Google::Analytics::Row; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
64
|
|
13
|
2
|
|
|
2
|
|
8
|
use URI; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1294
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
2
|
|
|
2
|
1
|
1203
|
my $package = shift; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
9
|
my $self = bless({}, $package); |
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
|
|
6
|
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
|
|
|
|
16
|
if (@_) { |
29
|
2
|
|
|
|
|
6
|
$self->{auth_params} = [ @_ ]; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
4
|
|
|
|
|
13
|
return @$auth_params; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub token { |
36
|
0
|
|
|
0
|
1
|
0
|
my ($self, $token) = @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
0
|
$self->{auth_params} = [ |
39
|
|
|
|
|
|
|
Authorization => "$token->{token_type} $token->{access_token}", |
40
|
|
|
|
|
|
|
]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub user_agent { |
44
|
6
|
|
|
6
|
1
|
1220
|
my $self = $_[0]; |
45
|
|
|
|
|
|
|
|
46
|
6
|
|
|
|
|
12
|
my $ua = $self->{user_agent}; |
47
|
|
|
|
|
|
|
|
48
|
6
|
100
|
|
|
|
24
|
if (@_ > 1) { |
|
|
100
|
|
|
|
|
|
49
|
2
|
|
|
|
|
7
|
$self->{user_agent} = $_[1]; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
elsif (!defined($ua)) { |
52
|
2
|
|
|
|
|
30
|
$ua = LWP::UserAgent->new(); |
53
|
2
|
|
|
|
|
4490
|
$self->{user_agent} = $ua; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
6
|
|
|
|
|
29
|
return $ua; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub new_request { |
60
|
2
|
|
|
2
|
1
|
9
|
my $self = shift; |
61
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
25
|
return Net::Google::Analytics::Request->new(@_); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _uri { |
66
|
2
|
|
|
2
|
|
3
|
my ($self, $req, $start_index, $max_results) = @_; |
67
|
|
|
|
|
|
|
|
68
|
2
|
100
|
|
|
|
8
|
my $service = $req->realtime ? 'realtime' : 'ga'; |
69
|
2
|
|
|
|
|
14
|
my $uri = URI->new( |
70
|
|
|
|
|
|
|
"https://www.googleapis.com/analytics/v3/data/$service" |
71
|
|
|
|
|
|
|
); |
72
|
2
|
|
|
|
|
15138
|
my @params; |
73
|
2
|
100
|
|
|
|
10
|
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
|
|
|
|
|
14
|
$uri->query_form( |
77
|
|
|
|
|
|
|
$req->_params, |
78
|
|
|
|
|
|
|
@params, |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
2
|
|
|
|
|
738
|
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
|
|
8
|
my ($self, $req, $start_index, $max_results) = @_; |
92
|
|
|
|
|
|
|
|
93
|
2
|
|
|
|
|
6
|
my $uri = $self->_uri($req, $start_index, $max_results); |
94
|
2
|
|
|
|
|
10
|
my @auth_params = $self->auth_params; |
95
|
|
|
|
|
|
|
|
96
|
2
|
|
|
|
|
9
|
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
|
|
3
|
my ($self, $req, $start_index, $max_results) = @_; |
107
|
|
|
|
|
|
|
|
108
|
2
|
|
|
|
|
8
|
my $http_res = $self->_retrieve_http($req, $start_index, $max_results); |
109
|
2
|
|
|
|
|
2616
|
my $res = Net::Google::Analytics::Response->new; |
110
|
2
|
|
|
|
|
13
|
$res->code($http_res->code); |
111
|
2
|
|
|
|
|
22
|
$res->message($http_res->message); |
112
|
|
|
|
|
|
|
|
113
|
2
|
50
|
|
|
|
16
|
if (!$http_res->is_success) { |
114
|
0
|
|
|
|
|
0
|
$res->content($http_res->decoded_content); |
115
|
0
|
|
|
|
|
0
|
return $res; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
2
|
|
|
|
|
17
|
my $json = from_json($http_res->decoded_content); |
119
|
2
|
|
|
|
|
181
|
$res->_parse_json($json); |
120
|
|
|
|
|
|
|
|
121
|
2
|
|
|
|
|
9
|
$res->start_index($start_index); |
122
|
2
|
|
|
|
|
13
|
$res->is_success(1); |
123
|
|
|
|
|
|
|
|
124
|
2
|
|
|
|
|
22
|
return $res; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub retrieve { |
128
|
2
|
|
|
2
|
1
|
38
|
my ($self, $req) = @_; |
129
|
|
|
|
|
|
|
|
130
|
2
|
|
|
|
|
13
|
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__ |