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

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 3 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶no warnings qw(experimental::signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 our $VERSION = 4.00;
10  
11 package Net::Google::Analytics::MeasurementProtocol {
12  
13 sub new ($class, %args) {
14 return bless {
15 api_secret => $args{api_secret},
16 measurement_id => $args{measurement_id},
17 client_id => $args{client_id} // _gen_uuid_v4(),
18 agent => $args{agent} // _build_user_agent(),
19 debug => $args{debug},
20 _route => _build_route(%args),
21 }, $class;
22 }
23  
24 sub send ($self, $name, $properties) {
25 Carp::croak('properties must be a hashref') unless ref $properties eq 'HASH';
26 return $self->send_multiple( [{ $name => $properties }] );
27 }
28  
29 sub send_multiple ($self, $events) {
30 Carp::croak('events must be an array reference') unless ref $events eq 'ARRAY';
31 my @formatted_events;
32 foreach my $e (@$events) {
33 my ($name, $params) = each %$e;
34 push @formatted_events, { name => $name, params => $params }
35 }
36  
37 my $payload = JSON::encode_json({
38 client_id => $self->{client_id},
39 events => \@formatted_events,
40 });
41  
42 my $res = $self->{agent}->post( $self->{_route}, $self->{agent}->isa('Furl') ? undef : (), $payload );
43 if ($res->is_success) {
44 return $self->{debug} ? JSON::decode_json($res->decoded_content) : 1;
45 }
46 return { __PACKAGE__ => $res->decoded_content };
47 }
48  
49 sub _build_route(%args) {
50 if ($args{tid}) {
51 Carp::croak('Looks like you are calling ' . __PACKAGE__ . ' with'
52 . ' outdated arguments from Universal Analytics. Please update'
53 . ' to Google Analytics 4 (GA4) accordingly');
54 }
55 if (!$args{api_secret}) {
56 Carp::croak('api_secret is required. Create one in Admin > Data Streams'
57 . ' > choose your stream > Measurement Protocol > Create');
58 }
59 if (!$args{measurement_id}) {
60 Carp::croak('measurement_id is required. Find yours under Admin > Data'
61 . ' Streams > choose your stream > Measurement ID');
62 }
63  
64 my $debug = $args{debug} ? '/debug' : '';
65 return 'https://www.google-analytics.com' . $debug . '/mp/collect'
66 . '?measurement_id=' . $args{measurement_id}
67 . '&api_secret=' . $args{api_secret};
68 }
69  
70 sub _build_user_agent {
71 require Furl;
72 return Furl->new( agent => __PACKAGE__ . '/' . $VERSION, timeout => 5, headers => ['Content-Type' => 'application/json'] );
73 }
74  
75 # UUID v4 (pseudo-random) generator based on UUID::Tiny
76 sub _gen_uuid_v4 {
77 my $uuid = '';
78 for ( 1 .. 4 ) {
79 my ($v1, $v2) = (int(rand(65536)) % 65536, int(rand(65536)) % 65536);
80 my $rand_32bit = ($v1 << 16) | $v2;
81 $uuid .= pack 'I', $rand_32bit;
82 }
83 substr $uuid, 6, 1, chr( ord( substr( $uuid, 6, 1 ) ) & 0x0f | 0x40 );
84 substr $uuid, 8, 1, chr( ord( substr( $uuid, 8, 1 ) ) & 0x3f | 0x80 );
85  
86 # uuid is created. Convert to string:
87 return join '-', map { unpack 'H*', $_ } map { substr $uuid, 0, $_, '' } ( 4, 2, 2, 2, 6 );
88 }
89 };
90  
91 1;
92  
93 __END__
↑ Summary