| 1 | package Net::Google::Analytics; | ||||||||||||||||||||||||||||
| 2 | 2 | $Net::Google::Analytics::VERSION = '3.05'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Simple interface to the Google Analytics Core Reporting API | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Net::Google::Analytics::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Net::Google::Analytics::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Net::Google::Analytics::Row; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 2 | my $package = shift; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | my $self = bless({}, $package); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | return $self; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | sub auth_params { | ||||||||||||||||||||||||||
|
Subroutine
auth_params: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | my $auth_params = $self->{auth_params} || []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'auth_params'} || []
MC/DC100%1/1
$self->{'auth_params'} || []
$self->{'auth_params'}
Truth table100%2/2
A
A= $self->{'auth_params'}
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 5 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 2 | $self->{auth_params} = [ @_ ]; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 5 | return @$auth_params; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub token { | ||||||||||||||||||||||||||
|
Subroutine
token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my ($self, $token) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | $self->{auth_params} = [ | ||||||||||||||||||||||||||||
| 39 | 0 | Authorization => "$token->{token_type} $token->{access_token}", | |||||||||||||||||||||||||||
| 40 | ]; | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 7 | ▶ | sub user_agent { | ||||||||||||||||||||||||||
|
Subroutine
user_agent: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 7 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 7 | my $ua = $self->{user_agent}; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 7 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | 2 | $self->{user_agent} = $_[1]; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | elsif (!defined($ua)) { | ||||||||||||||||||||||||||||
| 52 | 2 | $ua = LWP::UserAgent->new(); | |||||||||||||||||||||||||||
| 53 | 2 | $self->{user_agent} = $ua; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 7 | return $ua; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub new_request { | ||||||||||||||||||||||||||
|
Subroutine
new_request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | return Net::Google::Analytics::Request->new(@_); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 3 | ▶ | sub _uri { | ||||||||||||||||||||||||||
|
Subroutine
_uri: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 66 | 3 | my ($self, $req, $start_index, $max_results) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 3 | ▶ | my $service = $req->realtime ? 'realtime' : 'ga'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 3 | my $uri = URI->new( | |||||||||||||||||||||||||||
| 70 | "https://www.googleapis.com/analytics/v3/data/$service" | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | 3 | my @params; | |||||||||||||||||||||||||||
| 73 | 3 | ▶ | push(@params, 'start-index' => $start_index) if defined($start_index); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 3 | ▶ | push(@params, 'max-results' => $max_results) if defined($max_results); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | $uri->query_form( | |||||||||||||||||||||||||||
| 77 | $req->_params, | ||||||||||||||||||||||||||||
| 78 | @params, | ||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 3 | return $uri; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub uri { | ||||||||||||||||||||||||||
|
Subroutine
uri: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | return $self->_uri($req, $req->start_index, $req->max_results); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 3 | ▶ | sub _retrieve_http { | ||||||||||||||||||||||||||
|
Subroutine
_retrieve_http: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 3 | my ($self, $req, $start_index, $max_results) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 3 | my $uri = $self->_uri($req, $start_index, $max_results); | |||||||||||||||||||||||||||
| 94 | 3 | my @auth_params = $self->auth_params; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 3 | return $self->user_agent->get($uri->as_string, @auth_params); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub retrieve_http { | ||||||||||||||||||||||||||
|
Subroutine
retrieve_http: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 0 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | return $self->_retrieve_http($req, $req->start_index, $req->max_results); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | ▶ | sub _retrieve { | ||||||||||||||||||||||||||
|
Subroutine
_retrieve: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 106 | 3 | my ($self, $req, $start_index, $max_results) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 3 | my $http_res = $self->_retrieve_http($req, $start_index, $max_results); | |||||||||||||||||||||||||||
| 109 | 3 | my $res = Net::Google::Analytics::Response->new; | |||||||||||||||||||||||||||
| 110 | 3 | $res->code($http_res->code); | |||||||||||||||||||||||||||
| 111 | 3 | $res->message($http_res->message); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 3 | ▶ | if (!$http_res->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $res->content($http_res->decoded_content); | |||||||||||||||||||||||||||
| 115 | 0 | return $res; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | my $json = from_json($http_res->decoded_content); | |||||||||||||||||||||||||||
| 119 | 3 | $res->_parse_json($json); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | $res->start_index($start_index); | |||||||||||||||||||||||||||
| 122 | 3 | $res->is_success(1); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 3 | return $res; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 3 | ▶ | sub retrieve { | ||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 3 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | return $self->_retrieve($req, $req->start_index, $req->max_results); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub retrieve_paged { | ||||||||||||||||||||||||||
|
Subroutine
retrieve_paged: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 0 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my $start_index = $req->start_index; | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | $start_index = 1 if !defined($start_index); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | my $remaining_items = $req->max_results; | |||||||||||||||||||||||||||
| 139 | 0 | my $max_items_per_page = 10_000; | |||||||||||||||||||||||||||
| 140 | 0 | my $res; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | while (!defined($remaining_items) || $remaining_items > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $remaining_items or $remaining_items > 0
MC/DC0%0/2
not defined $remaining_items or $remaining_items > 0
not defined $remaining_items
$remaining_items > 0
Truth table0%0/3
not(A) or B
A= not defined $remaining_itemsB= $remaining_items > 0
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | my $max_results = | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $remaining_items and $remaining_items < $max_items_per_page
MC/DC0%0/2
defined $remaining_items and $remaining_items < $max_items_per_page
defined $remaining_items
$remaining_items < $max_items_per_page
Truth table0%0/3
A and B
A= defined $remaining_itemsB= $remaining_items < $max_items_per_page
| |||||||||||||||||||||||||||||
| 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 | ▶ | return $page if !$page->is_success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if (!defined($res)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | $res = $page; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||
| 155 | 0 | push(@{ $res->rows }, @{ $page->rows }); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | my $num_items = @{ $page->rows }; | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | last if $num_items < $max_results; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | $remaining_items -= $num_items if defined($remaining_items); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | $start_index += $num_items; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $total_items = @{ $res->rows }; | |||||||||||||||||||||||||||
| 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 | 2 | 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||