#!/usr/local/cpanel/3rdparty/bin/perl
package RVL::Rvanalytics;
use strict;
use warnings;
use Carp ();
use vars qw(@ISA @EXPORT $VERSION @lve @def);
our $VERSION = '1.0.1';
=head1 NAME
RVSInstaller::Analytics send Google Analytics user interaction data from Perl
=cut
sub new {
my $class = shift;
my %args = (@_ == 1 ? %{$_[0]} : @_ );
Carp::croak 'tracking_id (tid) missing or invalid'
unless $args{tid} && $args{tid} =~ /^(?:UA|MO|YT)-\d+-\d+$/;
# If the 'aip' key exists, even if set to 0, the ip will be anonymized.
# So we only push it to our args if user set it to 1.
delete $args{aip} if exists $args{aip} && !$args{aip};
# default settings:
$args{ua} ||= _get_user_agent();
$args{cid} ||= _gen_uuid_v4();
$args{v} ||= 1;
$args{cd} ||= '/';
$args{an} ||= 'My App';
$args{ds} ||= 'app';
my $debug = delete $args{debug};
return bless {
args => \%args,
debug => $debug,
ua => _build_user_agent( $args{ua} ),
}, $class;
}
sub _get_user_agent{
return ($ENV{HTTP_USER_AGENT}) ? $ENV{HTTP_USER_AGENT} : __PACKAGE__ . "/$VERSION";
}
sub _request {
my ($self, $args) = @_;
my $ua = $self->{ua};
my $target = $self->{debug}
? 'https://www.google-analytics.com/debug/collect'
: 'http://www.google-analytics.com/collect'
;
my $res = $ua->post( $target, $args );
if ($self->{debug}) {
return $res;
}
else {
return ($res->is_success) ? 1 : 0;
}
}
sub _build_user_agent {
my ($ua) = @_;
#require Furl;
#return Furl->new( agent => $ua, timeout => 5 );
require LWP::UserAgent;
my $oUA = LWP::UserAgent->new;
$oUA->timeout(5);
$oUA->agent($ua);
return $oUA;
}
# UUID v4 (pseudo-random) generator based on UUID::Tiny
sub _gen_uuid_v4 {
my $uuid = '';
for ( 1 .. 4 ) {
my $v1 = int(rand(65536)) % 65536;
my $v2 = int(rand(65536)) % 65536;
my $rand_32bit = ($v1 << 16) | $v2;
$uuid .= pack 'I', $rand_32bit;
}
substr $uuid, 6, 1, chr( ord( substr( $uuid, 6, 1 ) ) & 0x0f | 0x40 );
substr $uuid, 8, 1, chr( ord( substr( $uuid, 8, 1 ) ) & 0x3f | 0x80 );
# uuid is created. Convert to string:
return join '-',
map { unpack 'H*', $_ }
map { substr $uuid, 0, $_, '' }
( 4, 2, 2, 2, 6 );
}
sub send {
my ($self, $hit_type, $args) = @_;
my %args = (%{$self->{args}}, %$args, t => $hit_type);
my %required = $self->_parameters();
Carp::croak("invalid hit type $hit_type") unless $required{$hit_type};
foreach my $required ( @{$required{$hit_type}} ) {
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")
unless $args{$required};
}
Carp::croak('for "pageview" hit types you must set either "dl" or both "dh" and "dp"')
if $hit_type eq 'pageview' && !($args{dl} || ($args{dh} && $args{dp}));
return $self->_request(\%args);
}
=head3 Measurement Protocol Parameter Reference
=over 4
=item v (for Protocol Version)
=item tid (for Tracking ID / Web Property ID)
=item cid (for Client ID)
=item uid (for User ID)
=item uip (for IP Override)
=item cd (for Screen Name)
=item an (for Application Name)
=item sr (for Screen Resolution)
=item ul (for User Language)
=item av (for Application Version)
=item ec (for Event Category)
=item ea (for Event Action)
=item el (for Event Label)
=item ev (for Event Value)
=item ti (for Transaction ID)
=item in (for Item Name)
=item sn (for Social Network)
=item sa (for Social Action)
=item st (for Social Action Target)
=item utc (for User timing category)
=item utv (for User timing variable name)
=item utl (for User timing label)
=item utt (for User timing time)
=back
=cut
sub _parameters {
return (
pageview => [qw(v tid cid cd an)],
screenview => [qw(v tid cid cd an)],
event => [qw(v tid cid cd an ec ea)],
transaction => [qw(v tid cid cd an ti)],
item => [qw(v tid cid cd an ti in)],
social => [qw(v tid cid cd an sn sa st)],
exception => [qw(v tid cid cd an)],
timing => [qw(v tid cid cd an utc utv utt)],
);
}
1;
__END__
Copyright 2K16 - 2K18 Indonesian Hacker Rulez