File Coverage

blib/lib/WWW/Tracking.pm
Criterion Covered Total %
statement 37 37 100.0
branch 5 8 62.5
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 54 57 94.7


line stmt bran cond sub pod time code
1             package WWW::Tracking;
2              
3 4     4   239427 use warnings;
  4         6  
  4         102  
4 4     4   14 use strict;
  4         4  
  4         97  
5              
6             our $VERSION = '0.05';
7              
8 4     4   12 use base 'Class::Accessor::Fast';
  4         7  
  4         1647  
9              
10 4     4   9250 use Carp::Clan 'croak';
  4         6239  
  4         20  
11 4     4   438 use Scalar::Util 'weaken';
  4         4  
  4         146  
12 4     4   1318 use WWW::Tracking::Data;
  4         9  
  4         21  
13              
14             __PACKAGE__->mk_accessors(qw{
15             tracker_account
16             tracker_type
17             tracker_url
18             data
19             });
20              
21             sub new {
22 6     6 1 2111 my $class = shift;
23 6         58 my $self = $class->SUPER::new({
24             @_
25             });
26            
27 6         60 return $self;
28             }
29              
30             sub from {
31 5     5 1 760 my $self = shift;
32 5         8 my $type = shift;
33 5         7 my $args = shift;
34            
35 5         12 my $from_type = 'from_'.$type;
36            
37 5 50       45 croak 'no such data plugin for "'.$type.'"'
38             unless WWW::Tracking::Data->can($from_type);
39            
40 5         21 my $tracking_data = WWW::Tracking::Data->$from_type($args);
41 5         17 weaken($self);
42 5         23 $tracking_data->_tracking($self);
43 5 100       43 $tracking_data->new_visitor_id
44             unless $tracking_data->visitor_id;
45            
46 5         32 $self->data($tracking_data);
47            
48 5         31 return $self;
49             }
50              
51             sub make_tracking_request {
52 3     3 1 1406 my $self = shift;
53            
54 3 50       9 my $tracker_type = $self->tracker_type or croak 'tracker type not set';
55 3         20 my $tracker_function = 'make_tracking_request_'.$tracker_type;
56            
57 3 50       28 croak 'no tracking with '.$tracker_type.'possible'
58             unless $self->data->can($tracker_function);
59            
60 3         32 return $self->data->$tracker_function;
61             }
62              
63             1;
64              
65              
66             __END__
67              
68             =head1 NAME
69              
70             WWW::Tracking - universal website visitors tracking
71              
72             =head1 SYNOPSIS
73              
74             use WWW::Tracking;
75             use WWW::Tracking::Data::Plugin::GoogleAnalytics;
76              
77             my $wt = WWW::Tracking->new(
78             tracker_account => 'MO-9226801-5',
79             tracker_type => 'ga',
80             );
81              
82             $wt->from(
83             headers => {
84             headers => $c->request->headers,
85             'request_uri' => $c->request->uri,
86             'remote_ip' => $c->address,
87             visitor_cookie_name => '__vcid',
88             },
89             );
90            
91             eval { $wt->make_tracking_request; };
92             warn 'tracking request failed - '.$@
93             if $@;
94            
95             say $wt->data->visitor_id;
96             say $wt->data->hostname;
97             say $wt->data->request_uri;
98             say $wt->data->referer;
99             say $wt->data->user_agent;
100             say $wt->data->browser_language;
101             say $wt->data->remote_ip;
102              
103             my $data = $wt->data->as_hash;
104             my $wt2 = $wt->from(hash => $data);
105              
106             my $ga_url = $wt->data->as_ga;
107              
108             ###
109             # TODO
110              
111             my $wt3 = $wt->from(ga => $ga_url);
112            
113             use WWW::Tracking::Data::Plugin::Piwik;
114             my $piwik_url = $wt->data->as_piwik;
115             my $wt3 = $wt->from(piwik => $piwik_url);
116              
117             use WWW::Tracking::Data::Plugin::ECSV;
118             my $line = $wt->data->as_ecsv;
119             my $wt4 = $wt->from(ecsv => $line));
120              
121             use WWW::Tracking::Data::Plugin::Log;
122             my $line2 = $wt->data->as_log;
123             my $wt5 = $wt->from(log => $line2));
124              
125             =head1 NOTE
126              
127             Work in progress, designed to be pluggable, but for now only things that
128             I need (headers parsing and server-side Google Analytics) are implemented.
129              
130             =head1 DESCRIPTION
131              
132             =head2 GOAL
133              
134             Server-side web hits tracking, generic and flexible enough so that
135             many tracking services like Google Analytics, Piwik, local file, etc.
136             can be used depending on configuration.
137              
138             =head2 VISION
139              
140             Universal enough to process many sources (headers, logs, tracking URL-s, ...)
141             and covert or relay them to different other destinations (GA, Piwik, ...)
142             making use of the fact that the tracking data information is the same or
143             nearly the same for all the sources and destinations.
144              
145             =head2 IMPLEMENTATION
146              
147             Initially tracking data needs to be gathered. Look at L<WWW::Tracking::Data>
148             for the complete list. Most of these data can be found in headers of the
149             http request. Then these data can be serialized and passed on to one of
150             the tracking services.
151              
152             Bare L<WWW::Tracking::Data> offers just C<as_hash> and C<from_hash>, the
153             rest can be done by one or more plugins, like for example parsing the
154             http headers with L<WWW::Tracking::Data::Plugin::Headers> and passing it
155             to L<WWW::Tracking::Data::Plugin::GoogleAnalytics>.
156              
157             =head2 USE CASES
158              
159             =over 4
160              
161             =item *
162              
163             tracking browsers that doesn't support JavaScript (ex. mobile browsing)
164              
165             =item *
166              
167             store the tracking data in local logs or files and replay it later to
168             Piwik or Google Analytics
169              
170             =item *
171              
172             track web browsing simultaneous to more tracking services (compare the
173             results, choose the one that fits)
174              
175             =item *
176              
177             aid with transition from one tracking service to another
178              
179             =back
180              
181             =head1 PROPERTIES
182              
183             tracker_account
184             tracker_type
185             data
186              
187             =head1 METHODS
188              
189             =head2 new()
190              
191             Object constructor.
192              
193             =head2 from($type, $args)
194              
195             Will call one of the C<from_$type> functions provided by
196             C<WWW::Tracking::Data::Plugin::*> passing on C<$args>.
197              
198             =head2 make_tracking_request
199              
200             Makes request (http, write to file, ...) to the tracking API via
201             calling one of the C<make_tracking_request_$tracker_type> that are
202             provided by C<WWW::Tracking::Data::Plugin::*>.
203              
204             =head1 SEE ALSO
205              
206             L<http://code.google.com/apis/analytics/docs/tracking/gaTrackingTroubleshooting.html#gifParameters>
207              
208             L<http://piwik.org/docs/tracking-api/>
209              
210             =head1 AUTHOR
211              
212             Jozef Kutej
213              
214             =cut