line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Google::Analytics; |
2
|
|
|
|
|
|
|
$Net::Google::Analytics::VERSION = '3.05'; |
3
|
2
|
|
|
2
|
|
1224
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
56
|
|
4
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
64
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Simple interface to the Google Analytics Core Reporting API |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
8
|
use JSON; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
10
|
|
9
|
2
|
|
|
2
|
|
1472
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
65877
|
|
|
2
|
|
|
|
|
62
|
|
10
|
2
|
|
|
2
|
|
971
|
use Net::Google::Analytics::Request; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
47
|
|
11
|
2
|
|
|
2
|
|
876
|
use Net::Google::Analytics::Response; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
49
|
|
12
|
2
|
|
|
2
|
|
819
|
use Net::Google::Analytics::Row; |
|
2
|
|
|
|
|
29
|
|
|
2
|
|
|
|
|
44
|
|
13
|
2
|
|
|
2
|
|
7
|
use URI; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1582
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
2
|
|
|
2
|
1
|
710
|
my $package = shift; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
3
|
my $self = bless({}, $package); |
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
|
|
7
|
return $self; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub auth_params { |
24
|
5
|
|
|
5
|
1
|
10
|
my $self = shift; |
25
|
|
|
|
|
|
|
|
26
|
5
|
|
100
|
|
|
24
|
my $auth_params = $self->{auth_params} || []; |
27
|
|
|
|
|
|
|
|
28
|
5
|
100
|
|
|
|
14
|
if (@_) { |
29
|
2
|
|
|
|
|
5
|
$self->{auth_params} = [ @_ ]; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
5
|
|
|
|
|
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
|
7
|
|
|
7
|
1
|
855
|
my $self = $_[0]; |
45
|
|
|
|
|
|
|
|
46
|
7
|
|
|
|
|
11
|
my $ua = $self->{user_agent}; |
47
|
|
|
|
|
|
|
|
48
|
7
|
100
|
|
|
|
53
|
if (@_ > 1) { |
|
|
100
|
|
|
|
|
|
49
|
2
|
|
|
|
|
4
|
$self->{user_agent} = $_[1]; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
elsif (!defined($ua)) { |
52
|
2
|
|
|
|
|
19
|
$ua = LWP::UserAgent->new(); |
53
|
2
|
|
|
|
|
4262
|
$self->{user_agent} = $ua; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
7
|
|
|
|
|
25
|
return $ua; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub new_request { |
60
|
2
|
|
|
2
|
1
|
7
|
my $self = shift; |
61
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
17
|
return Net::Google::Analytics::Request->new(@_); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _uri { |
66
|
3
|
|
|
3
|
|
3
|
my ($self, $req, $start_index, $max_results) = @_; |
67
|
|
|
|
|
|
|
|
68
|
3
|
100
|
|
|
|
16
|
my $service = $req->realtime ? 'realtime' : 'ga'; |
69
|
3
|
|
|
|
|
19
|
my $uri = URI->new( |
70
|
|
|
|
|
|
|
"https://www.googleapis.com/analytics/v3/data/$service" |
71
|
|
|
|
|
|
|
); |
72
|
3
|
|
|
|
|
12538
|
my @params; |
73
|
3
|
100
|
|
|
|
13
|
push(@params, 'start-index' => $start_index) if defined($start_index); |
74
|
3
|
50
|
|
|
|
10
|
push(@params, 'max-results' => $max_results) if defined($max_results); |
75
|
|
|
|
|
|
|
|
76
|
3
|
|
|
|
|
15
|
$uri->query_form( |
77
|
|
|
|
|
|
|
$req->_params, |
78
|
|
|
|
|
|
|
@params, |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
3
|
|
|
|
|
661
|
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
|
3
|
|
|
3
|
|
4
|
my ($self, $req, $start_index, $max_results) = @_; |
92
|
|
|
|
|
|
|
|
93
|
3
|
|
|
|
|
8
|
my $uri = $self->_uri($req, $start_index, $max_results); |
94
|
3
|
|
|
|
|
11
|
my @auth_params = $self->auth_params; |
95
|
|
|
|
|
|
|
|
96
|
3
|
|
|
|
|
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
|
3
|
|
|
3
|
|
3
|
my ($self, $req, $start_index, $max_results) = @_; |
107
|
|
|
|
|
|
|
|
108
|
3
|
|
|
|
|
9
|
my $http_res = $self->_retrieve_http($req, $start_index, $max_results); |
109
|
3
|
|
|
|
|
3069
|
my $res = Net::Google::Analytics::Response->new; |
110
|
3
|
|
|
|
|
23
|
$res->code($http_res->code); |
111
|
3
|
|
|
|
|
18
|
$res->message($http_res->message); |
112
|
|
|
|
|
|
|
|
113
|
3
|
50
|
|
|
|
16
|
if (!$http_res->is_success) { |
114
|
0
|
|
|
|
|
0
|
$res->content($http_res->decoded_content); |
115
|
0
|
|
|
|
|
0
|
return $res; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
3
|
|
|
|
|
16
|
my $json = from_json($http_res->decoded_content); |
119
|
3
|
|
|
|
|
220
|
$res->_parse_json($json); |
120
|
|
|
|
|
|
|
|
121
|
3
|
|
|
|
|
11
|
$res->start_index($start_index); |
122
|
3
|
|
|
|
|
6
|
$res->is_success(1); |
123
|
|
|
|
|
|
|
|
124
|
3
|
|
|
|
|
26
|
return $res; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub retrieve { |
128
|
3
|
|
|
3
|
1
|
34
|
my ($self, $req) = @_; |
129
|
|
|
|
|
|
|
|
130
|
3
|
|
|
|
|
15
|
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__ |