File Coverage

blib/lib/WWW/Piwik/API.pm
Criterion Covered Total %
statement 12 14 85.7
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 17 19 89.4


line stmt bran cond sub pod time code
1             package WWW::Piwik::API;
2              
3 1     1   16919 use 5.010001;
  1         3  
4 1     1   4 use strict;
  1         1  
  1         17  
5 1     1   3 use warnings;
  1         4  
  1         21  
6 1     1   601 use Moo;
  1         11734  
  1         6  
7 1     1   1553 use Types::Standard qw/Str Object Int/;
  0            
  0            
8             use JSON::MaybeXS;
9             use URI;
10             use LWP::UserAgent;
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.01
19              
20             =cut
21              
22             our $VERSION = '0.01';
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             my ($self, %data) = @_;
74             my $uri = $self->track_uri(%data);
75             my $res = $self->ua->get($uri->as_string);
76             return $res;
77             }
78              
79             sub track_uri {
80             my ($self, %data) = @_;
81             my %params = (bots => 1,
82             rec => 1,
83             idsite => $self->idsite,
84             token_auth => $self->token_auth,
85             );
86             my $json = JSON::MaybeXS->new(ascii => 1);
87             foreach my $k (keys %data) {
88             my $v = $data{$k};
89             if (defined $v) {
90             if (ref($v)) {
91             $params{$k} ||= $json->encode($v);
92             }
93             else {
94             $params{$k} ||= $v;
95             }
96             }
97             }
98             my $uri = URI->new($self->endpoint);
99             $uri->query_form(%params);
100             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