File Coverage

blib/lib/WWW/Piwik/API.pm
Criterion Covered Total %
statement 35 39 89.7
branch 3 4 75.0
condition 3 6 50.0
subroutine 9 10 90.0
pod 2 2 100.0
total 52 61 85.2


line stmt bran cond sub pod time code
1             package WWW::Piwik::API;
2              
3 2     2   36995 use 5.010001;
  2         8  
4 2     2   13 use strict;
  2         3  
  2         54  
5 2     2   10 use warnings;
  2         10  
  2         89  
6 2     2   1416 use Moo;
  2         27955  
  2         53  
7 2     2   4580 use Types::Standard qw/Str Object Int/;
  2         179728  
  2         25  
8 2     2   3363 use JSON::MaybeXS;
  2         12161  
  2         137  
9 2     2   1493 use URI;
  2         9405  
  2         81  
10 2     2   1682 use LWP::UserAgent;
  2         76340  
  2         679  
11              
12             =head1 NAME
13              
14             WWW::Piwik::API - Tracking module for Piwik using the Tracking API
15              
16             =head1 VERSION
17              
18             Version 0.011
19              
20             =cut
21              
22             our $VERSION = '0.011';
23              
24              
25             =head1 SYNOPSIS
26              
27             my $tracker = WWW::Piwik::API->new(endpoint => $ENV{PIWIK_URL} || 'http://localhost/piwik.php',
28             idsite => $ENV{PIWIK_IDSITE} || 1,
29             token_auth => $ENV{PIWIK_TOKEN_AUTH},
30             );
31             # see https://developer.piwik.org/api-reference/tracking-api for params
32             $tracker->track(%data)
33              
34             =head1 ACCESSORS
35              
36             They are read-only and need to be set in the constructor.
37              
38             =head2 endpoint
39              
40             The endpoint of piwik. Required.
41              
42             =head2 idsite
43              
44             The ID of the site being tracked. You can look this up in the piwik console.
45              
46             =head2 token_auth
47              
48             The authentication token (you need an admin account for this, and the
49             token can be looked up under Personal settings/API.
50              
51             =head2 ua
52              
53             The user agent, which defaults to L with a timeout of
54             5 seconds.
55              
56             =cut
57              
58             has endpoint => (is => 'ro', isa => Str, required => 1);
59              
60             has idsite => (is => 'ro', isa => Int, required => 1);
61              
62             has token_auth => (is => 'ro', isa => Str);
63              
64             has ua => (is => 'ro',
65             isa => Object,
66             default => sub {
67             my $ua = LWP::UserAgent->new;
68             $ua->timeout(5);
69             return $ua;
70             });
71              
72             sub track {
73 0     0 1 0 my ($self, %data) = @_;
74 0         0 my $uri = $self->track_uri(%data);
75 0         0 my $res = $self->ua->get($uri->as_string);
76 0         0 return $res;
77             }
78              
79             sub track_uri {
80 2     2 1 1536 my ($self, %data) = @_;
81 2         18 my %params = (bots => 1,
82             rec => 1,
83             idsite => $self->idsite,
84             token_auth => $self->token_auth,
85             );
86 2         22 my $json = JSON::MaybeXS->new(ascii => 1);
87 2         70 foreach my $k (keys %data) {
88 14         18 my $v = $data{$k};
89 14 50       32 if (defined $v) {
90 14 100       23 if (ref($v)) {
91 2   33     37 $params{$k} ||= $json->encode($v);
92             }
93             else {
94 12   66     57 $params{$k} ||= $v;
95             }
96             }
97             }
98 2         17 my $uri = URI->new($self->endpoint);
99 2         7667 $uri->query_form(%params);
100 2         581 return $uri;
101             }
102              
103              
104             =head1 METHODS
105              
106             =head2 track(%data)
107              
108             Build an URI and do a call against it, serializing the parameters and
109             adding the default one set in the constructor.
110              
111             =head2 track_uri(%data)
112              
113             The URI against which track will be called.
114              
115             =head1 AUTHOR
116              
117             Stefan Hornburg, C<< >>
118              
119             =head1 BUGS
120              
121             Please report any bugs or feature requests at L.
122              
123             =head1 SEE ALSO
124              
125             L
126              
127             =head1 LICENSE AND COPYRIGHT
128              
129             Copyright 2016 Stefan Hornburg.
130              
131             This program is free software; you can redistribute it and/or modify it
132             under the terms of the the Artistic License (2.0). You may obtain a
133             copy of the full license at:
134              
135             L
136              
137             Any use, modification, and distribution of the Standard or Modified
138             Versions is governed by this Artistic License. By using, modifying or
139             distributing the Package, you accept this license. Do not use, modify,
140             or distribute the Package, if you do not accept this license.
141              
142             If your Modified Version has been derived from a Modified Version made
143             by someone other than you, you are nevertheless required to ensure that
144             your Modified Version complies with the requirements of this license.
145              
146             This license does not grant you the right to use any trademark, service
147             mark, tradename, or logo of the Copyright Holder.
148              
149             This license includes the non-exclusive, worldwide, free-of-charge
150             patent license to make, have made, use, offer to sell, sell, import and
151             otherwise transfer the Package with respect to any patent claims
152             licensable by the Copyright Holder that are necessarily infringed by the
153             Package. If you institute patent litigation (including a cross-claim or
154             counterclaim) against any party alleging that the Package constitutes
155             direct or contributory patent infringement, then this Artistic License
156             to you shall terminate on the date that such litigation is filed.
157              
158             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
159             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
160             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
161             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
162             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
163             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
164             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
165             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
166              
167              
168             =cut
169              
170             1; # End of WWW::Piwik::API