| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$args{"tid"} and $args{"tid"} =~ /^(?:UA|MO|YT)-\d+-\d+$/
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+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
exists $args{"aip"} and not $args{"aip"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # default settings: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | $args{ua} ||= __PACKAGE__ . "/$VERSION"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"ua"} ||= 'Net::Google::Analytics::MeasurementProtocol' . "/$Net::Google::Analytics::MeasurementProtocol::VERSION"
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | $args{cid} ||= _gen_uuid_v4(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{"cid"} ||= _gen_uuid_v4()
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()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | $args{v} ||= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"v"} ||= 1
MC/DC0%0/1
$args{"v"} ||= 1
$args{"v"}
Truth table50%1/2
A
A= $args{"v"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | $args{cd} ||= '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"cd"} ||= "/"
MC/DC0%0/1
$args{"cd"} ||= "/"
$args{"cd"}
Truth table50%1/2
A
A= $args{"cd"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | $args{an} ||= 'My App'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"an"} ||= "My App"
MC/DC0%0/1
$args{"an"} ||= "My App"
$args{"an"}
Truth table50%1/2
A
A= $args{"an"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | $args{ds} ||= 'app'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"ds"} ||= "app"
MC/DC0%0/1
$args{"ds"} ||= "app"
$args{"ds"}
Truth table50%1/2
A
A= $args{"ds"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'})
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'})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | unless ( $ua_object->isa('Furl') || $ua_object->isa('LWP::UserAgent') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ua_object->isa("Furl") or $ua_object->isa("LWP::UserAgent")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition11%1/9
$hit_type eq "pageview" and not $args{"dl"} || $args{"dh"} && $args{"dp"}
$args{'dl'} || $args{'dh'} && $args{'dp'}
$args{"dh"} and $args{"dp"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return $self->{debug} ? $res : $res->is_success; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||