Mojar::Google::Analytics - Fetch Google Analytics reporting data
use Mojar::Google::Analytics; $analytics = Mojar::Google::Analytics->new( auth_user => q{1234@developer.gserviceaccount.com}, private_key => $pk, profile_id => q{5678} ); $analytics->req( dimensions => [qw(pagePath)], metrics => [qw(visitors pageviews)], sort => 'pagePath', start_index => $start, max_results => $max_resultset ); if (my $res = $analytics->fetch) { # Do something with $res->rows or $res->columns }
Google Analytics provide an API for retrieving reporting data and there are recommended client libraries for several languages but not Perl. This class provides an interface to v3 of the Core Reporting API.
Currently the only supported value is https://www.googleapis.com/analytics/v3/data/ga.
https://www.googleapis.com/analytics/v3/data/ga
An instance of the user agent to use. Defaults to a Mojo::UserAgent.
$analytics = Mojar::Google::Analytics->new( auth_user => q{1234@developer.gserviceaccount.com}, private_key => $pk, profile_id => q{5678}, timeout => 120 );
The inactivity timeout for the user agent. Any change from the default (60 sec) must be applied before the first use of the user agent, and so is best done when creating your analytics object.
The profile within your GA account you want to use.
The current request object to use. First set profile_id then set req with your parameters.
profile_id
req
$ga->profile_id(...)->req(...);
The current result object.
The user GA generated for you when you registered your application. Should end in @developer.gserviceaccount.com.
@developer.gserviceaccount.com
Currently the only supported value is urn:ietf:params:oauth:grant-type:jwt-bearer.
urn:ietf:params:oauth:grant-type:jwt-bearer
Your account's private key.
The JWT object. Defaults to
Mojar::Auth::Jwt->new( iss => $self->auth_user, private_key => $self->private_key )
How close (in seconds) to the expiry time should the current token be replaced. Defaults to 10 seconds.
The current access token.
Sets the credentials for access.
$analytics = Mojar::Google::Analytics->new( auth_user => q{1234@developer.gserviceaccount.com}, private_key => $pk, profile_id => q{5678} );
Fetches first/next batch of results based on set credentials and the req object. Automatically checks/renews the access token.
$result = $analytics->fetch # replaces $analytics->res
Check if the current token is still valid (and not too close to expiry). (See validity_margin.)
validity_margin
unless ($analytics->has_valid_token) { ... }
Force obtaining a fresh token.
$token = $analytics->renew_token # replaces $analytics->token
You need to create a low-privilege user within your GA account, granting them access to an appropriate profile. Then register your application for unattended access. That results in a username and private key that your application uses for access.
See Mojar.
Net::Google::Analytics is similar, main differences being dependencies and means of getting tokens.
To install Mojar::Google::Analytics, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojar::Google::Analytics
CPAN shell
perl -MCPAN -e shell install Mojar::Google::Analytics
For more information on module installation, please visit the detailed CPAN module installation guide.