Created
October 19, 2009 02:21
-
-
Save lopnor/213031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Config::Pit; | |
use DateTime; | |
use YAML; | |
{ | |
package Analytics::Datum; | |
use Moose; | |
use Net::Google::DataAPI; | |
use XML::Atom::Util qw(first); | |
with 'Net::Google::DataAPI::Role::Entry'; | |
entry_has 'value' => ( | |
from_atom => sub { | |
my ($self, $atom) = @_; | |
first($atom->elem, $self->ns('dxp')->{uri}, 'metric')->getAttribute('value'); | |
} | |
); | |
entry_has 'name' => ( | |
from_atom => sub { | |
my ($self, $atom) = @_; | |
first($atom->elem, $self->ns('dxp')->{uri}, 'metric')->getAttribute('name'); | |
} | |
); | |
} | |
{ | |
package Analytics::Profile; | |
use Moose; | |
use Net::Google::DataAPI; | |
with 'Net::Google::DataAPI::Role::Entry'; | |
entry_has 'profile_id' => ( | |
ns => 'dxp', | |
tagname => 'tableId', | |
); | |
} | |
{ | |
package Analytics; | |
use Moose; | |
use Net::Google::DataAPI; | |
with 'Net::Google::DataAPI::Role::Service' => { | |
service => 'analytics', | |
source => 'analytics.pl by nobuo.danjou@gmail.com', | |
ns => { | |
dxp => 'http://schemas.google.com/analytics/2009', | |
} | |
}; | |
feedurl datum => ( | |
default => 'https://www.google.com/analytics/feeds/data', | |
entry_class => 'Analytics::Datum', | |
); | |
feedurl profile => ( | |
default => 'https://www.google.com/analytics/feeds/accounts/default', | |
entry_class => 'Analytics::Profile', | |
); | |
} | |
my $config = pit_get('google.com'); | |
my $service = Analytics->new( | |
{ | |
username => $config->{username}, | |
password => $config->{password}, | |
} | |
); | |
if (defined $ARGV[0]) { | |
my $profile_id = $ARGV[0]; | |
my $date_string = $ARGV[1] | |
|| DateTime->today( time_zone => 'local' ) | |
->subtract( days => 1 ) | |
->ymd('-'); | |
my $datum = $service->datum( | |
{ | |
ids => $profile_id, | |
metrics => 'ga:pageviews', | |
'start-date' => $date_string, | |
'end-date' => $date_string, | |
prettyprint => 'true', | |
} | |
); | |
print Dump { | |
date => $date_string, | |
name => $datum->name, | |
value => $datum->value, | |
} | |
} else { | |
my @profiles = $service->profiles( | |
{ | |
prettyprint => 'true', | |
} | |
); | |
for (@profiles) { | |
print Dump {title => $_->title, profile_id => $_->profile_id}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment