blib/lib/Net/Google/Analytics/MeasurementProtocol.pm

filter: Statementstmt 79.3% 46 / 58 Branchbran 35.0% 7 / 20 Conditioncond 32.3% 11 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 66.6% 6 / 9 Podpod 100% 2 / 2 totaltotal 50.7% 72 / 142 CC 26 SCAR 30.1
CC
26
Cov
57%
new
32.7
_request
20.0
_build_request_args
13.5
CRAP 20.3
↑ Summary
1 package Net::Google::Analytics::MeasurementProtocol;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = 0.05;
7  
8 sub new {
9 1 ▶ my $class = shift;
Subroutine new: called CC=15 SCAR=34.9
Pod documented
10 1 ▶ my %args = (@_ == 1 ? %{$_[0]} : @_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 1 1
11  
12 Carp::croak 'tracking_id (tid) missing or invalid'
13 1 ▶ unless $args{tid} && $args{tid} =~ /^(?:UA|MO|YT)-\d+-\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"tid"} and $args{"tid"} =~ /^(?:UA|MO|YT)-\d+-\d+$/ 0 1 1
Condition33%1/3
$args{"tid"} and $args{"tid"} =~ /^(?:UA|MO|YT)-\d+-\d+$/
!ll&&!rl&&r
001
MC/DC0%0/2
$args{"tid"} and $args{"tid"} =~ /^(?:UA|MO|YT)-\d+-\d+$/
$args{"tid"} $args{"tid"} =~ /^(?:UA|MO|YT)-\d+-\d+$/
Truth table33%1/3
A and B
A= $args{"tid"}B= $args{"tid"} =~ /^(?:UA|MO|YT)-\d+-\d+$/
ABresult
0X0
100
111
14  
15 # If the 'aip' key exists, even if set to 0, the ip will be anonymized.
16 # So we only push it to our args if user set it to 1.
17 1 ▶ delete $args{aip} if exists $args{aip} && !$args{aip};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"aip"} and not $args{"aip"} 0 1 1
Condition33%1/3
exists $args{"aip"} and not $args{"aip"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $args{"aip"} and not $args{"aip"}
exists $args{"aip"} not $args{"aip"}
Truth table33%1/3
A and not(B)
A= exists $args{"aip"}B= not $args{"aip"}
ABresult
0X0
100
111
18  
19 # default settings:
20 1 ▶ $args{ua} ||= __PACKAGE__ . "/$VERSION";
Condition50%1/2
$args{"ua"} ||= 'Net::Google::Analytics::MeasurementProtocol' . "/$Net::Google::Analytics::MeasurementProtocol::VERSION"
l!l
01
MC/DC0%0/1
$args{"ua"} ||= 'Net::Google::Analytics::MeasurementProtocol' . "/$Net::Google::Analytics::MeasurementProtocol::VERSION"
$args{"ua"}
Truth table50%1/2
A
A= $args{"ua"}
Aresult
11
00
21 1 ▶ $args{cid} ||= _gen_uuid_v4();
Condition33%1/3
$args{"cid"} ||= _gen_uuid_v4()
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{"cid"} ||= _gen_uuid_v4()
$args{"cid"} _gen_uuid_v4()
Truth table33%1/3
A ||= B
A= $args{"cid"}B= _gen_uuid_v4()
ABresult
1X1
011
000
22 1 ▶ $args{v} ||= 1;
Condition50%1/2
$args{"v"} ||= 1
l!l
01
MC/DC0%0/1
$args{"v"} ||= 1
$args{"v"}
Truth table50%1/2
A
A= $args{"v"}
Aresult
11
00
23 1 ▶ $args{cd} ||= '/';
Condition50%1/2
$args{"cd"} ||= "/"
l!l
01
MC/DC0%0/1
$args{"cd"} ||= "/"
$args{"cd"}
Truth table50%1/2
A
A= $args{"cd"}
Aresult
11
00
24 1 ▶ $args{an} ||= 'My App';
Condition50%1/2
$args{"an"} ||= "My App"
l!l
01
MC/DC0%0/1
$args{"an"} ||= "My App"
$args{"an"}
Truth table50%1/2
A
A= $args{"an"}
Aresult
11
00
25 1 ▶ $args{ds} ||= 'app';
Condition50%1/2
$args{"ds"} ||= "app"
l!l
01
MC/DC0%0/1
$args{"ds"} ||= "app"
$args{"ds"}
Truth table50%1/2
A
A= $args{"ds"}
Aresult
11
00
26  
27 1 ▶ my $ua_object = delete $args{ua_object} || _build_user_agent( $args{ua} );
Condition33%1/3
delete $args{'ua_object'} || _build_user_agent($args{'ua'})
l!l&&r!l&&!r
100
MC/DC0%0/2
delete $args{'ua_object'} || _build_user_agent($args{'ua'})
delete $args{'ua_object'} _build_user_agent($args{'ua'})
Truth table33%1/3
A || B
A= delete $args{'ua_object'}B= _build_user_agent($args{'ua'})
ABresult
1X1
011
000
28 1 ▶ unless ( $ua_object->isa('Furl') || $ua_object->isa('LWP::UserAgent') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua_object->isa("Furl") or $ua_object->isa("LWP::UserAgent")) 0 1 1
Condition33%1/3
$ua_object->isa("Furl") or $ua_object->isa("LWP::UserAgent")
l!l&&r!l&&!r
100
MC/DC0%0/2
$ua_object->isa("Furl") or $ua_object->isa("LWP::UserAgent")
$ua_object->isa("Furl") $ua_object->isa("LWP::UserAgent")
Truth table33%1/3
A or B
A= $ua_object->isa("Furl")B= $ua_object->isa("LWP::UserAgent")
ABresult
1X1
011
000
29 0 Carp::croak('ua_object must be of type Furl or LWP::UserAgent');
30 }
31  
32 1 my $debug = delete $args{debug};
33 1 return bless {
34 args => \%args,
35 debug => $debug,
36 ua => $ua_object,
37 }, $class;
38 }
39  
40 sub send {
41 0 ▶ my ($self, $hit_type, $args) = @_;
Subroutine send: not called CC=1 SCAR=6.9
Pod documented
42  
43 0 return $self->_request( $self->_build_request_args( $hit_type, $args ) );
44 }
45  
46 sub _build_request_args {
47 1 ▶ my ($self, $hit_type, $args) = @_;
Subroutine _build_request_args: called CC=8 SCAR=26.0
48  
49 1 my %args = (%{$self->{args}}, %$args, t => $hit_type);
50 1 my %required = (
51 pageview => [qw(v tid cid cd an)],
52 screenview => [qw(v tid cid cd an)],
53 event => [qw(v tid cid cd an ec ea)],
54 transaction => [qw(v tid cid cd an ti)],
55 item => [qw(v tid cid cd an ti in)],
56 social => [qw(v tid cid cd an sn sa st)],
57 exception => [qw(v tid cid cd an)],
58 timing => [qw(v tid cid cd an utc utv utt)],
59 );
60 1 ▶ Carp::croak("invalid hit type $hit_type") unless $required{$hit_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $required{$hit_type} 0 1 1
61  
62 1 foreach my $required ( @{$required{$hit_type}} ) {
63 Carp::croak("argument '$required' is required for '$hit_type' hit type. See https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#$required for more information")
64 6 ▶ unless $args{$required};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$required} 0 6 6
65 }
66 Carp::croak('for "pageview" hit types you must set either "dl" or both "dh" and "dp"')
67 1 ▶ if $hit_type eq 'pageview' && !($args{dl} || ($args{dh} && $args{dp}));
Branch: 1 branch
BranchTrueFalseTotal
if $hit_type eq "pageview" and not $args{"dl"} || $args{"dh"} && $args{"dp"} 0 1 1
Condition11%1/9
$hit_type eq "pageview" and not $args{"dl"} || $args{"dh"} && $args{"dp"}
!ll&&!rl&&r
100
$args{'dl'} || $args{'dh'} && $args{'dp'}
l!l&&r!l&&!r
000
$args{"dh"} and $args{"dp"}
!ll&&!rl&&r
000
MC/DC0%0/4
$hit_type eq "pageview" and not $args{"dl"} || $args{"dh"} && $args{"dp"}
$hit_type eq "pageview" $args{'dl'} $args{"dh"} $args{"dp"}
Truth table20%1/5
A and not(B || C and D)
A= $hit_type eq "pageview"B= $args{'dl'}C= $args{"dh"}D= $args{"dp"}
ABCDresult
0XXX0
11XX0
10110
100X1
10101
68 1 return \%args;
69 }
70  
71 sub _request {
72 0 ▶ my ($self, $args) = @_;
Subroutine _request: not called CC=4 SCAR=30.0
73  
74 0 my $ua = $self->{ua};
75 my $target = $self->{debug}
76 0 ▶ ? 'https://www.google-analytics.com/debug/collect'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'debug'} ? : 0 0 0
77 : 'https://www.google-analytics.com/collect'
78 ;
79  
80 # Compatibility layer for LWP::UserAgent
81 0 ▶ my $res = $ua->post( $target, $ua->isa('Furl') ? undef : (), $args );
Branch: 1 branch
BranchTrueFalseTotal
$ua->isa('Furl') ? : 0 0 0
82  
83 0 ▶ return $self->{debug} ? $res : $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'debug'} ? : 0 0 0
84 }
85  
86 sub _build_user_agent {
87 0 ▶ my ($ua) = @_;
Subroutine _build_user_agent: not called CC=1 SCAR=6.9
88 0 require Furl;
89 0 return Furl->new( agent => $ua, timeout => 5 );
90 }
91  
92 # UUID v4 (pseudo-random) generator based on UUID::Tiny
93 sub _gen_uuid_v4 {
94 3 ▶ my $uuid = '';
Subroutine _gen_uuid_v4: called CC=2 SCAR=6.9
95 3 for ( 1 .. 4 ) {
96 12 my $v1 = int(rand(65536)) % 65536;
97 12 my $v2 = int(rand(65536)) % 65536;
98 12 my $rand_32bit = ($v1 << 16) | $v2;
99 12 $uuid .= pack 'I', $rand_32bit;
100 }
101 3 substr $uuid, 6, 1, chr( ord( substr( $uuid, 6, 1 ) ) & 0x0f | 0x40 );
102 3 substr $uuid, 8, 1, chr( ord( substr( $uuid, 8, 1 ) ) & 0x3f | 0x80 );
103  
104 # uuid is created. Convert to string:
105 return join '-',
106 15 map { unpack 'H*', $_ }
107 3 map { substr $uuid, 0, $_, '' }
108 ( 4, 2, 2, 2, 6 );
109 }
110  
111 4 1;
112 __END__
↑ Summary